|
|
/*
|
|
|
建议使用dev c++打开
|
|
|
最佳字体:点阵字体(6*12)
|
|
|
密码:123456
|
|
|
*/
|
|
|
#include<stdio.h>
|
|
|
#include<stdlib.h>
|
|
|
#include<string.h>
|
|
|
#include<time.h>
|
|
|
#include<conio.h>
|
|
|
#include<windows.h>
|
|
|
|
|
|
/*结构体*/
|
|
|
struct ticket{
|
|
|
int num; //班次编号
|
|
|
int man; //已订票人数
|
|
|
float hour; //行车时间
|
|
|
int Bearer; //额定载量
|
|
|
char time[10]; //发车时间小时
|
|
|
char time_min[10]; //发车时间分钟
|
|
|
char start[10]; //起始站
|
|
|
char end[10]; //终点站
|
|
|
};
|
|
|
int CAR_num=0; //初始班次总数为0
|
|
|
struct ticket sum[100]; //定义数组
|
|
|
|
|
|
/*菜单枚举 指针数组*/
|
|
|
char *menu[10]={"********************************************欢迎使用车票查询系统********************************************\n",
|
|
|
"\n\t _____________________________________功能选项_____________________________________",
|
|
|
"\n\n\t\t\t\t 1. 录入班次信息 ","\t\t 2. 增加班次 ",
|
|
|
"\n\n\t\t\t\t 3. 查询路线 ","\t\t\t 4. 浏览班次信息 ",
|
|
|
"\n\n\t\t\t\t 5. 售票 ","\t\t\t 6. 退票 ",
|
|
|
"\n\n\t\t\t\t\t 0. 退出 ",
|
|
|
"\n\t __________________________________________________________________________________"};
|
|
|
|
|
|
/*菜单函数*/
|
|
|
int menu_select(){
|
|
|
char s[5]; //定义要输入的数字功能
|
|
|
int c,i,f=0;
|
|
|
system("cls"); //清屏
|
|
|
system("color F0"); //改变颜色(F为背景颜色为白色,0字体颜色为黑色)
|
|
|
for(i=0;i<10;i++){
|
|
|
printf("%s",menu[i]); //输出菜单各项
|
|
|
}
|
|
|
printf("\n\n\n请选择(0-6):");
|
|
|
scanf("%d",&c);
|
|
|
return c;
|
|
|
}
|
|
|
|
|
|
|
|
|
/*函数声明*/
|
|
|
void entry_bata(); //录入
|
|
|
void add_bata(); //添加
|
|
|
void search_bata(); //查询
|
|
|
void search_num(); //按班次查询
|
|
|
void search_endstation(); //按终点站查询
|
|
|
void menu_a(); //搜索函数菜单
|
|
|
void exit(); //退出函数
|
|
|
void Look_through(); //浏览函数
|
|
|
void sell_ticket(); //售票
|
|
|
void return_ticket(); //退票
|
|
|
int hour_judge(int ); //时间比较
|
|
|
void Look_through_main(); //浏览函数的主函数
|
|
|
void save(); //保存函数
|
|
|
void out_or_buy_ticket(); //退售票函数
|
|
|
int add_or_entry(int , int); //录入与增加函数
|
|
|
int password(); //密码判断函数
|
|
|
|
|
|
/*录入函数*/
|
|
|
void entry_bata(){
|
|
|
int i=0,j; //i控制退出,n控制班次个数,j控制循环次数
|
|
|
system("cls");
|
|
|
if((password())==0){ //调用密码判断函数,并判断密码是否正确
|
|
|
printf("验证成功,正在载入...");
|
|
|
Sleep(1000); //为了效果更好,睡眠1s
|
|
|
system("cls");
|
|
|
FILE *fp,*fp1;
|
|
|
printf("请输入要录入的班次个数:");
|
|
|
scanf("%d",&CAR_num);
|
|
|
system("cls");
|
|
|
printf("*************************************************班次录入系统********************************************\n\n\n");
|
|
|
add_or_entry(1,CAR_num); //调用录入与增加函数
|
|
|
fp1=fopen("CAR_num.txt","w");
|
|
|
fprintf(fp1,"%d",CAR_num); //写入车次数量文件信息
|
|
|
printf("文件写入完成按任意键退出...");
|
|
|
getch();
|
|
|
fclose(fp1); //关闭文件
|
|
|
}
|
|
|
else{
|
|
|
printf("密码错误...\n");
|
|
|
getch();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/*增加班次*/
|
|
|
void add_bata(){
|
|
|
int i=0,j,t; //i控制退出,n控制班次个数,j控制循环次数
|
|
|
system("cls");
|
|
|
if((password())==0){ //调用密码函数,并判断密码是否正确
|
|
|
printf("验证成功,正在载入...");
|
|
|
Sleep(1000);
|
|
|
system("cls");
|
|
|
FILE *fp,*fp1;
|
|
|
fp=fopen("input_data.txt","a+"); //打开input_data.txt文件
|
|
|
if(fp==NULL) printf("文件打开失败");
|
|
|
printf("请输入要增加的班次个数:");
|
|
|
scanf("%d",&t);
|
|
|
system("cls");
|
|
|
printf("*************************************************班次增加系统********************************************\n\n\n");
|
|
|
add_or_entry(0,t); //调用录入与增加函数
|
|
|
fp1=fopen("CAR_num.txt","r"); //读入文件中保存的CAR_num的数据
|
|
|
fscanf(fp1,"%d",&CAR_num);
|
|
|
t=CAR_num+t;
|
|
|
fclose(fp1);
|
|
|
fp1=fopen("CAR_num.txt","w"); //改写CAR_num的数据
|
|
|
fprintf(fp1,"%d",t); //写入车次文件信息
|
|
|
printf("文件写入完成按任意键退出...");
|
|
|
getch();
|
|
|
fclose(fp1); //关闭文件
|
|
|
}
|
|
|
else{
|
|
|
printf("密码错误...\n");
|
|
|
getch();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/*搜索函数*/
|
|
|
void search_bata(){
|
|
|
int t; //选择变量
|
|
|
system("cls");
|
|
|
printf("\n\t ________________________________________查询选项_________________________________________");
|
|
|
printf("\n\n\t\t\t\t1.按班次号查询");
|
|
|
printf("\t\t\t\t2.按终点站查询");
|
|
|
printf("\n\t\t\t\t\t\t 0.退出");
|
|
|
printf("\n\n请选择:");
|
|
|
scanf("%d",&t);
|
|
|
switch(t){
|
|
|
case 1:search_num();break; //按班次查询
|
|
|
case 2:search_endstation();break; //按终点站查询
|
|
|
case 0:break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/*按班次号查询*/
|
|
|
void search_num(){
|
|
|
FILE *fp,*fp1;
|
|
|
int num,t,i,ti;
|
|
|
system("cls");
|
|
|
|
|
|
fp1=fopen("CAR_num.txt","r"); //读入车次数量文件
|
|
|
if(fp1==NULL) printf("文件打开失败");
|
|
|
rewind(fp1);
|
|
|
fscanf(fp1,"%d",&CAR_num);
|
|
|
CAR_num=CAR_num;
|
|
|
|
|
|
if((fp=fopen("input_data.txt","r"))==NULL){
|
|
|
printf("文件打开失败,按任意键返回...");
|
|
|
getch();
|
|
|
exit(0); //退出
|
|
|
}
|
|
|
rewind(fp);
|
|
|
printf("请输入要查询的班次号:\n");
|
|
|
scanf("%d",&num); //输入查询班次号
|
|
|
system("cls");
|
|
|
menu_a(); //加载搜索菜单
|
|
|
for(i=0;i<CAR_num;i++){
|
|
|
fscanf(fp,"%d%s%s%s%f%d%d",&sum[i].num,sum[i].time,sum[i].start,
|
|
|
sum[i].end,&sum[i].hour,&sum[i].Bearer,&sum[i].man);
|
|
|
t=sum[i].num;
|
|
|
if(num==t){ //判断编号是否相等
|
|
|
printf("|----------|-----------|----------|----------|----------|----------|----------|----------|\n");
|
|
|
printf("|%10d|%s%s |%s |%s |%2.f |%10d|%10d|",sum[i].num,sum[i].time,sum[i].time_min,sum[i].start,
|
|
|
sum[i].end,sum[i].hour,sum[i].Bearer,sum[i].man); //输出班次
|
|
|
ti=hour_judge(i); //调用时间判断函数
|
|
|
if(ti==1) printf(" 未发出 |\n");
|
|
|
else printf(" 已发出 |\n");
|
|
|
}
|
|
|
}
|
|
|
printf("按任意键返回...");
|
|
|
getch();
|
|
|
fclose(fp);fclose(fp1);
|
|
|
}
|
|
|
|
|
|
/*按终点站查询*/
|
|
|
void search_endstation(){
|
|
|
FILE *fp,*fp1;
|
|
|
int ti,i; //判断时间
|
|
|
char a[16]; //储存要查询的终点站
|
|
|
system("cls");
|
|
|
|
|
|
fp1=fopen("CAR_num.txt","r"); //读入车次数量文件
|
|
|
rewind(fp1);
|
|
|
fscanf(fp1,"%d",&CAR_num);
|
|
|
CAR_num=CAR_num;
|
|
|
|
|
|
if((fp=fopen("input_data.txt","r"))==NULL){
|
|
|
printf("文件打开失败,按任意键返回...");
|
|
|
getch();
|
|
|
exit(0); //退出
|
|
|
}
|
|
|
rewind(fp);
|
|
|
printf("请输入要查询的终点站:\n");
|
|
|
scanf("%s",a); //输入查询终点站
|
|
|
system("cls");
|
|
|
menu_a(); //加载搜索菜单
|
|
|
for(i=0;i<CAR_num;i++){
|
|
|
fscanf(fp,"%d%s%s%s%f%d%d",&sum[i].num,sum[i].time,sum[i].start,
|
|
|
sum[i].end,&sum[i].hour,&sum[i].Bearer,&sum[i].man);
|
|
|
if((strcmp(a,sum[i].end))==0){ //用strcmp函数判断终点站是否相同
|
|
|
printf("|----------|-----------|----------|----------|----------|----------|----------|----------|\n");
|
|
|
printf("|%10d|%s%s |%s |%s |%2.f |%10d|%10d|",sum[i].num,sum[i].time,sum[i].time_min,sum[i].start,
|
|
|
sum[i].end,sum[i].hour,sum[i].Bearer,sum[i].man); //输出班次
|
|
|
ti=hour_judge(i); //调用时间判断函数
|
|
|
if(ti==1) printf(" 未发出 |\n");
|
|
|
else printf(" 已发出 |\n");
|
|
|
}
|
|
|
}
|
|
|
printf("按任意键返回...");
|
|
|
getch();
|
|
|
fclose(fp);fclose(fp1);
|
|
|
}
|
|
|
|
|
|
/*搜索函数菜单*/
|
|
|
void menu_a(){
|
|
|
printf("|______________________________________|车次信息|________________________________________|\n\n");
|
|
|
printf("|---班次---|-发车时间--|--起点站--|--终点站--|-行车时间-|-额定载量-|已定票人数|---状态---|\n");
|
|
|
}
|
|
|
|
|
|
/*退出函数*/
|
|
|
void exit(){
|
|
|
system("cls");
|
|
|
printf("感谢您使用本款系统,再见\n");
|
|
|
getch();
|
|
|
}
|
|
|
|
|
|
/*浏览函数*/
|
|
|
void Look_through(){
|
|
|
Look_through_main(); //调用搜索主函数
|
|
|
printf("\n\n按任意键返回...");
|
|
|
getch();
|
|
|
}
|
|
|
|
|
|
/*售票*/
|
|
|
void sell_ticket(){
|
|
|
int t,j,u; //t控制输入车次的编号 j控制便历编号,u等于时间函数返回的值
|
|
|
out_or_buy_ticket(); //调用退售票主函数
|
|
|
printf("\n\n请选择你要购票的车次编号:");
|
|
|
scanf("%d",&t);
|
|
|
for(j=0;j<CAR_num;j++){ //遍历车次编号,直至找到你所选的编号;
|
|
|
if(t==sum[j].num){
|
|
|
system("cls");
|
|
|
printf("以为您筛选出符合条件的车次\n\n");
|
|
|
menu_a();
|
|
|
u=hour_judge(j); //调用时间函数
|
|
|
if(u==1&&(sum[j].Bearer>sum[j].man)){
|
|
|
printf("|%10d|%s%s |%s |%s |%2.f |%10d|%10d|",sum[j].num,sum[j].time,sum[j].time_min,sum[j].start,
|
|
|
sum[j].end,sum[j].hour,sum[j].Bearer,sum[j].man); //输出班次
|
|
|
printf(" 能购票 |\n");
|
|
|
printf("\n\n\n\t\t\t是否购票(1/0)? ");
|
|
|
int check;
|
|
|
scanf("%d",&check); //判断是否购票
|
|
|
getchar();
|
|
|
if(check==1){
|
|
|
sum[j].man=sum[j].man+1;
|
|
|
save(); //比较妥协的保存方式
|
|
|
printf("购买完成\n");
|
|
|
}
|
|
|
printf("按任意键返回...");
|
|
|
getch();
|
|
|
}
|
|
|
else{
|
|
|
printf("\n\n\n未找到能够购票的车次...");
|
|
|
getch();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/*退票*/
|
|
|
void return_ticket(){
|
|
|
int j,t,u;
|
|
|
out_or_buy_ticket();
|
|
|
printf("\n\n请选择你要退票的车次编号:");
|
|
|
scanf("%d",&t);
|
|
|
for(j=0;j<CAR_num;j++){ //遍历车次编号,直至找到你所选的编号;
|
|
|
if(t==sum[j].num){
|
|
|
system("cls");
|
|
|
printf("请你确认\n\n");
|
|
|
menu_a();
|
|
|
printf("|%10d|%s%s |%s |%s |%2.f |%10d|%10d|",sum[j].num,sum[j].time,sum[j].time_min,sum[j].start,
|
|
|
sum[j].end,sum[j].hour,sum[j].Bearer,sum[j].man); //输出班次
|
|
|
printf(" 已购 |\n");
|
|
|
printf("\n\n\n\t\t\t是否退票(1/0)? ");
|
|
|
int check;
|
|
|
scanf("%d",&check); //判断是否退票
|
|
|
getchar();
|
|
|
if(check==1){
|
|
|
if((hour_judge(j))==1){
|
|
|
sum[j].man=sum[j].man-1;
|
|
|
save(); //比较妥协的保存方式
|
|
|
printf("退票完成\n");
|
|
|
}
|
|
|
else{
|
|
|
printf("\n\n\t\t\t抱歉,列车已发出,无法退票!\n");
|
|
|
printf("\t\t\t按任意键返回主菜单");
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
printf("按任意键返回...");
|
|
|
getch();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/*时间大小判断*/
|
|
|
int hour_judge(int i){ //传入i,结构体数组的i
|
|
|
struct tm *local; //时间结构体
|
|
|
time_t t; //把时间给t
|
|
|
t=time(NULL); //相当于给time初始化,使其保存当前时间
|
|
|
local=localtime(&t); //获取当前系统时间
|
|
|
if((local->tm_hour<atoi(sum[i].time))||(local->tm_hour==atoi(sum[i].time)&&local->tm_min<atoi(sum[i].time_min))) //atoi将字符串转换成一个整数值
|
|
|
return 1; //atoi函数将字符转化为整形
|
|
|
else //在时间之内返回1,否则返回0
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
/*浏览主函数*/
|
|
|
void Look_through_main(){
|
|
|
FILE *fp1,*fp;
|
|
|
int i,ti;
|
|
|
fp1=fopen("CAR_num.txt","r"); //读入车次数量文件
|
|
|
rewind(fp1);
|
|
|
fscanf(fp1,"%d",&CAR_num);
|
|
|
CAR_num=CAR_num;
|
|
|
system("cls");
|
|
|
if((fp=fopen("input_data.txt","r"))==NULL){
|
|
|
printf("文件打开失败,按任意键返回...");
|
|
|
getch();
|
|
|
exit(0); //退出
|
|
|
}
|
|
|
rewind(fp);
|
|
|
menu_a(); //加载搜索菜单
|
|
|
for(i=0;i<CAR_num;i++){
|
|
|
fscanf(fp,"%d%s%s%s%f%d%d",&sum[i].num,sum[i].time,sum[i].start,
|
|
|
sum[i].end,&sum[i].hour,&sum[i].Bearer,&sum[i].man);
|
|
|
|
|
|
printf("|----------|-----------|----------|----------|----------|----------|----------|----------|\n");
|
|
|
printf("|%10d|%s%s |%s |%s |%2.f |%10d|%10d|",sum[i].num,sum[i].time,sum[i].time_min,sum[i].start,
|
|
|
sum[i].end,sum[i].hour,sum[i].Bearer,sum[i].man); //输出班次
|
|
|
ti=hour_judge(i); //调用时间判断函数
|
|
|
if(ti==1) printf(" 未发出 |\n");
|
|
|
else printf(" 已发出 |\n");
|
|
|
}
|
|
|
fclose(fp);fclose(fp1); //关闭文件
|
|
|
}
|
|
|
|
|
|
/*保存函数*/
|
|
|
void save(){
|
|
|
FILE *fp,*fp1;
|
|
|
int j;
|
|
|
fp1=fopen("CAR_num.txt","r"); //读入车次数量文件
|
|
|
rewind(fp1);
|
|
|
fscanf(fp1,"%d",&CAR_num);
|
|
|
CAR_num=CAR_num;
|
|
|
fp=fopen("input_data.txt","w+");
|
|
|
for(j=0;j<CAR_num;j++){
|
|
|
fprintf(fp,"%d %s%s %s %s %.2f %d %d\n",sum[j].num,sum[j].time,sum[j].time_min,sum[j].start,sum[j].end,sum[j].hour,sum[j].Bearer,sum[j].man);
|
|
|
}
|
|
|
fclose(fp);fclose(fp1);
|
|
|
}
|
|
|
|
|
|
/*退售票主函数*/
|
|
|
void out_or_buy_ticket(){
|
|
|
int t,j,u; //t控制输入车次的编号 j控制便历编号,u等于时间函数返回的值
|
|
|
Look_through_main(); //调用搜索主函数
|
|
|
|
|
|
FILE *fp1,*fp;
|
|
|
int i,ti;
|
|
|
fp1=fopen("CAR_num.txt","r"); //读入车次数量文件
|
|
|
rewind(fp1);
|
|
|
fscanf(fp1,"%d",&CAR_num);
|
|
|
CAR_num=CAR_num;
|
|
|
system("cls");
|
|
|
if((fp=fopen("input_data.txt","r"))==NULL){
|
|
|
printf("文件打开失败,按任意键返回...");
|
|
|
getch();
|
|
|
exit(0); //退出
|
|
|
}
|
|
|
rewind(fp);
|
|
|
menu_a(); //加载搜索菜单
|
|
|
for(i=0;i<CAR_num;i++){
|
|
|
fscanf(fp,"%d%s%s%s%f%d%d",&sum[i].num,sum[i].time,sum[i].start,
|
|
|
sum[i].end,&sum[i].hour,&sum[i].Bearer,&sum[i].man);
|
|
|
printf("|----------|-----------|----------|----------|----------|----------|----------|----------|\n");
|
|
|
printf("|%10d|%s%s |%s |%s |%2.f |%10d|%10d|",sum[i].num,sum[i].time,sum[i].time_min,sum[i].start,
|
|
|
sum[i].end,sum[i].hour,sum[i].Bearer,sum[i].man); //输出班次
|
|
|
ti=hour_judge(i); //调用时间判断函数
|
|
|
if(ti==1) printf(" 未发出 |\n");
|
|
|
else printf(" 已发出 |\n");
|
|
|
|
|
|
fclose(fp); fclose(fp1);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/*信息录入与增加*/
|
|
|
int add_or_entry(int h,int numb){
|
|
|
int j;
|
|
|
FILE *fp;
|
|
|
if(h==1) fp=fopen("input_data.txt","w+"); //创建并打开input_data.txt文件
|
|
|
if(h==0) fp=fopen("input_data.txt","a+"); //打开input_data.txt文件
|
|
|
for(j=0;j<numb;j++)
|
|
|
{
|
|
|
printf("请输入第%d个班次信息,按任意键开始...\n",j+1);
|
|
|
getch();
|
|
|
system("cls");
|
|
|
printf("请输入班次编号:");
|
|
|
scanf("%d",&sum[j].num);
|
|
|
printf("请输入发车时间(xx: xx):");
|
|
|
|
|
|
scanf("%s",sum[j].time);
|
|
|
scanf("%s",sum[j].time_min);
|
|
|
|
|
|
if(sum[j].time[0]>='0'&&sum[j].time[0]<='9'&&sum[j].time[1]>='0'&&sum[j].time[1]<='9'&&sum[j].time[2]==':'&&sum[j].time_min[0]>='0'&&sum[j].time_min[0]<='9'&&sum[j].time_min[1]<='9'&&sum[j].time_min[1]>='0'){
|
|
|
printf("请输入起点站:");
|
|
|
scanf("%s",sum[j].start);
|
|
|
printf("请输入终点站:");
|
|
|
scanf("%s",sum[j].end);
|
|
|
printf("请输入行车时间:");
|
|
|
scanf("%f",&sum[j].hour);
|
|
|
printf("请输入额定载量:");
|
|
|
scanf("%d",&sum[j].Bearer);
|
|
|
printf("请输入已订票人数:");
|
|
|
scanf("%d",&sum[j].man); //为了增强txt文件的可读性,运用了fprintf
|
|
|
fprintf(fp,"%d %s%s %s %s %.2f %d %d\n",sum[j].num,sum[j].time,sum[j].time_min,sum[j].start,sum[j].end,sum[j].hour,sum[j].Bearer,sum[j].man);
|
|
|
}
|
|
|
else{
|
|
|
printf("输入格式错误");
|
|
|
}
|
|
|
}
|
|
|
fclose(fp);
|
|
|
}
|
|
|
|
|
|
/*密码判断*/
|
|
|
int password(){
|
|
|
char password[10]={NULL};
|
|
|
int i,f;
|
|
|
printf("\n\n\n\n\t\t\t\t请输入管理员密码:\n");
|
|
|
for(i=0;i<6;i++)
|
|
|
{
|
|
|
password[i]=getch();
|
|
|
printf("*");
|
|
|
}
|
|
|
password[i]='\0';
|
|
|
printf("\n");
|
|
|
f=strcmp(password,"123456");
|
|
|
Sleep(100);
|
|
|
return f;
|
|
|
}
|
|
|
|
|
|
/*主函数*/
|
|
|
int main(){
|
|
|
system("cls"); //清屏
|
|
|
while(1){
|
|
|
switch(menu_select()) //菜单函数
|
|
|
{
|
|
|
case 1:entry_bata();break; //录入
|
|
|
case 2:add_bata();break; //增加
|
|
|
case 3:search_bata();break; //查询
|
|
|
case 4:Look_through();break; //浏览
|
|
|
case 5:sell_ticket();break; //售票
|
|
|
case 6:return_ticket();break; //退票
|
|
|
case 0:exit();return 0; //退出
|
|
|
default: //其他
|
|
|
printf("输入错误...");
|
|
|
getch();
|
|
|
}
|
|
|
}
|
|
|
}
|