C++實(shí)現(xiàn)機(jī)票預(yù)訂系統(tǒng)
C++編寫一個(gè)簡(jiǎn)單的機(jī)票預(yù)訂系統(tǒng)。該程序顯示一個(gè)帶有下列選項(xiàng)的菜單:預(yù)訂機(jī)票、取消預(yù)訂、查看某人是否預(yù)定了機(jī)票,以及顯示預(yù)訂乘客。這些信息保存在一個(gè)按照字母排列的名字鏈表中。在程序的簡(jiǎn)化版中,假設(shè)只為一趟航班預(yù)訂機(jī)票。在完全版中不再限制航班的數(shù)目。創(chuàng)建一個(gè)航班鏈表,其中每個(gè)節(jié)點(diǎn)都指向乘客鏈表的指針。
/*******************list.h**********************/ #include <iostream> #include <malloc.h> #include <string.h> using namespace std; typedef struct TK{ char Name[20]; int SeatId; struct TK *next; }Ticket; typedef struct FLY { char FlightId[10]; int Seat[50]; Ticket *PersonHead; struct FLY *next; }Flight; class Person{ public: void ListInitiate(Flight **head); bool Check(int *Seat,int Ch); void Insert(Flight *head); int Delete(Flight *head); void show(Flight *head); void Search(Flight *head); void AddFlght(Flight *head); void DeleteFlght(Flight *head); }; /*******************************main.cpp**********************/ #include "list.h" int menu() { int option; cout<<endl<<endl; cout<<"主菜單"<<endl; cout<<" 1.Booking the ticket of flighting"<<endl; cout<<" 2.Cancel the flighting"<<endl; cout<<" 3.Display the information "<<endl; cout<<" 4.Search"<<endl; cout<<" 5.Add a Flight"<<endl; cout<<" 6.Delete a Flight"<<endl; cout<<" 0.Exit"<<endl<<endl; cout<<"Please input your option:"; cin>>option; getchar(); cout<<endl; if(option>=0&&option<=6) return option; else return -1; } int main() { cout<<"------------------------->航班管理系統(tǒng)<<<<-----------------------------"<<endl; cout<<" 歡迎你使用該航班系統(tǒng)"<<endl; Flight *head; Person P; P.ListInitiate(&head); while(true) { switch(menu( )) { case 1:P.Insert(head);break; //預(yù)訂 case 2:P.Delete(head);break; //取消 case 3:P.show(head);break; //顯示 case 4:P.Search(head);break; //查詢 case 5:P.AddFlght(head);break; //添加航班 case 6:P.DeleteFlght(head);break; //刪除航班 case 0:exit(0); default:cout<<"Choice error!\n"; } } return 0; } /*****************************************passenger.cpp*************************/ #include "List.h" void Person::ListInitiate(Flight **head) { int count=0; *head = (Flight *)malloc(sizeof(Flight)); (*head)->PersonHead=(Ticket *)malloc(sizeof(Ticket)); (*head)->PersonHead->next=NULL; (*head)->next=NULL; for(int i=0;i<50;i++) { (*head)->Seat[i]=0; } } bool Person::Check(int *Seat,int Ch) { int i; for(int i=0;i<50;i++) { if(Ch==i&&Seat[i]!=1)return 1; } return 0; } Flight* Index(Flight *head,char *Id) { Flight *p=head->next; while(p) { if(strcmp(p->FlightId,Id)==0) { return p; } p=p->next; } return NULL; } /*******************************預(yù)定******************************/ void Person::Insert(Flight *head) { int count=0; int Ch; Flight *s=head; if(s->next==NULL) { cout<<"暫無航班!"<<endl; return ; } cout<<"航班列表:"<<endl; s=s->next; while(s!=NULL) { puts(s->FlightId); count++; if(count%5==0) cout<<"\n"; s=s->next; } count=0; char FID[10]; cout<<"輸入航班ID:"; gets(FID); s=Index(head,FID); if(s==NULL) { cout<<"輸入ID有誤"<<endl; return; } cout<<endl; cout<<"有以下座位可供選擇:"<<endl; for(int i=0;i<50;i++) { if(s->Seat[i]!=1) { cout<<i<<"號(hào)"<<"\t"; count++; if(count%5==0) cout<<"\n"; } } cout<<endl; cout<<"輸入座位號(hào):\n"; cin>>Ch; getchar(); if(!Check(head->Seat,Ch)) { cout<<"This Seat have been booked or it is non-existent"; return ; } s->Seat[Ch]=1; char name[20]; cout<<endl; cout<<"Input your Name:"; gets(name); Ticket *p=s->PersonHead,*q; while(p->next!=NULL) { if(strcmp(p->next->Name,name)>0) break; p=p->next; } q=(Ticket *)malloc(sizeof(Ticket)); q->next=p->next; p->next=q; strcpy(q->Name,name); q->SeatId=Ch; } /*******************************取消預(yù)定******************************/ int Person::Delete(Flight *head) { char name[20],FID[10]; cout<<"Input your Name:"; gets(name); getchar(); Flight *s; cout<<"Input the Flight ID:"; gets(FID); s=Index(head,FID); if(s==NULL) { cout<<"輸入ID有誤"<<endl; return 0; } Ticket *p=s->PersonHead->next,*pre=s->PersonHead; int flag=0; while(p!=NULL) { if(strcmp(p->Name,name)==0){ flag=1; break; } pre=p; p=p->next; } if(flag==1){ pre->next=p->next; s->Seat[p->SeatId]=0; free(p); cout<<"你的機(jī)票已經(jīng)取消成功"; } else { cout<<"您還沒訂票\n"; return 0; } return 1; } /*******************************顯示信息******************************/ void Person::show(Flight *head) { Flight *s; char FID[10]; cout<<"Input The Flight ID:"; gets(FID); s=Index(head,FID); if(s==NULL) { cout<<"輸入ID有誤"<<endl; return; } Ticket *p=s->PersonHead->next; if(p==NULL) { cout<<"還沒乘客訂票"<<endl; return; } while(p!=NULL) { cout<<"乘客: "<<p->Name<<" 座位號(hào):" <<p->SeatId; p=p->next; } } /*******************************查詢相關(guān)信息******************************/ void Person::Search(Flight *head) { char name[20]; cout<<"Input Your Name:"; gets(name); Flight *s; char FID[10]; cout<<"Input The Flight ID:"; gets(FID); s=Index(head,FID); if(s==NULL) { cout<<"輸入ID有誤"<<endl; return; } Ticket *p=s->PersonHead->next; int flag=0; while(p!=NULL) { if(strcmp(p->Name,name)==0){ flag=1; break; } p=p->next; } if(flag==1){ cout<<name<<" 已訂機(jī)票"<<endl; } else { cout<<name<<" 未訂機(jī)票"<<endl; } } /*******************************增加航班**********************************/ void Person::AddFlght(Flight *head) { char FlightID[10]; Flight *p=head,*q; cout<<" 輸入航班ID:"; gets(FlightID); while(p->next) { p=p->next; } ListInitiate(&q); p->next=q; strcpy(q->FlightId,FlightID); cout<<"——航班已添加成功!"; } /**********************************刪除航班*******************************************/ void Person::DeleteFlght(Flight *head) { char FlightID[10]; int flag=0; Flight *p=head->next,*q=head; int count=0; Flight *s=head; if(s->next==NULL) { cout<<" 暫無航班!"<<endl; return ; } cout<<" 航班列表:"<<endl; s=s->next; while(s!=NULL) { cout<<s->FlightId<<endl; count++; if(count%5==0) cout<<"\n"; s=s->next; } cout<<" 輸入航班ID:"; gets(FlightID); while(p) { if(strcmp(p->FlightId,FlightID)==0) { flag=1;break; } q=p; p=p->next; } if(flag==0) { cout<<" 該航班ID不存在!"; return ; } q->next=q->next->next; free(p); cout<<" 航班已刪除!\n"; }
效果如下:
更多學(xué)習(xí)資料請(qǐng)關(guān)注專題《管理系統(tǒng)開發(fā)》。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
欄 目:C語言
本文標(biāo)題:C++實(shí)現(xiàn)機(jī)票預(yù)訂系統(tǒng)
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/480.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)-用棧實(shí)現(xiàn)表達(dá)式求值的方法詳解
- 01-10使用OpenGL實(shí)現(xiàn)3D立體顯示的程序代碼
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10求斐波那契(Fibonacci)數(shù)列通項(xiàng)的七種實(shí)現(xiàn)方法
- 01-10C語言 解決不用+、-、&#215;、&#247;數(shù)字運(yùn)算符做加法
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實(shí)現(xiàn)DBSCAN聚類算法
- 01-10深入全排列算法及其實(shí)現(xiàn)方法


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 04-02jquery與jsp,用jquery
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10delphi制作wav文件的方法