车票管并去理系统 输入文件显示出错
用VC++搞了个车票管理系统,结果在输入文件时出了问题,请高手指教,谢谢!!源码如下:程序代码:
#include<stdio.h> #include<stdlib.h> #include<dos.h> #include<time.h> #include<conio.h> #include<string.h> const int MAX=20; const int N=50; const int MAXFILENAME=100; enum {FALSE,TRUE}; struct time { int hour; int minutes; }; struct ticket { int SerialNumber; struct time SetOut; char DeparturePoint[MAX]; char TerminalPoint[MAX]; float LastTime; int FixNumber; int FixedNumber; }car[N]; int RecordNumber; int FLAG=FALSE; /**********************************************/ int ShowMessage(void); //载入原始数据 void mainmenu(void); //主菜单 void InsertMessage(void); //录入班次信息 void SearchMenu(void); //查询路线子菜单 void SearchByNumber(void); //按班次号查询 void SearchByAddress(void); //按终点站查询 void TicketManagement(void); //车票售出与退回 void TicketOrder(void); //售票 void TicketDelete(void); //退票 void exit(void); //退出该系统 void SaveRecord(ticket x); //储存信息到文件中 /********************************************/ int main(void) { do{ mainmenu(); }while(FLAG==FALSE); return 0; } void mainmenu(void) { system("color 1f"); system("mode con: cols=140 lines=130"); char functionNumber; printf(" 车票管理系统\n\n"); printf(" 制作:hmm182007\n"); printf(" 日期:2007/11/25/19:57\n"); printf("============================================================================\n"); printf(" 1.录入班次信息\t\n"); printf(" 2.浏览班次信息\t\n"); printf(" 3.查询行车路线\t\n"); printf(" 4.售票与退票系统\t\n"); printf(" 5.退出该系统\t\n"); printf("============================================================================\n"); printf("请选择你所需要的功能:"); scanf("%c",&functionNumber); switch(functionNumber) { case '1': { InsertMessage(); printf("\n按任意键回主菜单......\n"); getchar(); getchar(); } break; case '2': { RecordNumber=ShowMessage(); printf("\n按任意键回主菜单......\n"); getchar(); getchar(); } break; case '3': { SearchMenu(); printf("\n按任意键回主菜单......\n"); getchar(); getchar(); } break; case '4': { TicketManagement(); getchar(); getchar(); } break; case '5':FLAG=TRUE;exit();break; default: { printf("输入错误,请确保你的输入为1--5.\n"); printf("按任意键回主菜单......\n"); getchar(); getchar(); } }//switch ends FLAG=FALSE; } void InsertMessage(void) { FILE *fp; char filename[MAXFILENAME]; int i; printf("请输入文件名或者文件路径名:"); scanf("%s",filename); if((fp=fopen(filename,"r"))==NULL) { printf("文件%s不可读,请确认文件%s存在或者文件路径正确.\n\n",filename,filename); mainmenu(); } for(i=RecordNumber;!feof(fp)&&!ferror(fp);i++) { fscanf(fp,"%d%d%d",&car[i].SerialNumber,&car[i].SetOut.hour,&car[i].SetOut.minutes); fscanf(fp,"%s%s%f%d%d",&car[i].DeparturePoint,&car[i].TerminalPoint,&car[i].LastTime,&car[i].FixNumber,&car[i].FixedNumber); SaveRecord(*(car+i)); } RecordNumber=i; /*******************************输出更新后的订票记录************************************************************/ printf("班次\t发车时间\t起点\t终点\t行车时间(小时)\t额定载量\t已定票人数\n"); for(i=0;i<RecordNumber;i++) { printf("%d\t%d:%d\t\t",car[i].SerialNumber,car[i].SetOut.hour,car[i].SetOut.minutes); printf("%s\t%s\t\t%.1f\t %d\t\t%d\n",car[i].DeparturePoint,car[i].TerminalPoint,car[i].LastTime,car[i].FixNumber,car[i].FixedNumber); } fclose(fp); } int ShowMessage(void) { int i; FILE *fp; if((fp=fopen("add.txt","r"))==NULL) { printf("读取数据失败,请检查文件add.txt是否存在.\n"); exit(-1); } for(i=RecordNumber;!feof(fp)&&!ferror(fp);i++) { fscanf(fp,"%d%d%d",&car[i].SerialNumber,&car[i].SetOut.hour,&car[i].SetOut.minutes); fscanf(fp,"%s%s%f%d%d",&car[i].DeparturePoint,&car[i].TerminalPoint,&car[i].LastTime,&car[i].FixNumber,&car[i].FixedNumber); } int count=i; printf("班次\t发车时间\t起点\t终点\t行车时间(小时)\t额定载量\t已定票人数\n"); for(i=0;i<count;i++) { printf("%d\t%d:%d\t\t",car[i].SerialNumber,car[i].SetOut.hour,car[i].SetOut.minutes); printf("%s\t%s\t\t%.1f\t %d\t\t%d\t",car[i].DeparturePoint,car[i].TerminalPoint,car[i].LastTime,car[i].FixNumber,car[i].FixedNumber); /*调用系统时间实现的功能:如果车已发出,则打出提示"此车已发出",但是此处有错误*/ time_t tval; struct tm *now; tval = time(NULL); now = localtime(&tval); if((now->tm_hour==car[i].SetOut.hour&&now->tm_min>car[i].SetOut.minutes)||(now->tm_hour>car[i].SetOut.hour)) printf("此车已发出"); printf("\n"); } return count; } void SearchMenu(void) { system("color 1f"); system("mode con: cols=140 lines=130"); char functionNumber; printf(" 查询子菜单\n"); printf("============================================================================\n"); printf(" 1.按班次号查询\t\n"); printf(" 2.按终点站查询\t\n"); printf(" 3.返回主菜单\t\n"); printf("============================================================================\n"); printf("请选择你所需要的功能:"); scanf("%*c%c",&functionNumber); switch(functionNumber) { case '1':SearchByNumber();break; case '2':SearchByAddress();break; case '3': getchar(); mainmenu(); break; default: printf("输入错误,请确保你的输入为1--3.\n"); printf("按任意键回查询子菜单......\n"); getchar(); getchar(); SearchMenu(); } } void SearchByNumber(void) { int SearchNumber; printf("请输入要查询的班次号:"); scanf("%d",&SearchNumber); printf("班次\t发车时间\t起点\t终点\t行车时间(小时)\t额定载量\t已定票人数\n"); printf("%d\t%d:%d\t\t",car[SearchNumber-1].SerialNumber,car[SearchNumber-1].SetOut.hour,car[SearchNumber-1].SetOut.minutes); printf("%s\t%s\t\t%.1f\t %d\t\t%d\n",car[SearchNumber-1].DeparturePoint,car[SearchNumber-1].TerminalPoint,car[SearchNumber-1].LastTime,car[SearchNumber-1].FixNumber,car[SearchNumber-1].FixedNumber); } void SearchByAddress(void) { int i; char Address[MAX]; printf("请输入终点站的名称:"); scanf("%s",&Address); printf("班次\t发车时间\t起点\t终点\t行车时间(小时)\t额定载量\t已定票人数\n"); for(i=0;i<RecordNumber;i++) { //对比终点站记录,找到并输出 if((strcmp(Address,car[i].TerminalPoint))==0) { printf("%d\t%d:%d\t\t",car[i].SerialNumber,car[i].SetOut.hour,car[i].SetOut.minutes); printf("%s\t%s\t\t%.1f\t %d\t\t%d\n",car[i].DeparturePoint,car[i].TerminalPoint,car[i].LastTime,car[i].FixNumber,car[i].FixedNumber); } } } void TicketManagement(void) { char functionNumber; system("color 1f"); system("mode con: cols=140 lines=130"); printf(" 订票与退票菜单\n"); printf("============================================================================\n"); printf(" 1.订票\t\n"); printf(" 2.退票\t\n"); printf(" 3.返回主菜单.\n"); printf("============================================================================\n"); printf("选择你需要的功能:"); scanf("%*c%c",&functionNumber); switch(functionNumber) { case '1': { TicketOrder(); //getchar(); //getchar(); } break; case '2':TicketDelete();break; case '3': { getchar(); mainmenu(); } break; default: { printf("输入错误,请确保你的输入为1--3.\n"); printf("按任意键回主菜单......\n"); getchar(); getchar(); mainmenu(); } } } void TicketOrder(void) { int i; printf("请输入要订购的车票的班次:"); scanf("%d",&i); if(i<0||i>RecordNumber) { printf("对不起,今天没有这趟车,请明天再来,谢谢使用!\n"); printf("按任意键回主菜单......\n"); getchar();//getchar(); TicketManagement(); } time_t tval; struct tm *now; tval = time(NULL); now = localtime(&tval); if((now->tm_hour==car[i].SetOut.hour&&now->tm_min<car[i].SetOut.minutes)||(now->tm_hour<car[i].SetOut.hour)) { if(car[i].FixedNumber<car[i].FixNumber) { (car[i].FixedNumber)++; printf("你的订票成功,请按时上车,谢谢使用!\n"); } else printf("对不起,今天的这趟车的票已卖完,请明天再来,谢谢合作!\n"); } else printf("对不起,今天的这趟车已发出,请明天再来,谢谢合作!\n"); printf("班次\t发车时间\t起点\t终点\t行车时间(小时)\t额定载量\t已定票人数\n"); printf("%d\t%d:%d\t\t",car[i-1].SerialNumber,car[i-1].SetOut.hour,car[i-1].SetOut.minutes); printf("%s\t%s\t\t%.1f\t %d\t\t%d\n",car[i-1].DeparturePoint,car[i-1].TerminalPoint,car[i-1].LastTime,car[i-1].FixNumber,car[i-1].FixedNumber); //printf("按任意键回主菜单......\n"); //getchar();getchar(); } void TicketDelete(void) { int i; printf("请输入要退购的车票的班次:"); scanf("%d",&i); if(i<0||i>RecordNumber) { printf("对不起,今天没有这趟车,无法完成退票.谢谢使用!\n"); getchar(); //getchar(); TicketManagement(); getchar(); } time_t tval; struct tm *now; tval = time(NULL); now = localtime(&tval); if((now->tm_hour==car[i].SetOut.hour&&now->tm_min<car[i].SetOut.minutes)||(now->tm_hour<car[i].SetOut.hour)) { if(car[i].FixedNumber>=1) { car[i].FixedNumber-=1; printf("退订车票成功,谢谢使用!\n"); } else printf("对不起,今天的这趟车的票尚未卖出,无法完成退票!\n"); } else printf("对不起,今天的这趟车已发出,无法完成退票!\n"); printf("班次\t发车时间\t起点\t终点\t行车时间(小时)\t额定载量\t已定票人数\n"); printf("%d\t%d:%d\t\t",car[i-1].SerialNumber,car[i-1].SetOut.hour,car[i-1].SetOut.minutes); printf("%s\t%s\t\t%.1f\t %d\t\t%d\n",car[i-1].DeparturePoint,car[i-1].TerminalPoint,car[i-1].LastTime,car[i-1].FixNumber,car[i-1].FixedNumber); getchar(); } void exit(void) { printf("\n\n*********************谢谢使用本系统,欢迎下次继续使用***********************\n"); exit(0); } void SaveRecord(ticket x) { FILE *fp; if((fp=fopen("add.txt","a"))==NULL) { printf("打开文件%s失败\n","add.txt"); exit(-1); } fprintf(fp,"\n%d\t%d\t%d\t\t",x.SerialNumber,x.SetOut.hour,x.SetOut.minutes); fprintf(fp,"%s\t%s\t\t%.1f\t %d\t\t%d",x.DeparturePoint,x.TerminalPoint,x.LastTime,x.FixNumber,x.FixedNumber); }
未命名.rar
(9.32 KB)
输入文件为:班次 发车时间 起点站 终点站 行车时间 额定载量 已定票人数
1 8:00 郫县 广汉 2 45 30
2 6:30 郫县 成都 0.5 40 40
3 7:00 郫县 成都 0.5 40 20
4 10:00 郫县 成都 0.5 40 2
结果运行后显示:未命名文件中
谢谢了,本人是新手,也没什么分,希望各位不辞劳苦,帮小弟一把