⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dabao.cpp

📁 数据结构课程设计,航班顶票系统 .我的心血呀
💻 CPP
字号:
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>

#define m 5          //飞机数
#define n 8          //每架飞机8张票

struct node
{
  char name[21];
  int seat,plane,date,jianhao,phone;
  node *next,*pre;
};
struct wait
{
  char name[21];
  char phone[13];
  char jianhao[22];
  int seat,plane,date,count;
  wait *next,*pre;
};
struct piao
{
  int seat[n+1];
};

void wenjian();                               //初始化
void wenjian_piao();                         //从文件中读入票的信息
void wenjian_xinxi();                        //从文件中读入乘客信息
void wenjian_wait();                         //从文件中读入排队信息
void xianshi_menu();                         //显示主菜单
void xianshi_piao();                         //显示指定班机的余票
void xianshi_xinxi();                        //选择要察看的班机
void plane_xinxi(node *head);                //显示指定班机中所有乘客信息
void book();                                  //实现订票的功能
void add_xinxi(node *head,int x,int y);       //订票后将乘客信息加入相应的链表
void add_wait(int x,int y);                   //实现插队的功能
void search_del(int x);                    //参数x=0实现查找,x=1时实现删除
void write_to_file();                         //将票和各链表的信息写入文件中
void show_wait();                             //实现显示队列中人名的功能
bool comp(node *x,node*y);                    //判断是否匹配
void piaojia();  
node *head1,*head2,*head3,*head4,*q;
wait *wait_head,*wait_end;
char c;
piao a[m];

void main()                                       //主函数
{

  wenjian();
  do
  {  xianshi_menu();
     cout<<endl<<"请选择: ";
     cin>>c;
     if (c!='0')
        switch(c)
     {
           case '1' : show_wait();break;
        case '2' : {xianshi_piao();book();}break;
        case '3' : search_del(1);break;
        case '4' : xianshi_piao();break;
        case '5' : xianshi_xinxi();break;
        case '6' : search_del(0);break;
        case '7' : piaojia();break;
        default : break;
     }
  }while(c!='0');
  cout<<endl<<"";
  cout<<"退出系统! "<<endl;
}
void wenjian()                              //初始化
{
  wenjian_piao();
  wenjian_xinxi();
  wenjian_wait();
}
void xianshi_menu()                         //显示主菜单
{  cout<<endl<<"------------------------------------------------------";
   cout<<endl<<"****欢迎使用合肥航空有限责任公司售票服务系统*****";
  cout<<endl<<"";
  cout<<endl<<"";
   cout<<endl<<"                    --菜单--";
  cout<<endl<<"    ******************************************";
  cout<<endl<<"    *-----------1 : 查看预约情况------------*";
  cout<<endl<<"    *-----------2 : 订票程序----------------*";
  cout<<endl<<"    *-----------3 : 退票程序----------------*";
  cout<<endl<<"    *-----------4 : 查看剩余票数------------*";
  cout<<endl<<"    *-----------5 : 查看航班信息------------*";
  cout<<endl<<"    *-----------6 : 查看乘客信息------------*";
  cout<<endl<<"    *-----------7 : 今日票价----------------*";
  cout<<endl<<"    *-----------0 : 退出系统----------------*";
  cout<<endl<<"    ******************************************";
  cout<<endl<<"";
}
void piaojia()
{cout<<"************今日票价*********************"<<endl;
cout<<endl<<"";
cout<<" 合肥到北京:1300元/张,购买两张以上8.5折。"<<endl;
cout<<" 合肥到上海:900元/张, 购买两张以上9折。 "<<endl;
cout<<" 合肥到广州:1500元/张,购买三张以上7折。"<<endl;
cout<<" 合肥到新疆:2000元/张,购买两张以上8折。"<<endl;
cout<<endl<<"";
cout<<"********谢谢光临,祝君旅途愉快************"<<endl;
}

void wenjian_piao()                         //从文件中读入票的信息
{
   FILE *fp;
   int i;
   if((fp=fopen("piao.db","r")) == NULL )
   {
      fp=fopen("piao.db","w");
      for (i=1;i<=m-1;i++)
         fwrite(&a[i],sizeof(piao),1,fp);
      fclose(fp);
      fp=fopen("piao.db","r");
   }
   for(i=1;i<=m-1;i++)
      fread(&a[i],sizeof(piao),1,fp);
   fclose(fp);
}
void wenjian_xinxi()                        //从文件中读入乘客信息
{
   node *r;
   FILE *fp;
   int i,j,sum;
sum=a[1].seat[0]+a[2].seat[0]+a[3].seat[0]+a[4].seat[0];
   fp=fopen("xinxi.db","r");
   head1=new node;
   head2=new node;
   head3=new node;
   head4=new node;
   head1->pre=NULL;
   head1->next=NULL;
   head2->pre=NULL;
   head2->next=NULL;
   head3->pre=NULL;
   head3->next=NULL;
   head4->pre=NULL;
   head4->next=NULL;
   q=head1;
   for(i=1;i<=sum;i++)
   {
      j=0;
      r=new node;
      fread(r,sizeof(node),1,fp);
      q->next=r;
      r->pre=q;
      r->next=NULL;
      q=q->next;
      fclose(fp);
      if(i==a[1].seat[0]+1) {
         head2->next=q;
         q->pre->next=NULL;
         q->pre=head2;
      }
      if(i==a[1].seat[0]+a[2].seat[0]+1) 
	  {
         head3->next=q;
         q->pre->next=NULL;
         q->pre=head3;
      }
if(i==a[1].seat[0]+a[2].seat[0]+a[3].seat[0]+1) {
         head4->next=q;
         q->pre->next=NULL;
         q->pre=head4;
      }
   }
}
void wenjian_wait()                         //从文件中读入排队信息
{
   wait *tempw;
   FILE *fp;
   tempw=new wait;
   int i;
   if((fp=fopen("wait.db","r")) ==NULL )
   {
      fp=fopen("wait.db","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.db","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 xianshi_piao()                         //显示指定班机的余票
{
   cout<<"(1为北京;2为上海;3为广州;4新疆)"<<endl;
   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<<"到"<<i<<"地的票已经订完!"<<endl<<endl;
   }
}
void xianshi_xinxi()                           //选择要察看的班机
{
   int x;
   do {cout<<endl<<"显示到哪里的飞机的信息 ?(1为北京;2为上海;3为广州;4新疆) ";
      cin>>x;cout<<endl;}
   while(x<1 || x>=m);
   cout<<endl<<"到 "<<x<<" 的飞机的信息如下 "<<endl;
   cout<<endl<<"以下为到 "<<x<<" 的飞机已有乘客信息"<<endl;
   cout<<"";
   if(x==1) plane_xinxi(head1);
   if(x==2) plane_xinxi(head2);
   if(x==3) plane_xinxi(head3);
   if(x==4) plane_xinxi(head4);
}
void plane_xinxi(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->name<<endl;
      cout<<"日期 :"<<"星期"<<q->date<<endl;  
      cout<<"座位号 : "<<q->seat<<endl;
      q=q->next;x++;
      if (x % 4 ==0) ch=getch();
   }
   cout<<endl;
}void book()                           //实现订票的功能
{
   int i,j,p;
   cout<<endl<<"请选择地点:(1.北京;2.上海;3.广州;4新疆) ";
   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_xinxi(head1,1,j);
       if(i==2) add_xinxi(head2,2,j);
       if(i==3) add_xinxi(head3,3,j);
       if(i==4) add_xinxi(head4,4,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<<"电话 : ";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_xinxi(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<<"身份证号 : ";cin>>temp->jianhao;
   cout<<"电话 : ";cin>>temp->phone;
   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_del(int x)                    //参数x=0实现查找,x=1时实现删除
{
   node *p,*q,*r;
   wait *tempw,*tempw2,*tempw3,*tempw4;
   int step=1,t1,t2,i;
   char ch;
   p=new node;
   tempw=new wait;
   tempw2=new wait;
   tempw3=new wait;
   tempw4=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<<"身份证号 : "<<q->jianhao;
         cout<<endl<<"电    话 : "<<q->phone;
         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(tempw4->name,tempw->name);
                     strcpy(tempw4->phone,tempw->phone);
                     cout<<endl<<"等候的人中有可以订票的了:"<<endl;
                     cout<<endl<<"姓名 : "<<tempw->name;
                     a[t1].seat[0]++;
                     a[t1].seat[t2]=1;
                     if(tempw->plane==1) add_xinxi(head1,1,tempw->seat);
                     if(tempw->plane==2) add_xinxi(head2,2,tempw->seat);
                     if(tempw->plane==3) add_xinxi(head3,3,tempw->seat);
                     if(tempw->plane==4) add_xinxi(head4,4,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<<"等候的"<<tempw4->name<<"已经成功订票,已经由电话"<<tempw4->phone<<"通知了"<<endl;
                     break;
                  }
               }

            }
         }continue;
      }
      else 
      {
         if (q==NULL) 
         {
            step++;
            if(step==2) q=head2;
            if(step==3) q=head3;
            if(step==4) q=head4;
            if(step==5) {cout<<endl<<"**** 信息检索完毕 ****";break;}
         }
      }
   }while(1);
}
bool comp(node *x,node *y)                   //判断是否匹配
{
   node *p,*q;
   int i,j,k;
   p=x;
   q=y;
   i=j=0;
   do
   {
      while ( (p->name[i] != q->name[j]) && (p->name[i] != '\0') ) i++;
         
      if (p->name[i] == '\0') {return(false);break;}
      else 
      {
         k=i;
         while ( (p->name[k] == q->name[j]) && (q->name[j]!='\0') ) {k++;j++;}
         if (q->name[j]=='\0') return(true);
         else
         {
            j=0;
            i++;
         }
      }
   }while( (q->name[j]!='\0') && (p->name[i] != '\0') );
   return(false);
}

void write_to_file()                      //将票和各链表的信息写入文件中
{
   FILE *fp;
   int i,j;
   int x[m];
   node *p;
   wait *tempw;
   tempw=new wait;
   tempw=wait_head;
   fp=fopen("piao.db","w");
   for (i=1;i<=m-1;i++)
   {
      fwrite(&a[i],sizeof(piao),1,fp);
   }
   fclose(fp);

   fp=fopen("xinxi.db","w");
   x[0]=0;x[1]=a[1].seat[0];
   for(i=0,j=1;j<=m-1;j++) {i=i+a[j].seat[0];x[j]=a[j].seat[0]+x[j-1];}
   j=1;p=head1->next;
   for(j=1;j<=i;j++)
   {
      if(j==x[1]+1) p=head2->next;
      if(j==x[2]+1) p=head3->next; 
	  if(j==x[3]+1) p=head4->next;   
      if(p==NULL)break;
      fwrite(p,sizeof(node),1,fp);
      p=p->next;
   }
   fclose(fp);

   fp=fopen("wait.db","w");
   for(j=0;j<=wait_head->count;j++)
   {  
      if(tempw==NULL)break;
      fwrite(tempw,sizeof(wait),1,fp);
      tempw=tempw->next;
   }
   fclose(fp);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -