您好,登錄后才能下訂單哦!
本篇內容介紹了“怎么用C語言實現火車售票系統”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
主要的功能是:設置車次,刪除車次, 買票, 改簽, 退票, 查詢等。
#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;//火車編號 char Train_end[10];//終點站 int Train_price;//票價 int leftnum;//余票數量}Train_Ticket;typedef struct person{ int mark;//標記 int snum;//身份號碼 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("請按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("-------------車次設置-------------\n"); printf("請輸入你想設置的車次數量: "); scanf("%d",&n); for(i=d;i<d+n;i++){ printf("請輸入車次編號:"); scanf("%d",&L->data[i].Train_num); printf("請輸入車次終點站:"); scanf("%s",&L->data[i].Train_end); printf("請輸入設置的票價:"); scanf("%d",&L->data[i].Train_price); printf("請輸入余票的數量:"); scanf("%d",&L->data[i].leftnum); L->len++; if((L->len)>1&&i<(L->len-1)) { printf("請輸入下班車次信息\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("------------買票服務------------\n"); printf("請輸入你想購置車票數:\n"); scanf("%d",&a); if(a>1){ printf("請分別輸入%d名乘客信息\n",a); } else if(a<=1) { printf("請輸入該名乘客信息\n"); } for(i=0;i<a;i++) { printf("請先輸入你的姓名:"); scanf("%s",&pa1[i].name); printf("請選擇性別<男>F<女>M:"); scanf("%s",&pa1[i].sex); do{ if(strcmp(pa1[i].sex,"F")!=0 && strcmp(pa1[i].sex,"M")!=0){ printf("性別輸入錯誤!請重新輸入!\n"); scanf("%s",&pa1[i].sex); } }while(strcmp(pa1[i].sex,"F")!=0 && strcmp(pa1[i].sex,"M")!=0); printf("請先輸入你的身份證號碼:"); 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;//標記點位 printf("確定選擇購買 %d 車次的列車嗎(yes or no)\n",L->data[i].Train_num); scanf("%s",&answer); do { if(strcmp(answer,"yes")!=0){ printf("你的輸入有誤,請重新輸入!\n"); scanf("%s",&answer); } }while(strcmp(answer,"yes")!=0); if(strcmp(answer,"yes")==0){ printf("你已支付該車票的價格%d元\n",L->data[i].Train_price); L->data[i].leftnum--; }else if(strcmp(answer,"no")==0){ printf("請重新選擇你要購買的車次編號\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--------------按照車次編號刪除車次-------------\n"); printf("請輸入要刪除的車次編號\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("-------------------查詢服務-------------\n"); printf("----------------------------------------\n"); printf("1.按乘客的名稱查詢 2.按乘客證件號查詢\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("請輸入該乘客的姓名:\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("始發站:Yantai 終點站:%s\n",L->data[b].Train_end); printf("第%d號列車 票價: %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("請輸入你的證件號:\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("始發站:Yantai 終點站:%s\n",L->data[b].Train_end); printf("第%d號列車 票價: %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("--你的輸入有誤,請重新輸入!--\n"); printf("------------------------------\n"); Select(q,fp); break; } if(true==0) { printf("沒有該乘客的信息!\n"); }fclose(fp); //getchar();}void Exchange(SeqList*L,FILE *fp){ printf("------------改簽服務-----------\n"); printf("請輸入你的姓名:\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("請重新輸入你想去的目的地的車次編號:"); scanf("%d",&m); for(j=0;j<L->len;j++) { if(m == L->data[j].Train_num) { L->data[j].leftnum--; printf("-------------------------\n"); printf("---你的改簽服務已完成!--\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("----------退票服務-------\n"); char na[10]; printf("請輸入你的姓名:\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("---你的退票服務已完成!--\n"); printf("-------------------------\n"); }void Inquiry(SeqList*L){ int i; if(L->len==0) { printf("No ticket left!\n"); } else { printf("----------------------------Tickets information----------------------\n"); printf("火車車次編號 車 次 路 線 車票售價 剩余票數 "); 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語言實現火車售票系統”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。