|
|
#define _CRT_SECURE_NO_WARNINGS
|
|
|
#include <stdio.h>
|
|
|
#include <string.h>
|
|
|
#include <stdlib.h>
|
|
|
#include <time.h> //时间库函数
|
|
|
//#include <conio.h> //输入输出库
|
|
|
|
|
|
int class_num = 0; //初始班次总数为0
|
|
|
|
|
|
typedef struct car
|
|
|
{
|
|
|
char number[10]; //车次序号
|
|
|
char hour[3]; //发车时间小时
|
|
|
char min[3]; //发车时间分钟
|
|
|
char from[10]; //起始站
|
|
|
char to[10]; //终点站
|
|
|
float hours; //车程时间
|
|
|
int max; //最大车载容量
|
|
|
int bought; //已售票数
|
|
|
} CLASS;
|
|
|
|
|
|
CLASS records[1000]; // 可记录1000车次
|
|
|
void Menu();
|
|
|
void Import();
|
|
|
void Exit();
|
|
|
void Buy();
|
|
|
void Refund();
|
|
|
void Query();
|
|
|
void Exploer();
|
|
|
void Insert();
|
|
|
void ShowTableHead();
|
|
|
void ShowTableFoot();
|
|
|
void Find(char s1[], char s2[]);
|
|
|
|
|
|
int main()
|
|
|
{
|
|
|
Menu();
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
void Menu()
|
|
|
{
|
|
|
system("cls");
|
|
|
time_t rawtime; //获取系统时间函数
|
|
|
struct tm *timeinfo;
|
|
|
time(&rawtime);
|
|
|
timeinfo = localtime(&rawtime);
|
|
|
printf("系统当前时间:%d:%d\n", timeinfo->tm_hour, timeinfo->tm_min);
|
|
|
int InputNumber;
|
|
|
printf("/* 当前班次信息: */\n");
|
|
|
printf("/* 1、购票 */\n");
|
|
|
printf("/* 2、退票 */\n");
|
|
|
printf("/* 3、查询路线 */\n");
|
|
|
printf("/* 4、浏览所有班次 */\n");
|
|
|
printf("/* 5、增加班次信息 */\n");
|
|
|
printf("/* 6、录入班次信息(首次使用) */\n");
|
|
|
printf("/* 0、退出 */\n");
|
|
|
printf("请输入相应功能选项:");
|
|
|
scanf("%d", &InputNumber);
|
|
|
do
|
|
|
{
|
|
|
switch (InputNumber)
|
|
|
{
|
|
|
default:
|
|
|
system("cls");
|
|
|
printf("请输入正确选项!!\n");
|
|
|
system("pause");
|
|
|
Menu();
|
|
|
break;
|
|
|
case 0:
|
|
|
Exit();
|
|
|
break;
|
|
|
case 1:
|
|
|
Buy();
|
|
|
Menu();
|
|
|
break;
|
|
|
case 2:
|
|
|
Refund();
|
|
|
Menu();
|
|
|
break;
|
|
|
case 3:
|
|
|
Query();
|
|
|
Menu();
|
|
|
break;
|
|
|
case 4:
|
|
|
Exploer();
|
|
|
Menu();
|
|
|
break;
|
|
|
case 5:
|
|
|
Insert();
|
|
|
Menu();
|
|
|
break;
|
|
|
case 6:
|
|
|
Import();
|
|
|
Menu();
|
|
|
break;
|
|
|
}
|
|
|
} while (InputNumber != 0);
|
|
|
}
|
|
|
void Scanf(int i, int j) //用户用来输入班次
|
|
|
{
|
|
|
for (;;)
|
|
|
{
|
|
|
printf("请输入发车时间(xx xx)");
|
|
|
scanf("%s%s", records[i].hour, records[i].min);
|
|
|
if ((atoi(records[i].hour) < 24 && atoi(records[i].hour) >= 0) && (atoi(records[i].min) < 60 && atoi(records[i].min) >= 0))
|
|
|
break;
|
|
|
else
|
|
|
{
|
|
|
printf("输入错误!\n");
|
|
|
system("pause"); //使达到按下任意键返回的作用 在新版c标准中要使用system("pause");
|
|
|
}
|
|
|
}
|
|
|
printf("请输入起点站:\n");
|
|
|
scanf("%s", records[i].from);
|
|
|
printf("请输入终点站:\n");
|
|
|
scanf("%s", records[i].to);
|
|
|
printf("请输入行车时间:\n");
|
|
|
scanf("%f", &records[i].hours);
|
|
|
printf("请输入额定载量:\n");
|
|
|
scanf("%d", &records[i].max);
|
|
|
for (;;)
|
|
|
{
|
|
|
printf("请输入已售票数:\n");
|
|
|
scanf("%d", &records[i].bought);
|
|
|
if (records[i].bought <= records[i].max)
|
|
|
break;
|
|
|
else
|
|
|
{
|
|
|
printf("输入错误!\n");
|
|
|
system("pause");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
void Save() //文件保存函数
|
|
|
{
|
|
|
FILE *fp1, *fp2;
|
|
|
if ((fp1 = fopen("sql.txt", "wb")) == NULL)
|
|
|
{
|
|
|
printf("车次数据文件不存在\n");
|
|
|
exit(0);
|
|
|
}
|
|
|
if ((fp2 = fopen("num.txt", "wb")) == NULL)
|
|
|
{
|
|
|
printf("车次序号数据文件不存在\n");
|
|
|
exit(0);
|
|
|
}
|
|
|
printf("数据保存中,请稍后\n");
|
|
|
fwrite(&class_num, sizeof(int), 1, fp2); //写入文件信息
|
|
|
fwrite(records, sizeof(CLASS), class_num, fp1);
|
|
|
fclose(fp1);
|
|
|
fclose(fp2); ///关闭文件
|
|
|
}
|
|
|
void Load() //加载数据文件
|
|
|
{
|
|
|
FILE *fp1, *fp2;
|
|
|
if ((fp1 = fopen("sql.txt", "rb+")) == NULL)
|
|
|
{
|
|
|
system("cls");
|
|
|
printf("车次数据文件不存在\n");
|
|
|
system("pause");
|
|
|
exit(0);
|
|
|
}
|
|
|
if ((fp2 = fopen("num.txt", "rb+")) == NULL)
|
|
|
{
|
|
|
system("cls");
|
|
|
printf("车次序号数据文件不存在\n");
|
|
|
system("pause");
|
|
|
exit(0);
|
|
|
}
|
|
|
fread(&class_num, sizeof(int), 1, fp2); //读入信息
|
|
|
fread(records, sizeof(CLASS), class_num, fp1); //读入信息
|
|
|
fclose(fp1);
|
|
|
fclose(fp2);
|
|
|
}
|
|
|
int Judge(int i) //判断时间是否超出
|
|
|
{
|
|
|
struct tm *local; //时间结构体
|
|
|
time_t t; //把当前时间给t
|
|
|
local = localtime(&t); //获取当前系统时间
|
|
|
if (local->tm_hour < atoi(records[i].hour) || local->tm_hour == atoi(records[i].hour) && local->tm_min < atoi(records[i].min))
|
|
|
//atoi函数将字符串转换成一个整数值
|
|
|
return 1;
|
|
|
else
|
|
|
return 0;
|
|
|
}
|
|
|
int FindCarByNum(char s1[]) //查找班次
|
|
|
{
|
|
|
int i, h = 0;
|
|
|
ShowTableHead();
|
|
|
for (i = 0; i < class_num; i++)
|
|
|
{
|
|
|
if (strcmp(s1, records[i].number) == 0) //判断数据文件中的车次号与输入车次号是否相等,如果符合则输出车次信息
|
|
|
{
|
|
|
printf("|----------|----------|----------|----------|--------|--------|--------|\n");
|
|
|
printf("|%10s|%5s:%-4s|%10s|%10s|%8.1f|%8d|%8d|", records[i].number, records[i].hour,
|
|
|
records[i].min, records[i].from, records[i].to, records[i].hours, records[i].max, records[i].bought);
|
|
|
h += 2;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
ShowTableFoot();
|
|
|
return i;
|
|
|
}
|
|
|
void Import()
|
|
|
{
|
|
|
int i = 0, j = 5, h;
|
|
|
char s[5];
|
|
|
FILE *fp;
|
|
|
system("cls");
|
|
|
if ((fp = fopen("num.txt", "rb")) != NULL)
|
|
|
{
|
|
|
printf("车次信息已经存在,请选择添加车次功能!\n");
|
|
|
system("pause");
|
|
|
i = 1;
|
|
|
}
|
|
|
if (i == 0)
|
|
|
{
|
|
|
system("cls");
|
|
|
printf("请输入要录入班次总数:\n");
|
|
|
scanf("%d", &class_num); //指向班次指针
|
|
|
system("cls");
|
|
|
for (i = 0; i < class_num; i++)
|
|
|
{
|
|
|
system("cls");
|
|
|
printf("请输入第%d个班次信息:\n", i + 1);
|
|
|
h = -1;
|
|
|
for (; h != i;) ///循环输出
|
|
|
{
|
|
|
printf("请输入班次:\n");
|
|
|
scanf("%s", records[i].number);
|
|
|
for (h = 0; h < i; h++)
|
|
|
if (strcmp(records[h].number, records[i].number) == 0)
|
|
|
//判断字符串是否相等
|
|
|
{
|
|
|
printf("输入错误!该班次已存在!\n");
|
|
|
break; ///返回
|
|
|
}
|
|
|
}
|
|
|
Scanf(i, j);
|
|
|
j = 5;
|
|
|
}
|
|
|
Save();
|
|
|
}
|
|
|
}
|
|
|
void Exit()
|
|
|
{
|
|
|
printf("感谢您的使用!!!");
|
|
|
exit(0);
|
|
|
}
|
|
|
void Buy()
|
|
|
{
|
|
|
int i;
|
|
|
char num[10];
|
|
|
system("cls");
|
|
|
printf("1. 售票\n");
|
|
|
printf("2. 返回\n");
|
|
|
printf("请选择(1-2):\n");
|
|
|
scanf("%d", &i);
|
|
|
if (i == 1)
|
|
|
{
|
|
|
Load();
|
|
|
Query();
|
|
|
printf("请输入要订票的班次(若无请输入0):\n");
|
|
|
scanf("%s", num);
|
|
|
for (i = 0; i < class_num; i++)
|
|
|
if (strcmp(num, records[i].number) == 0) //判断字符串是否相等
|
|
|
if (records[i].max > records[i].bought && Judge(i) == 1)
|
|
|
//判断时间是否超出当前系统时间并且已售车票没超出最大客量
|
|
|
{
|
|
|
records[i].bought++;
|
|
|
printf("通向%s班次为%s的票订票成功!\n", records[i].to, records[i].number);
|
|
|
Save();
|
|
|
system("pause");
|
|
|
break;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
printf("该班次已满或已发出!\n");
|
|
|
system("pause");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
void Refund()
|
|
|
{
|
|
|
int i;
|
|
|
char num[10];
|
|
|
system("cls");
|
|
|
printf("1. 退票\n");
|
|
|
printf("2. 返回\n");
|
|
|
printf("请选择(1-2)\n:");
|
|
|
scanf("%d", &i);
|
|
|
if (i == 1)
|
|
|
{
|
|
|
system("cls");
|
|
|
Load();
|
|
|
printf("请输入要退票的班次:\n");
|
|
|
scanf("%s", num);
|
|
|
i = FindCarByNum(num); //调用班次查询函数
|
|
|
if (strcmp(num, records[i].number) == 0)
|
|
|
if (Judge(i)) //判断时间是否超出当前系统时间
|
|
|
{
|
|
|
printf("确定(Y/N)?");
|
|
|
scanf("%s", num);
|
|
|
if (num[0] == 'y' || num[0] == 'Y')
|
|
|
{
|
|
|
records[i].bought--;
|
|
|
printf("退票成功!\n");
|
|
|
Save();
|
|
|
system("pause");
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
printf("该班车已发出,无法退票!\n");
|
|
|
system("pause");
|
|
|
}
|
|
|
if (i == class_num)
|
|
|
{
|
|
|
printf("输入错误!\n");
|
|
|
system("pause");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
void Query()
|
|
|
{
|
|
|
int i;
|
|
|
char s1[10] = {'\0'}, s2[10] = {'\0'};
|
|
|
system("cls");
|
|
|
printf("1. 按班次查询\n");
|
|
|
printf("2. 按终点站查询\n");
|
|
|
printf("3. 退出\n");
|
|
|
printf("请选择(1-3):\n");
|
|
|
scanf("%d", &i);
|
|
|
Load();
|
|
|
switch (i)
|
|
|
{
|
|
|
case 1:
|
|
|
printf("请输入要查询的班次:\n"); //////s1为班次
|
|
|
scanf("%s", s1);
|
|
|
Find(s1, s2);
|
|
|
break;
|
|
|
case 2:
|
|
|
printf("请输入要查询终点站:\n"); /////s2为终点站
|
|
|
scanf("%s", s2);
|
|
|
Find(s1, s2);
|
|
|
break;
|
|
|
case 3:
|
|
|
break;
|
|
|
default:
|
|
|
printf("输入错误!\n");
|
|
|
break;
|
|
|
}
|
|
|
system("pause");
|
|
|
}
|
|
|
void Find(char s1[], char s2[])
|
|
|
{
|
|
|
int i, h = 0, m;
|
|
|
ShowTableHead();
|
|
|
if (s2[0] == '\0')
|
|
|
m = 1; //1为无条件执行
|
|
|
else
|
|
|
m = 0;
|
|
|
for (i = 0; i < class_num; i++)
|
|
|
if (strcmp(s1, records[i].number) == 0 || strcmp(s2, records[i].to) == 0)
|
|
|
{
|
|
|
printf("\n|----------|----------|----------|----------|--------|--------|--------|\n");
|
|
|
printf("|%10s|%5s:%-4s|%10s|%10s|%8.1f|%8d|%8d|", records[i].number, records[i].hour, records[i].min,
|
|
|
records[i].from, records[i].to, records[i].hours, records[i].max, records[i].bought);
|
|
|
h += 2;
|
|
|
if (m == 1)
|
|
|
break;
|
|
|
}
|
|
|
ShowTableFoot();
|
|
|
if (h == 0)
|
|
|
printf("要查找的班次不存在!\n");
|
|
|
}
|
|
|
void Insert()
|
|
|
{
|
|
|
int i, j = 5;
|
|
|
Load();
|
|
|
system("cls");
|
|
|
printf("1. 添加班次\n");
|
|
|
printf("2. 返回\n");
|
|
|
printf("请选择(1-2)\n");
|
|
|
scanf("%d", &i);
|
|
|
if (i == 1)
|
|
|
{
|
|
|
system("cls");
|
|
|
printf("1. 请输入要增加的班次:\n");
|
|
|
scanf("%s", records[class_num].number);
|
|
|
for (i = 0; i < class_num; i++)
|
|
|
if (strcmp(records[class_num].number, records[i].number) == 0)
|
|
|
//判断车次是否重复
|
|
|
{
|
|
|
printf("输出车次已存在!\n");
|
|
|
system("pause");
|
|
|
break;
|
|
|
}
|
|
|
if (i == class_num)
|
|
|
{
|
|
|
Scanf(i, j);
|
|
|
class_num++; ///使班次数加1
|
|
|
Save();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
void Exploer() //列出车次表函数
|
|
|
{
|
|
|
int i, j;
|
|
|
system("cls");
|
|
|
Load();
|
|
|
ShowTableHead();
|
|
|
//ShowTableBody
|
|
|
for (i = 0, j = 0; i < class_num; i++, j += 2)
|
|
|
{
|
|
|
printf("\n|----------|----------|----------|----------|--------|--------|--------|\n");
|
|
|
if (Judge(i))
|
|
|
printf("|%10s|%5s:%-4s|%10s|%10s|%8.1f|%8d|%8d|", records[i].number, records[i].hour,
|
|
|
records[i].min, records[i].from, records[i].to, records[i].hours,
|
|
|
records[i].max, records[i].bought);
|
|
|
else
|
|
|
printf("|%10s| 已发车 |%10s|%10s|%8.1f|%8d|%8d|", records[i].number,
|
|
|
records[i].from, records[i].to, records[i].hours, records[i].max, records[i].bought);
|
|
|
}
|
|
|
ShowTableFoot();
|
|
|
printf("\n");
|
|
|
system("pause");
|
|
|
}
|
|
|
void ShowTableHead()
|
|
|
{
|
|
|
int i = 2;
|
|
|
system("cls");
|
|
|
printf("**************************当前所有车次表*********************************\n");
|
|
|
printf("\n|----------|----------|----------|----------|--------|--------|--------|\n");
|
|
|
printf("\n| 车次 | 发的时间 | 起点站 | 终点站 |行车时间|额定载量| 已售票 |\n");
|
|
|
}
|
|
|
void ShowTableFoot()
|
|
|
{
|
|
|
printf("\n|----------|----------|----------|----------|--------|--------|--------|\n");
|
|
|
} |