#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define m 4 //3架飞机
#define n 5 //每架飞机5张票
struct node{
char name[21];
char id[21];
int seat,plane,date;
node *next,*pre;
};
struct wait{
char name[21];
char id[21];
char phone[8];
int seat,plane,date,count;
wait *next,*pre;
};
struct piao{
int seat[n+1];
};
void makenull();
void makenull_piao();
void makenull_information();
void list_menu();
void list_piao();
void makenull_wait();
void list_information();
void plane_information(node *head);
void book();
void add_information(node *head,int x,int y);
void add_wait(int x,int y);
void search_delete(int x);
void write_to_file();
void show_wait();
bool comp(node *x,node*y);
node *head1,*head2,*head3,*q;wait *wait_head,*wait_end;
char c;
piao a[m];
void main(){ makenull();
do { list_menu();
cout<<endl<<"choose an operation: ";
cin>>c;
if (c!='6')
switch(c) {
case '0' : show_wait();break;
case '1' : {list_piao();book();}break;
case '2' : search_delete(1);break;
case '3' : list_piao();break;
case '4' : list_information();break;
case '5' : search_delete(0);break;
default : break; }
}while(c!='6');
cout<<"Exit System ";}
void makenull(){
makenull_piao();
makenull_information();
makenull_wait();}
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<<" ************************";
cout<<endl<<"";}
void makenull_piao(){
FILE *fp;
int i;
if((fp=fopen("piao.dat","r")) == NULL ) {
fp=fopen("piao.dat","w");
for (i=1;i<=m-1;i++)
fwrite(&a[i],sizeof(piao),1,fp);
fclose(fp);
fp=fopen("piao.dat","r");
} for(i=1;i<=m-1;i++)
fread(&a[i],sizeof(piao),1,fp);
fclose(fp);}
void makenull_information(){
node *r;
FILE *fp;
int i,j,sum;
sum=a[1].seat[0]+a[2].seat[0]+a[3].seat[0];
fp=fopen("information.dat","r");
head1=new node;
head2=new node;
head3=new node;
head1->pre=NULL;
head1->next=NULL;
head2->pre=NULL;
head2->next=NULL;
head3->pre=NULL;
head3->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;
} }}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();
}