c語言定訂票系統(tǒng)_第1頁
c語言定訂票系統(tǒng)_第2頁
c語言定訂票系統(tǒng)_第3頁
c語言定訂票系統(tǒng)_第4頁
c語言定訂票系統(tǒng)_第5頁
已閱讀5頁,還剩9頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

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;/*定義存儲(chǔ)火車信息的結(jié)構(gòu)體*/structtrain{charnum[10];/*列車號(hào)*/charstartcity[10];/*出發(fā)城市*/charreachcity[10];/*目的城市*/chartakeofftime[10];/*發(fā)車時(shí)間*/charreceivetime[10];/*到達(dá)時(shí)間*/intprice;/*票價(jià)*/intticketnum;/*票數(shù)*/};/*訂票人的信息*/structman{charnum[10];/*ID*/charname[10];/*姓名*/intbookNum;/*訂的票數(shù)*//*定義火車信息鏈表的結(jié)點(diǎn)結(jié)構(gòu)*/typedefstructnode{structtraindata;structnode*next;}Node,*Link;/*定義訂票人鏈表的結(jié)點(diǎn)結(jié)構(gòu)*/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");|"););}/*添加一個(gè)火車信息*/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;/*判斷是否已經(jīng)存在*/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);/*輸入車號(hào)*/printf("Inputthecitywherethetrainwillstart:");scanf("%s",p->data.startcity);/*輸入出發(fā)城市*/printf("Inputthecitywherethetrainwillreach:");scanf("%s",p->data.reachcity);/*輸入到站城市*/printf("Inputthetimewhichthetraintakeoff:");scanf("%s",p->data.takeofftime);/*輸入出發(fā)時(shí)間*/printf("Inputthetimewhichthetrainreceive:");scanf("%s",&p->data.receivetime);/*輸入到站時(shí)間*/printf("Inputthepriceofticket:");scanf("%d",&p->data.price);/*輸入火車票價(jià)*/printf("Inputthenumberofbookedtickets:");scanf("%d",&p->data.ticketnum);/*輸入預(yù)定票數(shù)*/p->next=NULL;r->next=p;/*插入到鏈表中*/r=p;saveflag=1;}}/*打印火車票信息*/voidprintheader()/*格式化輸出表頭*/{printf(HEADER1);printf(HEADER2);printf(HEADER3);}voidprintdata(Node*q)/*格式化輸出表中數(shù)據(jù)*/{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);/*輸入選擇的序號(hào)*/if(sel==1){printf("Inputthethenumberoftrain:");scanf("%s",str1);r=l->next;while(r!=NULL)if(strcmp(r->data.num,strl)==O)/*檢索是否有與輸入的車號(hà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);/*輸入要到達(dá)的城市*/p=l->next;while(p!=NULL){if(strcmp(p->data.reachcity,str)==0)r[i]=p;/*將滿足條件的記錄存到數(shù)組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)/*判斷剩余的供訂票的票數(shù)是否為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;/*定票成功則可供訂的票數(shù)相應(yīng)減少*/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)/*查找與輸入的車號(hà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)/*自定義函數(shù)顯示列車信息*/{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+");/*打開存儲(chǔ)車票信息的文件*/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;/*構(gòu)造鏈表*/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)/*當(dāng)退出時(shí)判斷信息是否保存*/{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)/*根據(jù)輸入的sel值不同選擇相應(yīng)操作*

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論