《C语言程序-火车站售票系统程序》由会员分享,可在线阅读,更多相关《C语言程序-火车站售票系统程序(11页珍藏版)》请在人人文库网上搜索。
1、实用标准火车站售票查询系统:#include #include #include #include int shoudsave=0 ;int count1=0,count2=0,mark=0,mark1=0 ;/*定义存储火车信息的结构体*/struct trainchar num10;/* 列车号 */char city10;/* 目的城市 */char takeoffTime10;/*发车时间 */char receiveTime10;/*到达时间 */int price;/* 票价*/int bookNum ;/* 票数 */;/*订票人的信息*/ struct manchar num1。
2、0;/*ID*/char name10;/* 姓名 */int bookNum ;/* 需求的票数*/;/*定义火车信息链表的结点结构*/ typedef struct nodestruct train data ;struct node * next ;Node,*Link ;/*定义订票人链表的结点结构*/typedef struct peoplestruct man data ;struct people*next ;bookMan,*bookManLink ;/*初始界面*/ void printInterface()文档puts(I*”);puts(“*Welcome to use 。
3、the system of booking tickets);puts(I*”);puts(“*You can choose the operation:*);puts(“*1:Insert a train information*);puts(“*2:Inquire a train information*);puts(“*3:Book a train ticket*);puts(“*4:Update the train information*);puts(“*5:Advice to you about the train*);puts(*6:save information to fil。
4、e*);puts(*7:quit the system*);puts(*);/*添加一个火车信息*/void In sertTra inin fo(L ink lin khead)struct node *p,*r,*s ;char num10;r = linkhead ;s = linkhead-next ;while(r-next!=NULL)r=r-next ;while(1)printf(please input the number of the train(0-return);scanf(%s,num);if(strcmp(num,0)=0)break ;/*判断是否已经存在*/w。
5、hile(s)if(strcmp(s-data.num,num)=0)printf(the train %shas been born!n,num); return ;s = s-next ;p = (struct node*)malloc(sizeof(struct node);strcpy(p-data.num,num);printf(lnput the city where the train will reach:);scanf(%s,p-data.city);printf(Input the time which the train take off:);scanf(%s,p-dat。
6、a.takeoffTime);printf(Input the time which the train receive:);scanf(%s, &p-data.receiveTime);printf(Input the price of ticket:);scanf(%d,&p-data.price);printf(lnput the number of booked tickets:);scanf(%d,&p-data.bookNum);p-next=NULL ;r-next=p ;r=p ;shoudsave = 1 ;/*打印火车票信息*/void pri ntTra inIn fo(。
7、struct n ode*p)puts(nThe following is the record you want:);printf(number of train: %sn,p-data.num);printf(city the train will reach: %sn,p-data.city);printf(the time the train take off: %snthe time the trainreach: %sn,p-data.takeoffTime,p-data.receiveTime);printf(the price of the ticket:%dn,p-data.。
8、price);printf(the number ofbooked tickets: %dn,p-data.bookNum);struct node * Locate1(Link l,char findmess,char numorcity)Node*r ;if(strcmp(numorcity,num)=0)r=l-next ;while(r)if(strcmp(r-data.num,findmess)=0)return r;r=r-next ;else if(strcmp( numorcity,city)=O)r=l-next ;while(r)if(strcmp(r-data.city,。
9、findmess)=O)return r;r=r-next ;return 0 ;/*查询火车信息*/void QueryTrain(Link I)Node *p ;int sel ;char str15 ,str210;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,&sel);if(sel=1)printf(Input the the numbe。
10、r of train:);scanf(%s,str1);p=Locate1(l,str1,num);if(p)pr in tTra inIn fo(p);elsemark1=1 ;pri ntf(nthe file cant be foun d!);else if(sel=2)prin tf(I nput the city:);sca nf(%s,str2);p=Locate1(l,str2,city);if(p)pr in tTra inIn fo(p);elsemark1=1 ;pri ntf(nthe file cant be foun d!“);/*订票子模块*/void BookTi。
11、cket(Li nk l,bookMa nLink k)Node*r10,*p ;char ch,dem ;bookMan*v,*h ;int i=0,t=0 ;char str10,str110,str210;v=k ;while(v-next!=NULL)v=v-next ;printf(Input the city you want to go:);scanf(%s, &str);p=l-next ;while(p!=NULL)if(strcmp(p-data.city,str)=0)ri=p ;i+;p=p-n ext ;printf(nnthe number of record ha。
12、ve %dn,i);for(t=0;tn); scanf(%d,&ch);if(ch = 1)h=(bookMan*)malloc(sizeof(bookMan); printf(Input your name:);scanf(%s, &str1);strcpy(h-data.name,str1);printf(Input your id:); scanf(%s, &str2); strcpy(h-data.num,str2); printf(lnput your bookNum:); scanf(%d,&dem); h-data.bookNum=dem ; h-next=NULL ;v-ne。
13、xt=h ;v=h ;printf(nLucky!you have booked a ticket!); getch();shoudsave=1 ;bookMa n*Locate2(bookMa nLink k,char fin dmess)bookMan*r ;r=k-next ;while(r)if(strcmp(r-data.num,findmess)=0)mark=1 ;return r;r=r-next ;return 0 ;/*修改火车信息*/void UpdateInfo(Link l)Node*p ;char findmess20,ch ;if(!l-next)printf(n。
14、there isnt record for you to modify!n); return ;elseQueryTrain(l);if(mark1=0)printf(nDo you want to modify it?n);getchar();scanf(%c, &ch);if(ch=y);printf(nInput the number of the train:);scanf(%s,findmess);p=Locate1(l,findmess,num);if(p)printf(Input new number of train:);scanf(%s, &p-data.num);print。
15、f(Input new city the train will reach:);scanf(%s, &p-data.city);printf(Input new time the train take off);scanf(%s, &p-data.takeoffTime);printf(Input new time the train reach:);scanf(%s, &p-data.receiveTime);printf(Input new price of the ticket:);scanf(%d,&p-data.price);printf(Input new number of pe。
16、ople who have booked ticket:);scanf(%d,&p-data.bookNum);printf(nmodifying record is sucessful!n);shoudsave=1 ;else printf(tttcant find the record!);elsemark1=0 ;void AdvicedTrains(Link l)Node*r ;char str1O;int mar=0 ;r=l-next ;printf(Iuput the city you want to go:);scanf(%s,str);while(r)if(strcmp(r-。
17、data.city,str)=0&r-data.bookNumnext ;if(mar=0)printf(ntttyou cant book any ticket now!n);/*保存火车信息*/void SaveTrainlnfo(Link l)FILE*fp ;Node*p ;int count=0,flag=1 ;fp=fopen(c:train.txt,wb);if(fp=NULL)printf(the file cant be opened!);return ;p=l-next ;while(p)if(fwrite(p,sizeof(Node),1,fp)=1)p=p-next ;。
18、count+;elseflag=0 ;break ;if(flag)prin tf(the nu mber of the record which have bee n saved is %dn ,co unt); shoudsave=0 ;fclose(fp);/*保存订票人的信息*/void SaveBookmanlnfo(bookManLink k)FILE*fp ;bookMan*p ;int count=0,flag=1 ;fp=fopen(c:man.txt,wb);if(fp=NULL)printf(the file cant be opened!);return ;p=k-ne。
19、xt ;while(p)if(fwrite(p,sizeof(bookMan),1,fp)=1)p=p-next ;count+;elseflag=0 ;break ;if(flag)printf(the number of the record which have been saved is %dn,count); shoudsave=0 ;fclose(fp);int main()FILE*fp1,*fp2 ;Node*p,*r ;char ch1,ch2 ;Link l ;bookManLink k ;bookMan*t,*h ;int sei ;l=(Node*)malloc(siz。
20、eof(Node);l-next=NULL ;r=l ;k=(bookMan*)malloc(sizeof(bookMan);k-next=NULL ;h=k ;fp仁 fopen(c:train.txt,ab+);if(fp1=NULL)prin tf(ca nt ope n 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 ;count1+;fclose(fp1);fp2=fope n(c:。
21、ma n. txt,ab+);if(fp2=NULL)prin tf(ca nt ope n the file!);return 0 ;while(!feof(fp2)t=(bookMan*)malloc(sizeof(bookMan); if(fread(t,sizeof(bookMan),1,fp2)=1)t-next=NULL ;h-next=t ;h=t ;count2+;fclose(fp2);while(1)clrscr();printlnterface();printf(please choose the operation:);scanf(%d,&sel);clrscr();i。
22、f(sel=8)if(shoudsave=1)getchar();printf(nthe file have been changed!do you want to save it(y/n)?n);scanf(%c,& ch1);if(ch1=y|ch1=Y)SaveBookmanlnfo(k);SaveTrainlnfo(l);printf(nThank you!You are welcome toon);break ;switch(sel)case 1 :InsertTraininfo(l);break ;case 2 :QueryTrain(l);break ;case 3 :BookTicket(l,k);break ;case 4 :Updatelnfo(l);break ;case 5 :AdvicedTrains(l);break ;case 6 :SaveTrainlnfo(l);SaveBookmanlnfo(k);break ;case 7 :return 0;printf(nplease press any key to continue);getch();return 0。