您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“怎么用C語言實(shí)現(xiàn)火車售票系統(tǒng)”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
主要的功能是:設(shè)置車次,刪除車次, 買票, 改簽, 退票, 查詢等。
#include<stdio.h>#include<stdlib.h>#include<malloc.h>#include<string.h>#include<conio.h>#define MAXNUM 10000#define Num 100typedef struct train{ int Train_num;//火車編號(hào) char Train_end[10];//終點(diǎn)站 int Train_price;//票價(jià) int leftnum;//余票數(shù)量}Train_Ticket;typedef struct person{ int mark;//標(biāo)記 int snum;//身份號(hào)碼 char sex[10];//買票人性別 //char Train_start[8]; char name[10];//買票人姓名}Person;typedef struct node{ Train_Ticket data[MAXNUM]; int len;}SeqList;Person pa1[Num],pa2[Num];void init_Seqlist(SeqList*);void Sale(SeqList*,FILE*);void Withdraw(FILE*);void Inquiry(SeqList*);void Select(SeqList*,FILE*);void Delete(SeqList*);void Init(SeqList*,struct train t);void Input(SeqList*);void Exchange(SeqList*,FILE *fp);int main(){ SeqList*q; FILE *fp; fp=fopen("train.dat","wb"); q=(SeqList*)malloc(sizeof(SeqList)); int sel; Input(q); system("cls"); system("color 3f");//改變顏色 system("cls"); while(1){ printf("-------------------------------------\n"); printf("-----Welcome to YaiTai trainstation--\n"); printf("1-- Set Tickets --");//初始化 printf("\n2-- Book Ticket --");//買票 printf("\n3-- Exchange Ticket --");//改簽 printf("\n4-- Withdraw Ticket --");//退票 printf("\n5-- Delete Ticket --");//刪除車次 printf("\n6-- Select Ticket --");//查詢 printf("\n7-- Inquiry --");// printf("\n8-- Exit --");//退出 printf("\n-------------------------------------"); printf("\nPlease input your choice:"); scanf("%d",&sel); getchar(); switch(sel) { case 1: init_Seqlist(q); break; case 2: if(q->len==0) { printf("------------------\n"); printf(" No tickets!\n"); printf("------------------\n");break; }else{ Sale(q,fp); break; } case 3: Exchange(q,fp); break; case 4: Withdraw(fp);break; case 5: Delete(q); break; case 6: Select(q,fp); break; case 7: Inquiry(q);break; case 8: printf("------------------------------\n"); printf("---Thank you for your use!---\n"); printf("------------------------------\n"); exit(1); default : printf("Enter error!!\n"); } printf("請(qǐng)按ENTER返回主菜單\n"); getchar(); } return 0;}void Input(SeqList*L){ int i; struct train tra[5]={{206,"beijing",200,20}, {301,"chengdu",350,20}, {432,"shangha",400,20}, {783,"qingdao",175,20}, {312,"hangzho",480,20} }; for(i=0;i<5;i++) Init(L,tra[i]);}void Init(SeqList*L,struct train t){ int i=L->len; L->data[i].Train_num=t.Train_num; strcpy(L->data[i].Train_end,t.Train_end); L->data[i].Train_price=t.Train_price; L->data[i].leftnum=t.leftnum; L->len++;}void init_Seqlist(SeqList*L){ int i,d,n; d=L->len; printf("-------------車次設(shè)置-------------\n"); printf("請(qǐng)輸入你想設(shè)置的車次數(shù)量: "); scanf("%d",&n); for(i=d;i<d+n;i++){ printf("請(qǐng)輸入車次編號(hào):"); scanf("%d",&L->data[i].Train_num); printf("請(qǐng)輸入車次終點(diǎn)站:"); scanf("%s",&L->data[i].Train_end); printf("請(qǐng)輸入設(shè)置的票價(jià):"); scanf("%d",&L->data[i].Train_price); printf("請(qǐng)輸入余票的數(shù)量:"); scanf("%d",&L->data[i].leftnum); L->len++; if((L->len)>1&&i<(L->len-1)) { printf("請(qǐng)輸入下班車次信息\n"); } } printf("Initiation finish!\n");}void Sale(SeqList*L,FILE *fp){ int i=0,k,a,j; if((fp=fopen("train.dat","ab+"))==NULL) { printf("Cannot open file!\n"); exit(1); } printf("------------買票服務(wù)------------\n"); printf("請(qǐng)輸入你想購(gòu)置車票數(shù):\n"); scanf("%d",&a); if(a>1){ printf("請(qǐng)分別輸入%d名乘客信息\n",a); } else if(a<=1) { printf("請(qǐng)輸入該名乘客信息\n"); } for(i=0;i<a;i++) { printf("請(qǐng)先輸入你的姓名:"); scanf("%s",&pa1[i].name); printf("請(qǐng)選擇性別<男>F<女>M:"); scanf("%s",&pa1[i].sex); do{ if(strcmp(pa1[i].sex,"F")!=0 && strcmp(pa1[i].sex,"M")!=0){ printf("性別輸入錯(cuò)誤!請(qǐng)重新輸入!\n"); scanf("%s",&pa1[i].sex); } }while(strcmp(pa1[i].sex,"F")!=0 && strcmp(pa1[i].sex,"M")!=0); printf("請(qǐng)先輸入你的身份證號(hào)碼:"); scanf("%d",&pa1[i].snum); } Inquiry(L); for(j=0;j<a;j++){ printf("\nPlease input the Train_number you want to book(1~%d):"); scanf("%d",&k); char answer[10]; for(i=0;i<L->len;i++){ if(k==L->data[i].Train_num){ pa1[j].mark=i;//標(biāo)記點(diǎn)位 printf("確定選擇購(gòu)買 %d 車次的列車嗎(yes or no)\n",L->data[i].Train_num); scanf("%s",&answer); do { if(strcmp(answer,"yes")!=0){ printf("你的輸入有誤,請(qǐng)重新輸入!\n"); scanf("%s",&answer); } }while(strcmp(answer,"yes")!=0); if(strcmp(answer,"yes")==0){ printf("你已支付該車票的價(jià)格%d元\n",L->data[i].Train_price); L->data[i].leftnum--; }else if(strcmp(answer,"no")==0){ printf("請(qǐng)重新選擇你要購(gòu)買的車次編號(hào)\n"); } } } printf("Tickets purchased success!\n"); } for(i=0;i<a;i++) //printf("%d\n",pa1[i].mark); fwrite((void*)pa1,sizeof(Person),a,fp); fclose(fp);}void Delete(SeqList*L){ int n,i,true=0; SeqList*q; q=(SeqList*)malloc(sizeof(SeqList)); printf("\n--------------按照車次編號(hào)刪除車次-------------\n"); printf("請(qǐng)輸入要?jiǎng)h除的車次編號(hào)\n"); scanf("%d",&n); for(i=0;i<L->len;i++){ if(L->data[i].Train_num==n) { true=1; if(i<L->len-1) { L->data[i].Train_num=L->data[i+1].Train_num; L->data[i].Train_price=L->data[i+1].Train_price; L->data[i].leftnum=L->data[i+1].leftnum; strcpy(L->data[i].Train_end,L->data[i+1].Train_end);L->len--; printf("--------------------\n"); printf("--已刪除該車次信息--\n"); printf("--------------------\n"); } if(i==L->len-1) { L->len--; printf("--------------------\n"); printf("--已刪除該車次信息--\n"); printf("--------------------\n"); } } } if(true==0) { printf("No this ticket numbers!\n"); }}void Select(SeqList*L,FILE *fp){ SeqList*q; q=(SeqList*)malloc(sizeof(SeqList)); int i,a,b,true=0; printf("-------------------查詢服務(wù)-------------\n"); printf("----------------------------------------\n"); printf("1.按乘客的名稱查詢 2.按乘客證件號(hào)查詢\n"); printf("---------------------------------------\n"); if((fp=fopen("train.dat","rb+"))==NULL) { printf("Canot open file !\n"); } fseek(fp,0L,SEEK_END); int d=ftell(fp)/sizeof(Person); rewind(fp); fread((void*)&pa2,sizeof(Person),d,fp); scanf("%d",&a); switch(a) { case 1: { char na[20]; printf("請(qǐng)輸入該乘客的姓名:\n"); scanf("%s",&na); for(i=0;i<d;i++) { if(strcmp(pa2[i].name,na)==0){ b=pa2[i].mark; true=1; printf("-------------%s的車次信息-------\n",pa2[i].name); printf("始發(fā)站:Yantai 終點(diǎn)站:%s\n",L->data[b].Train_end); printf("第%d號(hào)列車 票價(jià): %d\n",L->data[b].Train_num,L->data[b].Train_price); printf("乘客姓名:%s\n",pa2[i].name); printf("乘客證件信息:%d\n",pa2[i].snum); printf("---------------------------------\n"); } } } break; case 2: { int w; printf("請(qǐng)輸入你的證件號(hào):\n"); scanf("%d",&w); for(i=0;i<Num;i++) { if(pa1[i].snum==w) { b=pa2[i].mark; printf("-------------%s的車次信息-------\n",pa2[i].name); printf("始發(fā)站:Yantai 終點(diǎn)站:%s\n",L->data[b].Train_end); printf("第%d號(hào)列車 票價(jià): %d\n",L->data[b].Train_num,L->data[b].Train_price); printf("乘客姓名:%s\n",pa2[i].name); printf("乘客證件信息:%d\n",pa2[i].snum); printf("---------------------------------\n"); } } }break; default: printf("------------------------------\n"); printf("--你的輸入有誤,請(qǐng)重新輸入!--\n"); printf("------------------------------\n"); Select(q,fp); break; } if(true==0) { printf("沒有該乘客的信息!\n"); }fclose(fp); //getchar();}void Exchange(SeqList*L,FILE *fp){ printf("------------改簽服務(wù)-----------\n"); printf("請(qǐng)輸入你的姓名:\n"); char ta[10]; if((fp=fopen("train.dat","rb+"))==NULL) { printf("Canot open file !\n"); } Person p[Num]; fseek(fp,0L,SEEK_END); int r=ftell(fp)/sizeof(Person); rewind(fp); fread((void*)&p,sizeof(Person),r,fp); int m,i,d,j,y,h; scanf("%s",&ta); for(i=0;i<r;i++) { if(strcmp(p[i].name,ta)==0){ d=p[i].mark; printf("你目前的目的地是%s站\n",L->data[d].Train_end); L->data[d].leftnum++; } } Inquiry(L); printf("請(qǐng)重新輸入你想去的目的地的車次編號(hào):"); scanf("%d",&m); for(j=0;j<L->len;j++) { if(m == L->data[j].Train_num) { L->data[j].leftnum--; printf("-------------------------\n"); printf("---你的改簽服務(wù)已完成!--\n"); printf("-------------------------\n"); d=j; } } for(i=0;i<r;i++) { if(strcmp(p[i].name,ta)==0){ p[i].mark=d; h=i; } } fseek(fp,h*sizeof(Person),SEEK_SET); fwrite((void*)&p,sizeof(Person),1,fp); fclose(fp);}void Withdraw(FILE *fp){ printf("----------退票服務(wù)-------\n"); char na[10]; printf("請(qǐng)輸入你的姓名:\n"); scanf("%s",&na); Person *pe; int i,j; if((fp=fopen("train.dat","rb"))==NULL) { printf("The file cannot be opened!\n"); } fseek(fp,0L,SEEK_END); int n=ftell(fp)/sizeof(Person); if(n==1) { fclose(fp); fp=fopen("train.dat","wb"); } pe=(Person*)malloc(sizeof(Person)*(n-1)); rewind(fp); for(j=i=0;i<n;i++) { fread((void*)(pe+j),sizeof(Person),1,fp); if((strcmp((pe+j)->name,na))==0) continue; j++; } fclose(fp); fp=fopen("train.dat","wb"); fwrite((void*)pe,sizeof(Person),n-1,fp); printf("-------------------------\n"); printf("---你的退票服務(wù)已完成!--\n"); printf("-------------------------\n"); }void Inquiry(SeqList*L){ int i; if(L->len==0) { printf("No ticket left!\n"); } else { printf("----------------------------Tickets information----------------------\n"); printf("火車車次編號(hào) 車 次 路 線 車票售價(jià) 剩余票數(shù) "); for(i=0;i<L->len;i++){ printf("\n %d Yantai--%s %d %d\n",L->data[i].Train_num,L->data[i].Train_end,L->data[i].Train_price,L->data[i].leftnum); printf("---------------------------------------------------------------------\n"); } }}
“怎么用C語言實(shí)現(xiàn)火車售票系統(tǒng)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。