1、. .火车票务管理系统一、 课题容和要求设计目标:设计一个火车票务管理系统,用C语言实现。对该系统的要求如下:需求描述:1.系统功能(1)火车时刻信息录入。包括车次、日期、起点、终点、开车时间、到达时间、票价。数据存入数据文件hchsk.dat或hchsk.txt中。(2)火车时刻信息查询。按照车次查询按终点查询按起点查询按终点和日期查询 (3)统计 按终点统计每日的车次数 按起点统计每日的车次数2. 系统主界面应有以下功能选项录入火车时刻信息查询火车时刻信息统计火车车次退出注:建议用VC环境开发用户界面。结果形式:提交程序设计报告和可演示的系统软件二、需求分析系统主界面应有以下功能选项1录入
2、火车时刻信息2查询火车时刻信息3统计火车车次4.退出 (1)火车时刻信息录入。包括车次、日期、起点、终点、开车时间、到达时间、票价。数据存入数据文件hchsk.dat或hchsk.txt中。(2)火车时刻信息查询。按照车次查询按终点查询按起点查询按终点和日期查询 (3)统计 按终点统计每日的车次数 按起点统计每日的车次数二、 概要设计void SearchTrainNum();/按照车次查询void SearchToPlace();/按终点查询void SearchFromPlace();/按起点查询void SearchToPlaceAndDate();/按终点和日期查询void Count
3、ToPlace();/按终点统计每日的车次数void CountFromPlace();/按起点统计每日的车次数三、详细设计 程序的源代码与注释 #include#include#includevoid SearchTrainNum();void SearchToPlace();void SearchFromPlace();void SearchToPlaceAndDate();void CountToPlace();void CountFromPlace();void Star();struct TRAIN char trainNum10; char date30;char fromPlac
4、e20;char toPlace20;char inTime30;char outTime30; double price;struct TRAIN train30;int main() int request; char ch=Y;int i=0;double myprice; FILE *fp;repeat: Star(); printf(火车管理系统界面n);printf(1.录入火车时刻信息.n);printf(2.查询火车时刻信息.n);printf(3.统计火车车次.n);printf(4.退出.n); Star();printf(n);printf(请输入:);scanf(%d,
5、&request);switch(request)/利用case分支构造case 1: fp=fopen(.train.rec,wb);/利用文件与流 if(!fp) printf(File can not be openedn); exit(1); doprintf(Enter车次 日期 起点 终点 开车时间 到达时间 票价n );scanf(%s%s%s%s%s%s%lf,traini.trainNum,traini.date,traini.fromPlace ,traini.toPlace,traini.inTime,traini.outTime,&myprice);traini.pri
6、ce=myprice;fwrite(&traini,sizeof(struct TRAIN),1,fp);i+; printf(continue(Y/N)(N to end)?);getchar(); scanf(%c,&ch); while(ch!=N); fclose(fp);printf(返回主界面吗?(Y返回);getchar();scanf(%c,&ch);if(ch=Y)printf(nn);goto repeat;break; case 2: printf(nn); printf(火车时刻信息查询火车车次统计系统);scanf(%s,num);while(!feof(fp1)fr
7、ead(&traini,sizeof(struct TRAIN),1,fp1);if(strcmp(num,traini.trainNum)=0)printf(%-4s%-12s%-8s%-10s%-10s%-8s%.2lf,traini.trainNum,traini.date,traini.fromPlace,traini.toPlace,traini.inTime,traini.outTime,traini.price);printf(n);i+; printf(continue(Y/N)(N to end)?); getchar(); scanf(%c,&ch);while(ch!=N
8、);printf(n);fclose(fp1);void SearchToPlace()FILE *fp2;char num20;char ch;int i=0;fp2=fopen(.train.rec,r+); if(!fp2)printf(File can not be openedn);exit(1); dorewind(fp2);printf(输入你要查询的终点.n);scanf(%s,num);while(!feof(fp2)fread(&traini,sizeof(struct TRAIN),1,fp2);if(strcmp(num,traini.toPlace)=0)printf
9、(%-4s%-12s%-8s%-10s%-10s%-8s%.2lf,traini.trainNum,traini.date,traini.fromPlace,traini.toPlace,traini.inTime,traini.outTime,traini.price);printf(n);i+; printf(continue(Y/N)(N to end)?); getchar(); scanf(%c,&ch);while(ch!=N);printf(n);fclose(fp2);void SearchFromPlace()FILE *fp3;char num20;char ch;int
10、i=0;fp3=fopen(.train.rec,r+); if(!fp3)printf(File can not be openedn);exit(1); dorewind(fp3);printf(输入你要查询的起点.n);scanf(%s,num);while(!feof(fp3)fread(&traini,sizeof(struct TRAIN),1,fp3);if(strcmp(num,traini.fromPlace)=0) printf(%-4s%-12s%-8s%-10s%-10s%-8s%.2lf,traini.trainNum,traini.date,traini.fromP
11、lace,traini.toPlace,traini.inTime,traini.outTime,traini.price); printf(n);i+; printf(continue(Y/N)(N to end)?); getchar(); scanf(%c,&ch);while(ch!=N);printf(n);fclose(fp3);void SearchToPlaceAndDate()FILE *fp4;char num20;char num120;char ch;int i=0;fp4=fopen(.train.rec,r+); if(!fp4)printf(File can no
12、t be openedn);exit(1); dorewind(fp4);printf(输入你要查询的终点与日期.n);scanf(%s,num);scanf(%s,num1);while(!feof(fp4)fread(&traini,sizeof(struct TRAIN),1,fp4);if(strcmp(num,traini.toPlace)=0)&(strcmp(num1,traini.date)=0)printf(%-4s%-12s%-8s%-10s%-10s%-8s%.2lf,traini.trainNum,traini.date,traini.fromPlace,traini.
13、toPlace,traini.inTime,traini.outTime,traini.price); printf(n);i+; printf(continue(Y/N)(N to end)?); getchar(); scanf(%c,&ch);while(ch!=N);printf(n);fclose(fp4);void CountToPlace()FILE *fp5;char num20;char ch;int i=0;int j=0;fp5=fopen(.train.rec,r+); if(!fp5)printf(File can not be openedn);exit(1); d
14、orewind(fp5);printf(输入你(要统计的车次数的)终点.n);scanf(%s,num); j=0;while(!feof(fp5)fread(&traini,sizeof(struct TRAIN),1,fp5);if(strcmp(num,traini.toPlace)=0) j+;i+;printf(当日终点%s的车次数:%d,num,j);printf(n); printf(continue(Y/N)(N to end)?); getchar();scanf(%c,&ch);while(ch!=N);fclose(fp5);void CountFromPlace()FI
15、LE *fp6;char num20;char ch;int i=0; int j=0;fp6=fopen(.train.rec,r+); if(!fp6)printf(File can not be openedn);exit(1); dorewind(fp6);printf(输入你(要统计的车次数的)起点.n);scanf(%s,num); j=0;while(!feof(fp6)fread(&traini,sizeof(struct TRAIN),1,fp6);if(strcmp(num,traini.fromPlace)=0) j+;i+;printf(当日起点%s的车次数:%d,nu
16、m,j);printf(n); printf(continue(Y/N)(N to end)?); getchar();scanf(%c,&ch);while(ch!=N);fclose(fp6);void Star()int i;for(i=0;i20;i+)printf(*);printf(n);四、测试数据及其结果分析如图1.火车时刻信息录入2.火车时刻信息查询。(1)按照车次查询(2)按终点查询(3)按起点查询(4)按终点和日期查询3.统计1按终点统计每日的车次数2按起点统计每日的车次数4.退出。五、调试过程中的问题编译过程中由于MicrosoftVisualStudioC+6.0编译
17、器的原因。直接给构造体double赋值会出现问题。在金教师的指导和帮助下:程序改为如下:struct TRAIN char trainNum10; char date30;char fromPlace20;char toPlace20;char inTime30;char outTime30; double price;int main() int request; char ch=Y;int i=0;double myprice; FILE *fp;scanf(%s%s%s%s%s%s%lf,traini.trainNum,traini.date,traini.fromPlace, traini.toPlace,traini.inTime,traini.outTime,&myprice);traini.price=myprice;. .word.zl.