<模拟飞机票订票和退票系统>的c++实现

本文介绍了一个飞机票订票和退票系统的详细设计,包括数据结构的选择、成员函数的实现,以及购票、退票和查询等功能的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述: 

假定某民航有M个航次的班机,每个航次都只到达一个地方。试为该机场售票处设计一个自动订票和退票系统,要求系统具有以下功能:

(1) 订票:若该航次余票大于等于乘客订票数,则在该航次的乘客表中,插入订票乘客的信息项,并修改该航次有关数据,否则给出相应信息。

(2) 退票:若该航次当前退票数小于等于乘客原订票数,则在相应的乘客表中找到该乘客项,修改该航次及乘客表中有关数据;当某乘客由于退票使订票数为零时,则从乘客表中撤消该数据项。

要求:

(1)描述对航次表和乘客表选用的数据结构(顺序表、链表的综合应用);

(2)编程实现飞机票订票和退票系统(查询、更新、删除运算的实现)。


一、逻辑设计

       编写一个模拟飞机票订票和退票系统,使该系统能够实现对航班信息的录入、浏览、查询以及订票、退票的功能。

       根据题目要求,系统要处理的数据有两类:一类是乘客数据;另一类是航班数据。乘客数据是动态的,用链表存储;而航班数据是静态的,用队列存储。

       乘客应包括的数据如下:

             name    乘客姓名

             TicketNum    购票数量

             Passenger * next    下一节点指针

       购买机票又要查询航班数据,航班应包括的数据如下:

             StartTime    航班出发时间

             EndTime    航班到达时间

             MaxContain    最大载客数量

             ReadyContain    当前已售票数量

             PassengerNode * pHead    该航班乘客首指针

       其应包含成员函数如下:

             TypeFlight    构造函数

             SetFlight    修改航班数据

             GetDeparture    取出发时间

             GetArrive    取到达时间

             GetLength    获取航班时长

             RemainTicket    获取余票数量

             BuyTicket    成员函数:购票

             CancelTicket    成员函数:退票

             SearchTicket    成员函数:搜索乘客购票数量

       为了实现出发与到达时间的输入输出及计算,定义了“时间”的抽象数据类型:

             1、在TypeTime内部用整形数定义它的小时和分钟。

             2、实现 + 、 - 运算的成员函数

             3、利用重载的流函数来输出形如“hh:mm”的格式时间

#define MaxNode 100
#define charl 100
#define MaxContainf 100

int FlightAmount;

class TypeTime
{
private:
    int hour;
    int minute;
public:
    TypeTime(int h,int m){hour=h;minute=m;}
    TypeTime(){hour=0;minute=0;}
    int Set(int h,int m)
    {
        if(h>=0&&h<24&&m>=0&&m<60){hour=h;minute=m;return 0;}
        return 1;
    }
    TypeTime & operator + ( TypeTime & t2)
    {
        int flag = 0;
        if (minute + t2.minute >= 60) flag = 1;
        TypeTime *result = new TypeTime ((hour + t2.hour + flag) % 24, (minute + t2.minute) % 60);
        return *result;
    }
    TypeTime & operator - ( TypeTime & t2)
    {
        int flag = 0;
        if (minute - t2.minute <= 0) flag = -1;
        TypeTime *result = new TypeTime ( hour - t2.hour + flag, minute - t2.minute - 60 * flag );
        return *result;
    }
    friend ostream & operator << ( ostream & os, TypeTime & t )
    {
        os<<setfill('0')<<setw(2)<<t.hour<<":"<<setw(2)<<t.minute;
        return os;
    }
};
       乘客数据可用一个结构来描述:

class PassengerNode
{
public:
    char name[charl];
    int TicketNum;
    PassengerNode * pNext;
};
       所有的航班数据进入一个队列,航班数据的描述如下:
       
class TypeFlight
{
private:
  TypeTime StartTime;
  TypeTime EndTime;
  int MaxContain;
  int ReadyContain;
  PassengerNode * pHead;
public:
  TypeFlight()
  {
    pHead=new PassengerNode();
    pHead->pNext=NULL;
  }
  void SetFlight(int h1,int m1,int h2,int m2,int MC)
  {
      StartTime.Set(h1,m1);
      EndTime.Set(h2,m2);
      MaxContain=MC;
      ReadyContain=0;
  }

  TypeTime GetDeparture(){return StartTime;}
  TypeTime GetArrive(){return EndTime;}
  TypeTime GetLength(){return EndTime - StartTime;}
  int RemainTicket(){return MaxContain - ReadyContain;}

  int BuyTicket(char name[],int amount);

  int CancelTicket(char name[],int amount);
  
  int SearchTicket(char name[]);
};
TypeFlight Flight[MaxNode];


二、成员函数设计

       1、BuyTicket
            若购票乘客存在则增加购票数量,否则创建新乘客节点
int TypeFlight :: BuyTicket(char name[],int amount)
  {
     if(amount>this->RemainTicket()){cout<<"there is not enough ticket"<<endl;return 1;}
     PassengerNode* p=new PassengerNode();
     p=pHead;
     while(p->pNext!=NULL)//找到尾节点
     {
        if(strcmp(p->name,name) == 0)
            {
                p->TicketNum += amount;
                this->ReadyContain += amount;
                return 0;
            }
        p=p->pNext;
     }
     PassengerNode* newNode=new PassengerNode();
     newNode->pNext=NULL;
     strcpy(newNode->name,name);
     newNode->TicketNum=amount;
     p=pHead;
     if(pHead==NULL)
     {
       pHead=newNode; //新节点为链表头节点
     }else
     {
        while(p->pNext!=NULL)//找到尾节点
            p=p->pNext;
        p->pNext=newNode; //在尾节点后面加入新节点
        this->ReadyContain += amount;
     }
     return 0;
  }


       2、CancelTicket
int TypeFlight :: CancelTicket(char name[],int amount)
  {
     PassengerNode* p=new PassengerNode();
     PassengerNode* deleteNode=NULL;
     p=pHead;
      while(p->pNext!=NULL && strcmp(p->pNext->name,name) != 0) p=p->pNext;
      if(p->pNext!=NULL && strcmp(p->pNext->name,name) == 0)
        {
            if(amount > p->pNext->TicketNum){cout<<"Excessive refund"<<endl;return 1;}
            if(amount == p->pNext->TicketNum)
            {
                this->ReadyContain -= amount;
                deleteNode=p->pNext;
                p->pNext=p->pNext->pNext;
                delete deleteNode;
                deleteNode=NULL;
                return 0;
            }
            p->pNext->TicketNum -= amount;
            this->ReadyContain -= amount;
            return 0;
            }
     cout<<endl<<"No such person!"<<endl;
     return 1;
  }

       
       3、SearchTicket
int TypeFlight :: SearchTicket(char name[])
  {
      PassengerNode* p=new PassengerNode();
      p=pHead;
      while(p->pNext!=NULL && strcmp(p->name,name) != 0) p=p->pNext;
      if(strcmp(p->name,name) == 0)
      {
          return p->TicketNum;
      }
      return -1;
  }

三、购票、退票、查询界面函数设计

      1、购票
void booking()
{
    system("cls");
    char name[charl];
    int n1,n3,n2;
    TypeTime t1,t2;
    while(1){
    system("cls");
    cout<<"Booking\n"<<endl<<"Please enter your name\n";
    scanf("%s", name);
    cout<<endl<<"standby ticket:"<<endl<<endl<<"No.  DEPT   ARR    remain"<<endl;
    for(int i=0;i<FlightAmount;i++)
    {
        t2=Flight[i].GetArrive();
        t1=Flight[i].GetDeparture();
        cout<<setfill('0')<<i<<".   "<<t1<<"  "<<t2<<"   "<<setw(3)<<Flight[i].RemainTicket()<<endl;
    }
    cout<<endl<<"Please enter your estimated ticket to purchase"<<endl;
    scanf("%d", &n3);
    if(n3>=FlightAmount||n3<0)
    {
        cout<<"no such ticket"<<endl;
        cout<<"exit? 1.YES 0.NO"<<endl;
        scanf("%d", &n1);
        if(n1 == 1) break;
        else continue;
    }
    cout<<"Please enter the quantity you require"<<endl;
    scanf("%d", &n2);
    if(Flight[n3].BuyTicket(name,n2)==0){cout<<endl<<"booking success!"<<endl;}
    else cout<<endl<<"booking fail!"<<endl;
    cout<<endl<<"exit? 1.YES 0.NO"<<endl;
    scanf("%d", &n1);
    if(n1 == 1) break;
    else continue;
    }
}

       2、退票
void refunding()
{
    system("cls");
    char name[charl];
    TypeTime t1,t2;
    int n1=0,n2=-1,index[FlightAmount],a=0,n3=0;
    while(1){
    a=0,n1=0,n2=-1,n3=0;
    system("cls");
    cout<<"Refunding\n"<<endl<<"Please enter your name\n";
    scanf("%s", name);
    cout<<endl<<"Your tickets:"<<endl;
    for(int i=0;i<FlightAmount;i++)
    {
        if(Flight[i].SearchTicket(name)<0) continue;
        index[a++]=i;
        n2=0;
        t2=Flight[i].GetArrive();
        t1=Flight[i].GetDeparture();
        cout<<setfill('0')<<a-1<<".  "<<t1<<" "<<t2<<"  "<<setw(3)<<Flight[i].SearchTicket(name)<<" tickets"<<endl;
    }
    if(n2<0)
    {
        cout<<endl<<"No such person!"<<endl;
        cout<<endl<<"exit? 1.YES 0.NO"<<endl;
        scanf("%d", &n1);
        if(n1 == 1) break;
        else continue;
    }
    cout<<endl<<"Please enter your estimated ticket to refund"<<endl;
    scanf("%d", &n3);
        if(n3>=a||n3<0)
    {
        cout<<"no such ticket"<<endl;
        cout<<"exit? 1.YES 0.NO"<<endl;
        scanf("%d", &n1);
        if(n1 == 1) break;
        else continue;
    }
    n2=0;
    cout<<"Please enter the quantity you require"<<endl;
    scanf("%d", &n2);
    if(Flight[index[n3]].CancelTicket(name,n2)==0){cout<<endl<<"refunding success!"<<endl;}
    else cout<<endl<<"refunding fail!"<<endl;
    cout<<endl<<"exit? 1.YES 0.NO"<<endl;
    scanf("%d", &n1);
    if(n1 == 1) break;
    else continue;

    }

}

      3、查询
void searchp()
{
    system("cls");
    char name[charl];
    TypeTime t1,t2;
    int n1,n2=-1;
    while(1){
    system("cls");
    cout<<"Order Query\n"<<endl<<"Please enter your name\n";
    scanf("%s", name);
    cout<<endl<<"Your tickets:"<<endl;
    for(int i=0;i<FlightAmount;i++)
    {
        if(Flight[i].SearchTicket(name)<0) continue;
        n2=0;
        t2=Flight[i].GetArrive();
        t1=Flight[i].GetDeparture();
        cout<<setfill('0')<<t1<<" "<<t2<<"  "<<setw(3)<<Flight[i].SearchTicket(name)<<" tickets"<<endl;
    }
    if(n2<0) cout<<"No such person"<<endl;
    cout<<endl<<"exit? 1.YES 0.NO"<<endl;
    scanf("%d", &n1);
    if(n1 == 1) break;
    }
}

四、航班的录入

       利用文件输入航班信息来提高程序真实性
void Input()
{
    //初始输入输入
    FILE *fp;
    int i,dtht,dtmt,atht,atmt,MaxContain;
    TypeTime temp1,temp2;
    fp = fopen("plane.txt", "r");
    fscanf(fp,"%d",&FlightAmount);
    printf("%d\n", FlightAmount);
    for(i = 0;i < FlightAmount;i++)
    {
        fscanf(fp, "%d:%d", &dtht,&dtmt);
        fscanf(fp, "%d:%d", &atht,&atmt);
        fscanf(fp,"%d",&MaxContain);
        Flight[i].SetFlight(dtht,dtmt,atht,atmt,MaxContain);
        temp1 = Flight[i].GetDeparture();
        temp2 = Flight[i].GetArrive();
        cout<<temp1<<" "<<temp2<<endl;
        dtht=0;dtmt=0;atht=0;atmt=0;
    }
    fclose(fp);
}
      输入格式如下:
10

07:30 09:45 99
08:25 10:40 233
10:45 13:10 100
11:20 13:40 120
14:35 16:55 325
16:40 19:00 213
18:40 20:55 65
18:50 21:05 22
21:05 23:25 46
21:35 23:50 130

五、主程序设计

void newline(int n){for(int i = 1;i <= n;i++) cout<<endl;}

void space(int n){for(int i = 1;i <= n;i++)cout<<" ";}

int main()
{
    int n, temp, s;
    Input();
    while(1)
    {
        system("cls");
        newline(10);
        space(20);
        cout<<"Welcome to use the computerized reservation system"<<endl;
        space(24);
		printf("0.booking\n");
		space(24);
		printf("1.refunding\n");
		space(24);
        printf("2.Order Query\n");
		space(24);
		printf("3.exit\n");
        scanf("%d", &n);
        if(n == 3) break;
        if(n == 0)
        {
            booking();
        }
        if(n == 1)
        {
            refunding();
        }
        if(n == 2)
        {
            searchp();
        }
    }
    return 0;
}



具体实现细节待补充,多谢阅读。

} void list_menu() { cout<<endl<<""; cout<<endl<<" 菜单"; cout<<endl<<" ************************"; cout<<endl<<" * 0 . 查看排队情况 *"; cout<<endl<<" * 1 . 订票 *"; cout<<endl<<" * 2 . 退票 *"; cout<<endl<<" * 3 . 查看剩余票 *"; cout<<endl<<" * 4 . 查看飞机信息 *"; cout<<endl<<" * 5 . 查看乘客信息 *"; cout<<endl<<" * 6 . 退出 *"; cout<<endl<<" ************************"; void makenull_wait() { wait *tempw; FILE *fp; tempw=new wait; int i; if((fp=fopen("wait.txt","r")) ==NULL ) { fp=fopen("wait.txt","w"); fclose(fp); } wait_end=new wait; wait_head=new wait; wait_end->next=NULL; wait_end->pre=NULL; wait_head=wait_end; wait_head->count=0; fp=fopen("wait.txt","r"); fread(wait_head,sizeof(wait),1,fp); for(i=1;i<=wait_head->count;i++) { fread(tempw,sizeof(wait),1,fp); wait_end->next=tempw; tempw->pre=wait_end; tempw->next=NULL; wait_end=tempw; } } void list_piao() { int i,j; for(i=1;i<=m-1;i++) { if(a[i].seat[0]!=n) { cout<<endl<<"第 "<<i<<" 架飞机剩余的票 :"<<endl; for(j=1;j<=n;j++) if (a[i].seat[j]==0) cout<<" "<<j; cout<<endl; } else cout<<endl<<"The "<<i<<" plane is full !"<<endl<<endl; } } void list_information() { int x; do {cout<<endl<<"显示哪架飞机的信息 ? "; cin>>x;cout<<endl;}while(x<1 || x>=m); cout<<endl<<"第 "<<x<<" 架飞机的信息如下 "<<endl; if(x==1) plane_information(head1); if(x==2) plane_information(head2); if(x==3) plane_information(head3); } void plane_information(node *head) { node *q; char ch; int x=0; if(head!=NULL && head->next!=NULL) q=head->next; else { q=NULL; cout<<"飞机空,无预订票 !"<<endl; } while(q!=NULL) { cout<<endl<<"*******************"<<endl; q->date=q->plane; cout<<"日期 :"<<q->date<<endl; cout<<"座位号 : "<<q->seat<<endl; cout<<"姓名 : "<<q->name; cout<<endl<<"ID 号 : "<<q->id; q=q->next;x++; if (x % 3 ==0) ch=getch(); } cout<<endl; } void book() { int i,j,p; cout<<endl<<"请选择地点:(1、2、3) "; do { cin>>i; if (i<1 || i>=m) { cout<<endl<<"**** 超出范围!****"<<endl<<"请重新输入:"; } else {cout<<endl<<"你要订的是到"<<i<<"地的飞机"<<endl; cout<<endl<<"第 "<<i<<" 架飞机剩余的票 :"<<endl; for(p=1;p<=n;p++) if (a[i].seat[p]==0) cout<<" "<<p; cout<<endl; break;} }while(1); cout<<endl<<"请选择座位号 : "; do { cin>>j; if (j<1 || j>n) { cout<<endl<<"**** 超出范围!****"<<endl<<"请重新输入:"; } else { q->date=i; cout<<endl<<"您的订票日期 : "<<q->date<<endl; break; } }while(1); if (a[i].seat[j]==0) { a[i].seat[j]=1; cout<<endl; a[i].seat[0]++; if(i==1) add_information(head1,1,j); if(i==2) add_information(head2,2,j); if(i==3) add_information(head3,3,j); } else { cout<<endl<<"**** 对不起,该座位已被预订,您被安排到订票等候队列 ****"<<endl; add_wait(i,j); } } void add_wait(int x,int y) { wait *tempw; tempw=new wait; tempw->next=NULL; cout<<"请输入个人信息"<<endl; cout<<endl<<"*************"<<endl; cout<<"姓名 : ";cin>>tempw->name; cout<<"ID号 : ";cin>>tempw->id; cout<<"电话 :";cin>>tempw->phone; tempw->seat=y; tempw->plane=x; wait_end->next=tempw; tempw->pre=wait_end; wait_end=wait_end->next; cout<<endl<<"**** 正在排队等候 ****"<<endl; wait_head->count++; write_to_file(); } void show_wait() { wait *tempw; tempw=wait_head->next; if (tempw==NULL) cout<<endl<<"排队中没有人!"<<endl; while(tempw!=NULL) { cout<<tempw->name<<" - "; tempw=tempw->next; } } void add_information(node *head,int x,int y) { node *temp; temp=new node; temp->pre=NULL; temp->next=NULL; cout<<"请输入个人信息"<<endl; cout<<endl<<"*************"<<endl; cout<<"姓名 : ";cin>>temp->name; cout<<"ID号 : ";cin>>temp->id; temp->seat=y; temp->plane=x; temp->next=head->next; temp->pre=head; if (head->next!=NULL) head->next->pre=temp; head->next=temp; write_to_file(); cout<<endl<<"**** 订票成功 ****"<<endl; } void search_delete(int x) { node *p,*q,*r; wait *tempw,*tempw2,*tempw3; int step=1,t1,t2,i; char ch; p=new node; tempw=new wait; tempw2=new wait; tempw3=new wait; q=head1; cout<<endl<<"请输入个人信息"<<endl; cout<<"*************"<<endl; cout<<endl<<"姓名 : ";cin>>p->name; do{ q=q->next; if ( (q!=NULL) && (comp(q,p)) ) { cout<<endl; q->date=q->plane; cout<<"Located!"<<endl; cout<<"****************"; cout<<endl<<"姓名 : "<<q->name; cout<<endl<<"ID号 : "<<q->id; cout<<endl<<"座位号 : "<<q->seat; cout<<endl<<"班机号 : "<<q->plane; cout<<endl<<"日期 : "<<q->date<<endl; if (x==1) { cout<<"删除该纪录 ? [Y/N] "; cin>>ch; if (ch=='Y' || ch=='y') { t1=q->plane; t2=q->seat; a[t1].seat[t2]=0; a[t1].seat[0]--; r=q;q=q->pre; r->pre->next=r->next; if(r->next!=NULL) r->next->pre=r->pre; delete(r); cout<<"**** 记录删除成功 ! ****"; write_to_file(); tempw=wait_head; for(i=0;i<wait_head->count;i++) { tempw=tempw->next; if(tempw==NULL) break; if((tempw->plane==t1) && (tempw->seat==t2)) { strcpy(tempw3->name,tempw->name); strcpy(tempw3->phone,tempw->phone); cout<<endl<<"等候的人中有可以订票的了:"<<endl; cout<<endl<<"姓名 : "<<tempw->name; cout<<endl<<"ID号 : "<<tempw->id<<endl; a[t1].seat[0]++; a[t1].seat[t2]=1; if(tempw->plane==1) add_information(head1,1,tempw->seat); if(tempw->plane==2) add_information(head2,2,tempw->seat); if(tempw->plane==3) add_information(head3,3,tempw->seat); tempw2=tempw->pre; tempw2->next=tempw->next; if(tempw->next==NULL) wait_end=tempw2; else tempw->next->pre=tempw2; delete(tempw); wait_head->count--; write_to_file(); cout<<endl<<"等候的"<<tempw3->name<<"已经成功订票,已经由电话"<<tempw3->phone<<"通知了"<<endl; break; } } } }continue; } else { if (q==NULL) { step++; if(step==2) q=head2; if(step==3) q=head3; if(step==4) {cout<<endl<<"**** 信息检索完毕 ****";break;}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值