




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
C語言——訂票系統(tǒng)代碼部分:#include<conio.h>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<dos.h>#defineHEADER1" BOOKTICKET \n"#defineHEADER2"|number|startcity|reachcity|takeofftime|receivetime|price|ticketnumber|\n"#defineHEADER3"| | | | | | | |\n"#defineFORMAT"|%-10s|%-10s|%-10s|%-10s|%-10s|%5d|%5d|\n"#defineDATAp->data.num,p->data.startcity,p->data.reachcity,p->data.takeofftime,p->data.receivetime,p->data.price,p->data.ticketnumintsaveflag=0;/*定義存儲火車信息的結構體*/structtrain{charnum[10];/*列車號*/charstartcity[10];/*出發(fā)城市*/charreachcity[10];/*目的城市*/chartakeofftime[10];/*發(fā)車時間*/charreceivetime[10];/*到達時間*/intprice;/*票價*/intticketnum;/*票數*/};/*訂票人的信息*/structman{charnum[10];/*ID*/charname[10];/*姓名*/intbookNum;/*訂的票數*//*定義火車信息鏈表的結點結構*/typedefstructnode{structtraindata;structnode*next;}Node,*Link;/*定義訂票人鏈表的結點結構*/typedefstructMan{structmandata;structMan*next;}book,*bookLink;/*初始界面*/voidmenu(){puts("\t\t| |");puts("\t\t|BookingTicketsputs("\t\t| |");puts("\t\t|0:quitthesystem|"puts("\t\t|1:Insertatraininformation|");puts("\t\t|2:Searchatraininformation|");puts("\t\t|3:Bookatrainticket|");puts("\t\t|4:Modifythetraininformation|");puts("\t\t|5:Showthetraininformation|");puts("\t\t|6:saveinformationtofile|");puts("\t\t| |");puts("\n\n");|"););}/*添加一個火車信息*/voidTraininfo(Linklinkhead){structnode*p,*r,*s;charnum[10];r=linkhead;s=linkhead->next;while(r->next!=NULL)r=r->next;while(1)printf("pleaseinputthenumberofthetrain(0-return)");scanf("%s",num);if(strcmp(num,"0")==0)break;/*判斷是否已經存在*/while(s){if(strcmp(s->data.num,num)==0){printf("thetrain'%s'isexisting!\n",num);return;}s=s->next;}p=(structnode*)malloc(sizeof(structnode));strcpy(p->data.num,num);/*輸入車號*/printf("Inputthecitywherethetrainwillstart:");scanf("%s",p->data.startcity);/*輸入出發(fā)城市*/printf("Inputthecitywherethetrainwillreach:");scanf("%s",p->data.reachcity);/*輸入到站城市*/printf("Inputthetimewhichthetraintakeoff:");scanf("%s",p->data.takeofftime);/*輸入出發(fā)時間*/printf("Inputthetimewhichthetrainreceive:");scanf("%s",&p->data.receivetime);/*輸入到站時間*/printf("Inputthepriceofticket:");scanf("%d",&p->data.price);/*輸入火車票價*/printf("Inputthenumberofbookedtickets:");scanf("%d",&p->data.ticketnum);/*輸入預定票數*/p->next=NULL;r->next=p;/*插入到鏈表中*/r=p;saveflag=1;}}/*打印火車票信息*/voidprintheader()/*格式化輸出表頭*/{printf(HEADER1);printf(HEADER2);printf(HEADER3);}voidprintdata(Node*q)/*格式化輸出表中數據*/{Node*p;p=q;printf(FORMAT,DATA);}/*查詢火車信息*/voidsearchtrain(Linkl){Node*s[10],*r;intsel,k,i=0;charstr1[5],str2[10];if(!l->next){printf("Thereisnotanyrecord!");return;}printf("Choosetheway:\n1:accordingtothenumberoftrain;\n2:accordingtothecity:\n");scanf("%d",&sel);/*輸入選擇的序號*/if(sel==1){printf("Inputthethenumberoftrain:");scanf("%s",str1);r=l->next;while(r!=NULL)if(strcmp(r->data.num,strl)==O)/*檢索是否有與輸入的車號相匹配的*/{s[i]=r;i++;break;}elser=r->next;}elseif(sel==2)printf("Inputthecityyouwanttogo:");scanf("%s",str2);r=l->next;while(r!=NULL)if(strcmp(r->data.reachcity,str2)==0)/*檢索是否有與輸入的城市相匹配的火車*/{s[i]=r;i++;r=r->next;}elser=r->next;}if(i==0)printf("cannotfind!");else{printheader();for(k=0;k<i;k++)printdata(s[k]);}}/*訂票子模塊*/voidBookticket(Linkl,bookLinkk){Node*r[10],*p;charch[2],tnum[10],str[10],str1[10],str2[10];book*q,*h;inti=0,t=0,flag=0,dnum;q=k;while(q->next!=NULL)q=q->next;printf("Inputthecityyouwanttogo:");scanf("%s",&str);/*輸入要到達的城市*/p=l->next;while(p!=NULL){if(strcmp(p->data.reachcity,str)==0)r[i]=p;/*將滿足條件的記錄存到數組r中*/}p=p->next;}printf("\n\nthenumberofrecordhave%d\n",i);printheader();for(t=0;t<i;t++)printdata(r[t]);if(i==0)printf("\nSorry!Can'tfindthetrainforyou!\n");else{printf("\ndoyouwanttobookit?<y/n>\n");scanf("%s",ch);if(strcmp(ch,"Y")==Ollstrcmp(ch,"y")==O)/*判斷是否訂票*/{h=(book*)malloc(sizeof(book));printf("Inputyourname:");scanf("%s",&str1);strcpy(h->,str1);printf("Inputyourid:");scanf("%s",&str2);strcpy(h->data.num,str2);printf("pleaseinputthenumberofthetrain:");scanf("%s",tnum);for(t=0;t<i;t++)if(strcmp(r[t]->data.num,tnum)==0){if(r[t]->data.ticketnumvl)/*判斷剩余的供訂票的票數是否為0*/{printf("sorry,noticket!");sleep(2);return;}printf("remain%dtickets\n",r[t]->data.ticketnum);flag=l;break;}if(flag==0)printf("inputerror");sleep(2);return;}printf("InputyourbookNum:");scanf("%d",&dnum);r[t]->data.ticketnum=r[t]->data.ticketnum-dnum;/*定票成功則可供訂的票數相應減少*/h->data.bookNum=dnum;h->next=NULL;q->next=h;q=h;printf("\nLucky!youhavebookedaticket!");getch();saveflag=1;}}}/*修改火車信息*/voidModify(Linkl){Node*p;chartnum[10],ch;p=l->next;if(!p){printf("\nthereisn'trecordforyoutomodify!\n");return;}else{printf("\nDoyouwanttomodifyit?(y/n)\n");getchar();scanf("%c",&ch);if(ch=='y'||ch=='Y'){printf("\nInputthenumberofthetrain:");scanf("%s",tnum);while(p!=NULL)if(strcmp(p->data.num,tnum)==O)/*查找與輸入的車號相匹配的記錄*/break;elsep=p->next;if(p){printf("Inputnewnumberoftrain:");scanf("%s",&p->data.num);printf("Inputnewcitythetrainwillstart:");scanf("%s",&p->data.startcity);printf("Inputnewcitythetrainwillreach:");scanf("%s",&p->data.reachcity);printf("Inputnewtimethetraintakeoff");scanf("%s",&p->data.takeofftime);printf("Inputnewtimethetrainreach:");scanf("%s",&p->data.receivetime);printf("Inputnewpriceoftheticket::");scanf("%d",&p->data.price);printf("Inputnewnumberofpeoplewhohavebookedticket:");scanf("%d",&p->data.ticketnum);printf("\nmodifyingrecordissucessful!\n");saveflag=1;}elseprintf("\tcan'tfindtherecord!");}}}voidshowtrain(Link1)/*自定義函數顯示列車信息*/{Node*p;p=1->next;printheader();if(1->next==NULL)printf("norecords!");e1sewhi1e(p!=NULL){printdata(p);p=p->next;}—8—}/*保存火車信息*/voidSaveTrainInfo(Linkl){FILE*fp;Node*p;intcount=0,flag=1;fp=fopen("f:\\train.txt","wb");if(fp==NULL){printf("thefilecan'tbeopened!");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%dtrainrecords\n",count);saveflag=0;}fclose(fp);}/*保存訂票人的信息*/voidSaveBookInfo(bookLinkk){FILE*fp;book*p;intcount=0,flag=1;fp=fopen("f:\\man.txt","wb");if(fp==NULL){printf("thefilecan'tbeopened!");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%dbookingrecords\n",count);saveflag=0;}fclose(fp);}main(){FILE*fp1,*fp2;Node*p,*r;charch1,ch2;Linkl;bookLinkk;book*t,*h;intsel;l=(Node*)malloc(sizeof(Node));l->next=NULL;r=l;k=(book*)malloc(sizeof(book));k->next=NULL;h=k;fpl=fopen("f:\\train.txt","ab+");/*打開存儲車票信息的文件*/if((fp1==NULL)){printf("can'topenthefile!");return0;}while(!feof(fpl)){p=(Node*)malloc(sizeof(Node));if(fread(p,sizeof(Node),l,fpl)==l)/*從指定磁盤文件讀取記錄*/{p->next=NULL;r->next=p;/*構造鏈表*/r=p;}}fclose(fpl);fp2=fopen("f:\\man.txt","ab+");if((fp2==NULL)){printf("can'topenthefile!");return0;}while(!feof(fp2)){t=(book*)malloc(sizeof(book));if(fread(t,sizeof(book),l,fp2)==l){t->next=NULL;h->next=t;h=t;}}fclose(fp2);while(1){clrscr();menu();printf("\tpleasechoose(0~6):");scanf("%d",&sel);clrscr();if(sel==0){if(saveflag==l)/*當退出時判斷信息是否保存*/{getchar();printf("\nthefilehavebeenchanged!doyouwanttosaveit(y/n)?\n");scanf("%c",&chl);if(chl=='y'||chl=='Y'){SaveBookInfo(k);SaveTrainInfo(l);}}printf("\nThankyou!!Youarewelcometoo\n");break;}switch(sel)/*根據輸入的sel值不同選擇相應操作*
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年輕鋼龍骨項目評估報告
- 地熱發(fā)電成套設備生產建設項目節(jié)能評估報告(節(jié)能專)
- 2025年中國速凍米面食品行業(yè)發(fā)展監(jiān)測及投資戰(zhàn)略研究報告
- 稅務師網盤課件2021
- 2025-2030年中國碳化硅砂布卷項目投資可行性研究分析報告
- 2025年中國磁療胃墊行業(yè)市場發(fā)展前景及發(fā)展趨勢與投資戰(zhàn)略研究報告
- 中國浙江省天然氣市場前景預測及未來發(fā)展趨勢報告
- 2025年中國江西省水泥行業(yè)市場調查研究及投資戰(zhàn)略研究報告
- 2025-2030年中國密封止水帶行業(yè)深度研究分析報告
- 在線教育課程合同
- 【完美排版】山東科技出版社二年級下冊綜合實踐活動教案
- 公共政策學(第三版)-課件
- 齊魯醫(yī)學Lisfranc-損傷
- 大型鋼網架整體提升施工工法
- 干熄焦爐內固_氣流動與傳熱數值模擬畢業(yè)論文
- 公司股東變更登記申請書(一變一)
- 供應鏈金融業(yè)務工作開展情況總結匯報
- 基于motor的六相電機繞組分相設置
- 一年級數學學困生轉化典型案例
- 廣西壯族自治區(qū)醫(yī)院大全
- 電子時鐘 Multisim仿真
評論
0/150
提交評論