C語言實現(xiàn)火車訂票系統(tǒng)_第1頁
C語言實現(xiàn)火車訂票系統(tǒng)_第2頁
C語言實現(xiàn)火車訂票系統(tǒng)_第3頁
C語言實現(xiàn)火車訂票系統(tǒng)_第4頁
C語言實現(xiàn)火車訂票系統(tǒng)_第5頁
已閱讀5頁,還剩13頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

第C語言實現(xiàn)火車訂票系統(tǒng)本文實例為大家分享了C語言實現(xiàn)火車訂票系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下

1.運(yùn)行程序時,首先進(jìn)入到菜單部分,菜單部分提供了菜單顯示和輸入功能部分。其運(yùn)行效果如圖所示。在主界面上輸入數(shù)字06,實現(xiàn)相應(yīng)的功能。

2.主界面輸入1,進(jìn)入添加火車信息界面,如圖所示。根據(jù)屏幕上給出的提示輸入火車的車次,起點(diǎn),終點(diǎn),出發(fā)時間,到達(dá)時間,票價和可以訂購的票數(shù)。

3.主界面輸入2,可以查詢火車信息,可以選擇查詢的方法有兩種,一種是按照車次查詢,一種是按照你想要到達(dá)的地方查詢,運(yùn)行效果如圖所示。

4.當(dāng)在主界面輸入3時,進(jìn)入訂票界面,按照提示輸入你想要到達(dá)的城市,會自動顯示出你終點(diǎn)站為你輸入城市的信息,根據(jù)提示輸入你是否決定訂票以及你的個人信息,運(yùn)行效果如圖所示。

5.當(dāng)在主界面輸入4時,進(jìn)入修改界面,根據(jù)提示輸入你要修改的內(nèi)容,修改模塊的運(yùn)行效果如圖所示。

6.當(dāng)在主界面輸入5時,可以顯示出所有的火車信息,顯示模塊效果如圖所示。

7.當(dāng)在主界面輸入6時,進(jìn)入到保存模塊,將錄入的火車信息進(jìn)行保存,并且將訂票人的信息也進(jìn)行保存,存儲在指定的磁盤文件中。運(yùn)行效果如圖所示。

#includeconio.h

#includestdio.h

#includestdlib.h

#includestring.h

#includedos.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.ticketnum

intsaveflag=0;

/*定義存儲火車信息的結(jié)構(gòu)體*/

structtrain

charnum[10];/*列車號*/

charstartcity[10];/*出發(fā)城市*/

charreachcity[10];/*目的城市*/

chartakeofftime[10];/*發(fā)車時間*/

charreceivetime[10];/*到達(dá)時間*/

int

price;/*票價*/

int

ticketnum;/*票數(shù)*/

/*訂票人的信息*/

structman

charnum[10];/*ID*/

charname[10];/*姓名*/

int

bookNum;/*訂的票數(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("\n\n");

puts("\t\t|------------------------------------------------------|");

puts("\t\t|

BookingTickets

|");

puts("\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|------------------------------------------------------|");

/*添加一個火車信息*/

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);/*輸入車號*/

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);/*輸入預(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;

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,str1)==0)/*檢索是否有與輸入的車號相匹配的*/

{

s[i]=r;

i++;

break;

}

else

r=r-next;

}

elseif(sel==2)

{

printf("Inputthecity

youwanttogo:");

scanf("%s",str2);

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("cannotfind!");

else

{

printheader();

for(k=0;kk++)

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中*/

i++;

}

p=p-next;

}

printf("\n\nthenumberofrecordhave%d\n",i);

printheader();

for(t=0;tt++)

printdata(r[t]);

if(i==0)

printf("\nSorry!Can'tfindthetrainforyou!\n");

else

{

printf("\ndoyouwanttobookity/n\n");

scanf("%s",ch);

if(strcmp(ch,"Y")==0||strcmp(ch,"y")==0)/*判斷是否訂票*/

{

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;tt++)

if(strcmp(r[t]-data.num,tnum)==0)

{

if(r[t]-data.ticketnum1)/*判斷剩余的供訂票的票數(shù)是否為0*/

{

printf("sorry,noticket!");

sleep(2);

return;

}

printf("remain%dtickets\n",r[t]-data.ticketnum);

flag=1;

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)==0)/*查找與輸入的車號相匹配的記錄*/

break;

else

p=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;

}

else

printf("\tcan'tfindtherecord!");

}

}

voidshowtrain(Linkl)/*自定義函數(shù)顯示列車信息*/

Node*p;

p=l-next;

printheader();

if(l-next==NULL)

printf("norecords!");

while(p!=NULL)

printdata(p);

p=p-next;

/*保存火車信息*/

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;

fp1=fopen("f:\\train.txt","ab+");/*打開存儲車票信息的文件*/

if((fp1==NULL))

{

printf("can'topenthefile!");

return0;

}

while(!feof(fp1))

{

p=(Node*)malloc(sizeof(Node));

if(fread(p,sizeof(Node),1,fp1)==1)/*從指定磁盤文件讀取記錄*/

{

p-next=NULL;

r-next=p;/*構(gòu)造鏈表*/

r=p;

}

}

fclose(fp1);

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),1,fp2)==1)

{

t-next=NULL;

h-next=t;

h=t;

溫馨提示

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

最新文檔

評論

0/150

提交評論