C++实现机票预订系统

所属分类: 软件编程 / C 语言 阅读数: 98
收藏 0 赞 0 分享

C++编写一个简单的机票预订系统。该程序显示一个带有下列选项的菜单:预订机票、取消预订、查看某人是否预定了机票,以及显示预订乘客。这些信息保存在一个按照字母排列的名字链表中。在程序的简化版中,假设只为一趟航班预订机票。在完全版中不再限制航班的数目。创建一个航班链表,其中每个节点都指向乘客链表的指针。

/*******************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<<"------------------------->航班管理系统<<<<-----------------------------"<<endl;
 cout<<"  欢迎你使用该航班系统"<<endl;
 Flight *head;
 Person P;
 P.ListInitiate(&head);
 while(true)
 {
 switch(menu( ))
 {
 case 1:P.Insert(head);break; //预订
 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;
}
/*******************************预定******************************/
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<<"号"<<"\t";
 count++;
 if(count%5==0)
 cout<<"\n";
 }
 }
 cout<<endl;
 cout<<"输入座位号:\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;
}
 
/*******************************取消预定******************************/
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<<"你的机票已经取消成功";
 }
 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<<" 座位号:" <<p->SeatId;
 p=p->next;
 }
}
 
/*******************************查询相关信息******************************/
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<<" 已订机票"<<endl;
 }
 else 
 {
 cout<<name<<" 未订机票"<<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";
}

效果如下:

更多学习资料请关注专题《管理系统开发》。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

C++中四种对象生存期和作用域以及static的用法总结分析

以下是对C++中四种对象生存期和作用域以及static的用法进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++嵌套类与局部类详细解析

从作用域的角度看,嵌套类被隐藏在外围类之中,该类名只能在外围类中使用。如果在外围类之外的作用域使用该类名时,需要加名字限定
收藏 0 赞 0 分享

C++空类详解

以下是对C++中的空类进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++之友元:友元函数和友元类详解

友元是一种允许非类成员函数访问类的非公有成员的一种机制。可以把一个函数指定为类的友元,也可以把整个类指定为另一个类的友元
收藏 0 赞 0 分享

C++中返回指向函数的指针示例

int (*ff(int)) (int *,int);表示:ff(int)是一个函数,带有一个int型的形参,该函数返回int (*) (int *,int),它是一个指向函数的指针,所指向的函数返回int型并带有两个分别是Int*和int型的形参
收藏 0 赞 0 分享

C数据结构之单链表详细示例分析

以下是对C语言中的单链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C数据结构之双链表详细示例分析

以下是对c语言中的双链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅析如何在c语言中调用Linux脚本

如何在c语言中调用Linux脚本呢?下面小编就为大家详细的介绍一下吧!需要的朋友可以过来参考下
收藏 0 赞 0 分享

深入解析unsigned int 和 int

以下是对unsigned int和int进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅谈C++中的string 类型占几个字节

本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节
收藏 0 赞 0 分享
查看更多