代码部分: #include HEADER1 \" number -------------------------------BOOK |start city|reach TICKET----------------------------------\\n\" #define HEADER2 \" | city|takeofftime|receivetime|price|ticketnumber|\\n\" #define HEADER3 \" |----------|----------|----------|-----------|-----------|-----|------------|\\n\" #define FORMAT \" |%-10s|%-10s|%-10s|%-10s |%-10s |%5d| %5d |\\n\" #define DATA p->data.num,p->data.startcity,p->data.reachcity,p->data.takeofftime,p->data.receivetime,p->data.price,p->data.ticketnum int saveflag=0 ; /*定义存储火车信息的结构体*/ struct train { char num[10];/*列车号*/ char startcity[10];/*出发城市*/ char reachcity[10];/*目的城市*/ char takeofftime[10];/*发车时间*/ char receivetime[10];/*到达时间*/ int price;/*票价*/ int ticketnum ;/*票数*/ }; /*订票人的信息*/ struct man { char num[10];/*ID*/ char name[10];/*姓名*/ int bookNum ;/*订的票数*/ }; – 1 – /*定义火车信息链表的结点结构*/ typedef struct node { struct train data ; struct node * next ; }Node,*Link ; /*定义订票人链表的结点结构*/ typedef struct Man { struct man data ; struct Man *next ; }book,*bookLink ; /* 初始界面*/ void menu() { puts(\"\\n\\n\"); puts(\"\\|------------------------------------------------------|\"); puts(\"\\| Booking Tickets puts(\"\\|------------------------------------------------------|\"); puts(\"\\| 0:quit the system puts(\"\\| 1:Insert a train information puts(\"\\| 2:Search a train information puts(\"\\| 3:Book a train ticket puts(\"\\| 4:Modify the train information puts(\"\\| 5:Show the train information puts(\"\\| 6:save information to file puts(\"\\|------------------------------------------------------|\"); } /*添加一个火车信息*/ void Traininfo(Link linkhead) { struct node *p,*r,*s ; char num[10]; r = linkhead ; s = linkhead->next ; while(r->next!=NULL) r=r->next ; while(1) { printf(\"please input the number of the train(0-return)\"); – 2 – |\"); |\"); |\"); |\"); |\"); |\"); |\"); |\"); scanf(\"%s\ if(strcmp(num,\"0\")==0) break ; /*判断是否已经存在*/ while(s) { if(strcmp(s->data.num,num)==0) { printf(\"the train '%s'is existing!\\n\ return ; } s = s->next ; } p = (struct node*)malloc(sizeof(struct node)); strcpy(p->data.num,num);/*输入车号*/ printf(\"Input the city where the train will start:\"); scanf(\"%s\输入出发城市*/ printf(\"Input the city where the train will reach:\"); scanf(\"%s\输入到站城市*/ printf(\"Input the time which the train take off:\"); scanf(\"%s\输入出发时间*/ printf(\"Input the time which the train receive:\"); scanf(\"%s\输入到站时间*/ printf(\"Input the price of ticket:\"); scanf(\"%d\输入火车票价*/ printf(\"Input the number of booked tickets:\"); scanf(\"%d\输入预定票数*/ p->next=NULL ; r->next=p ;/*插入到链表中*/ r=p ; saveflag = 1 ; } } /*打印火车票信息*/ void printheader() /*格式化输出表头*/ { printf(HEADER1); printf(HEADER2); printf(HEADER3); } – 3 – void printdata(Node *q) /*格式化输出表中数据*/ { Node* p; p=q; printf(FORMAT,DATA); } /*查询火车信息*/ void searchtrain(Link l) { Node *s[10],*r; int sel,k,i=0 ; char str1[5],str2[10]; if(!l->next) { printf(\"There is not any record !\"); return ; } printf(\"Choose the way:\\n1:according to the number of train;\\n2:according to the city:\\n\"); scanf(\"%d\输入选择的序号*/ if(sel==1) { printf(\"Input the the number of train:\"); scanf(\"%s\ r=l->next; while(r!=NULL) if(strcmp(r->data.num,str1)==0)/*检索是否有与输入的车号相匹配的*/ { s[i]=r; i++; break; } else r=r->next; } else if(sel==2) { printf(\"Input the city you want to go:\"); – 4 – scanf(\"%s\ r=l->next; while(r!=NULL) if(strcmp(r->data.reachcity,str2)==0)/*检索是否有与输入的城市相匹配的火车*/ { s[i]=r; i++; r=r->next; } else r=r->next; } if(i==0) printf(\"can not find!\"); else { printheader(); for(k=0;k/*订票子模块*/ void Bookticket(Link l,bookLink k) { Node *r[10],*p ; char ch[2],tnum[10],str[10],str1[10],str2[10]; book *q,*h ; int i=0,t=0,flag=0,dnum; q=k ; while(q->next!=NULL) q=q->next ; printf(\"Input the city you want to go: \"); scanf(\"%s\输入要到达的城市*/ p=l->next ; while(p!=NULL) { if(strcmp(p->data.reachcity,str)==0) { r[i]=p ;/*将满足条件的记录存到数组r中*/ – 5 – i++; } p=p->next ; } printf(\"\\n\\nthe number of record have %d\\n\ printheader(); for(t=0;tif(i==0) printf(\"\\nSorry!Can't find the train for you!\\n\"); else { printf(\"\\ndo you want to book it? scanf(\"%s\ if(strcmp(ch,\"Y\")==0||strcmp(ch,\"y\")==0)/*判断是否订票*/ { h=(book*)malloc(sizeof(book)); printf(\"Input your name: \"); scanf(\"%s\ strcpy(h->data.name,str1); printf(\"Input your id: \"); scanf(\"%s\ strcpy(h->data.num,str2); printf(\"please input the number of the train:\"); scanf(\"%s\ – 6 – for(t=0;tif(strcmp(r[t]->data.num,tnum)==0) { if(r[t]->data.ticketnum<1)/*判断剩余的供订票的票数是否为0*/ { printf(\"sorry,no ticket!\"); sleep(2); return; } printf(\"remain %d tickets\\n\ break; } if(flag==0) { flag=1; printf(\"input error\"); sleep(2); return; } printf(\"Input your bookNum: \"); scanf(\"%d\ r[t]->data.ticketnum=r[t]->data.ticketnum-dnum;/*定票成功则可供订的票数相应减少*/ h->data.bookNum=dnum ; h->next=NULL ; q->next=h ; q=h ; printf(\"\\nLucky!you have booked a ticket!\"); getch(); saveflag=1 ; } } } /*修改火车信息*/ void Modify(Link l) { Node *p ; char tnum[10],ch ; p=l->next; if(!p) { printf(\"\\nthere isn't record for you to modify!\\n\"); return ; } else { printf(\"\\nDo you want to modify it?(y/n)\\n\"); getchar(); scanf(\"%c\ if(ch=='y'||ch=='Y') { printf(\"\\nInput the number of the train:\"); scanf(\"%s\ while(p!=NULL) if(strcmp(p->data.num,tnum)==0)/*查找与输入的车号相匹配的记录*/ – 7 – break; else p=p->next; if(p) { printf(\"Input new number of train:\"); scanf(\"%s\ printf(\"Input new city the train will start:\"); scanf(\"%s\ printf(\"Input new city the train will reach:\"); scanf(\"%s\ printf(\"Input new time the train take off\"); scanf(\"%s\ printf(\"Input new time the train reach:\"); scanf(\"%s\ printf(\"Input new price of the ticket::\"); scanf(\"%d\ printf(\"Input new number of people who have booked ticket:\"); scanf(\"%d\ printf(\"\\nmodifying record is sucessful!\\n\"); saveflag=1 ; } else printf(\"\can't find the record!\"); } } } void showtrain(Link l)/*自定义函数显示列车信息*/ { Node *p; p=l->next; printheader(); if(l->next==NULL) printf(\"no records!\"); else while(p!=NULL) { } – 8 – printdata(p); p=p->next; } /*保存火车信息*/ void SaveTrainInfo(Link l) { FILE*fp ; Node*p ; int count=0,flag=1 ; fp=fopen(\"f:\\\rain.txt\ if(fp==NULL) { printf(\"the file can't be opened!\"); return ; } p=l->next ; while(p) { if(fwrite(p,sizeof(Node),1,fp)==1) { p=p->next ; count++; } else { flag=0 ; break ; } } if(flag) { printf(\" saved %d train records\\n\ saveflag=0 ; } fclose(fp); } /*保存订票人的信息*/ void SaveBookInfo(bookLink k) { FILE*fp ; book *p ; int count=0,flag=1 ; – 9 – fp=fopen(\"f:\\\\man.txt\ if(fp==NULL) { printf(\"the file can't be opened!\"); return ; } p=k->next ; while(p) { if(fwrite(p,sizeof(book),1,fp)==1) { p=p->next ; count++; } else { flag=0 ; break ; } } if(flag) { printf(\" saved %d booking records\\n\ saveflag=0 ; } fclose(fp); } main() { FILE*fp1,*fp2 ; Node *p,*r ; char ch1,ch2 ; Link l ; bookLink k ; book *t,*h ; int sel ; l=(Node*)malloc(sizeof(Node)); l->next=NULL ; r=l ; – 10 – k=(book*)malloc(sizeof(book)); k->next=NULL ; h=k ; fp1=fopen(\"f:\\\rain.txt\打开存储车票信息的文件*/ if((fp1==NULL)) { printf(\"can't open the file!\"); return 0 ; } while(!feof(fp1)) { p=(Node*)malloc(sizeof(Node)); if(fread(p,sizeof(Node),1,fp1)==1)/*从指定磁盘文件读取记录*/ { p->next=NULL ; r->next=p ;/*构造链表*/ r=p ; } } fclose(fp1); fp2=fopen(\"f:\\\\man.txt\ if((fp2==NULL)) { printf(\"can't open the file!\"); return 0 ; } while(!feof(fp2)) { t=(book*)malloc(sizeof(book)); if(fread(t,sizeof(book),1,fp2)==1) { t->next=NULL ; h->next=t ; h=t ; } } fclose(fp2); – 11 – while(1) { clrscr(); menu(); printf(\"\please choose (0~6): \"); scanf(\"%d\ clrscr(); if(sel==0) { if(saveflag==1)/*当退出时判断信息是否保存*/ { getchar(); printf(\"\\nthe file have been changed!do you want to save it(y/n)?\\n\"); scanf(\"%c\ if(ch1=='y'||ch1=='Y') { SaveBookInfo(k); SaveTrainInfo(l); } } printf(\"\\nThank you!!You are welcome too\\n\"); break ; } switch(sel)/*根据输入的sel值不同选择相应操作*/ { case 1 : Traininfo(l);break ; case 2 : searchtrain(l);break ; case 3 : Bookticket(l,k);break ; case 4 : Modify(l);break ; case 5: showtrain(l);break; case 6 : SaveTrainInfo(l);SaveBookInfo(k);break ; case 0: return 0; – 12 – } printf(\"\\nplease press any key to continue.......\"); getch(); } } 使用说明 运行程序时,首先进入到菜单部分,菜单部分提供了菜单显示和输入功能部分。其运行效果如图19.1所示。在主界面上输入数字0——6,实现相应的功能。 图19.1 火车订票系统主界面 主界面输入“1”,进入添加火车信息界面,如图19.2所示。根据屏幕上给出的提示输入火车的车次,起点,终点,出发时间,到达时间,票价和可以订购的票数。 图19.2 输入效果图 – 13 – 主界面输入“2”,可以查询火车信息,可以选择查询的方法有两种,一种是按照车次查询,一种是按照你想要到达的地方查询,运行效果如图19.3所示。 图19.3 查询效果图 当在主界面输入“3”时,进入订票界面,按照提示输入你想要到达的城市,会自动显示出你终点站为你输入城市的信息,根据提示输入你是否决定订票以及你的个人信息,运行效果如图19.4所示。 图19.4 订票效果图 当在主界面输入“4”时,进入修改界面,根据提示输入你要修改的内容,修改模块的运行效果如图19.5所示。 – 14 – 图19.5 修改效果图 当在主界面输入“5”时,可以显示出所有的火车信息,显示模块效果如图19.6所示。 图19.6 显示效果图 当在主界面输入“6”时,进入到保存模块,将录入的火车信息进行保存,并且将订票人的信息也进行保存,存储在指定的磁盘文件中。运行效果如图19.7所示。 – 15 – 图19.7 保存效果图 – 16 – 因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- efsc.cn 版权所有 赣ICP备2024042792号-1
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务