📄 hangkongdingpiao.cpp
字号:
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define m 10//9架飞机
#define n 25 //每架飞机25张票
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[11];
int seat,plane,count,date;
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 *head[m-1],*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!='7')
switch(c)
{
case '1' : show_wait();break;
case '2' : {list_piao();book();}break;//先查看剩余票然后再定票
case '3' : search_delete(1);break;
case '4' : list_piao();break;
case '5' : list_information();break;
case '6' : search_delete(0);break;
default : break;
}
}while(c!='7');
cout<<"Exit System ";
}
void makenull()
{
makenull_piao();
makenull_information();
makenull_wait();
}
void list_menu()
{
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<<" ************************";
cout<<endl<<"";
}
void makenull_piao()
{
FILE *fp;
int i;
//fopen()函数中第一个参数为要打开的文件名,第二个表示文件形式,例如'r'表示只读'w'表示只写
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); //fwrite()函数功能是将内存中的数据读到磁盘上
fclose(fp);
fp=fopen("piao.dat","r");
}
for(i=1;i<=m-1;i++)
fread(&a[i],sizeof(piao),1,fp);
//fread()函数功能是读入数据,第一的参数表示要读入数据的存放地址,通常为指针形式,
fclose(fp);//通常为防止读入的数据丢失在读入一个流后要关闭文件
}
void makenull_information()
{
node *r;
FILE *fp;
int i,j,k,sum=0,sum1;
for(k=1;k<=m-1;k++)
sum+=a[k].seat[0];
fp=fopen("information.dat","r");
for(k=1;k<=m-1;k++)
{
head[k]=new node;
head[k]->pre=NULL;
head[k]->next=NULL;
}
q=head[1];
for(i=1;i<=sum;i++)
{
j=0;
sum1=0;
r=new node;
fread(r,sizeof(node),1,fp);
q->next=r; //q做头指针将读入的数据链到q后
r->pre=q;
r->next=NULL;
q=q->next;//q指向读入的数据r
fclose(fp);
for(k=1;k<m-1;k++)
{
sum1+=a[k].seat[0];
if(i==sum1+1) {
head[k+1]->next=q;
q->pre->next=NULL; //q->pre表示读入的第i-1个数据。整个功能是断开链表用head[k+1]当头指针
q->pre=head[k+1];
}
}
}//for
}
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) //第i架飞机的座位没有满
{
switch(i){
case 1: cout<<endl<<"第 "<<i<<" 架飞机西安—北京未预订的座位号 :"<<endl;break;
case 2: cout<<endl<<"第 "<<i<<" 架飞机西安—上海未预订的座位号 :"<<endl;break;
case 3: cout<<endl<<"第 "<<i<<" 架飞机西安—成都未预订的座位号 :"<<endl;break;
case 4: cout<<endl<<"第 "<<i<<" 架飞机西安—广州未预订的座位号 :"<<endl;break;
case 5: cout<<endl<<"第 "<<i<<" 架飞机西安—杭州未预订的座位号 :"<<endl;break;
case 6: cout<<endl<<"第 "<<i<<" 架飞机西安—海南未预订的座位号 :"<<endl;break;
case 7: cout<<endl<<"第 "<<i<<" 架飞机西安—天津未预订的座位号 :"<<endl;break;
case 8: cout<<endl<<"第 "<<i<<" 架飞机西安—石家庄未预订的座位号 :"<<endl;break;
case 9: cout<<endl<<"第 "<<i<<" 架飞机西安—青岛未预订的座位号 :"<<endl;break;
}
for(j=1;j<=n;j++)
if (a[i].seat[j]==0) cout<<" "<<j;//a[i].seat[p]==0表示到i地的飞机第p号座位未预定
cout<<endl;
}//if
else cout<<endl<<"The "<<i<<" plane is full !"<<endl<<endl;
} //for
}
void list_information() //查看飞机订票信息
{
int x;
do {cout<<endl<<"显示哪架飞机的信息 ? "; cin>>x;cout<<endl;}while(x<1 || x>=m);
cout<<endl<<"第 "<<x<<" 架飞机的信息如下 "<<endl;
plane_information(head[x]);
}
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 % (m-1) ==0) ch=getch();
}
cout<<endl;
}
void book()//订票
{
int i,j,p;
cout<<endl<<"请选择地点:(1..."<< m-1<<") ";
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; //a[i].seat[p]==0表示到i地的飞机第p号座位未预定
cout<<endl;
break;
}//else
}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;
}//else
}while(1);
if (a[i].seat[j]==0) {
a[i].seat[j]=1;
cout<<endl;
a[i].seat[0]++; //a[i].seat[0]表示第i架飞机已预订的票数
add_information(head[i],i,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<<"姓名 : ";cin>>tempw->name;
cout<<"ID号 : ";cin>>tempw->id;
cout<<"电话 : ";cin>>tempw->phone;
tempw->seat=y; //j由参数传递过来表示预订的座位号
tempw->plane=x; //x由参数传递过来表示预订的飞机号
wait_end->next=tempw;//与下一句一并将预订乘客信息插入链表
tempw->pre=wait_end;
wait_end=wait_end->next; //排队的队尾向后移,wait_end指针指向刚插入的元素
cout<<endl<<"**** 正在排队等候 ****"<<endl;
wait_head->count++;// 排队的队列长度加1
write_to_file();
}
void show_wait()//查看排队情况
{
wait *tempw;
tempw=wait_head->next;//初始化将排队信息的第一个赋给tempw
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; //接下来的四句话是把head结点插入到先前结点之后
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)//当x=1时退票,当x=0时查看乘客信息
{
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=head[1];
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]--; //将预订飞机的预订座位总数减1
//接下来三句话表示从该双向循环链表中删除对应的结点
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;//将 tempw指向排队链表的表头
for(i=0;i<wait_head->count;i++)//wait_head->count表示该链表中存有的结点个数
{
tempw=tempw->next;
if(tempw==NULL) break;
//排队的第一个人预订的飞机号及座位号的和已删除的记录相同
if((tempw->plane==t1) && (tempw->seat==t2))
{
strcpy(tempw3->name,tempw->name);//将tempw->name复制到tempw3->name中
strcpy(tempw3->phone,tempw->phone);
cout<<endl<<"等候的人中有可以订票的了:"<<endl;
cout<<endl<<"姓名 : "<<tempw->name;
cout<<endl<<"ID号 : "<<tempw->id<<endl;
a[t1].seat[0]++; //将预订飞机的预订座位总数加1
a[t1].seat[t2]=1; //将预订飞机的预订的座位号置为1代表已经预订
add_information(head[tempw->plane],tempw->plane,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;
}//if
}//for
}//if (ch=='Y' || ch=='y')
}continue;//if (x==1)
}//if ( (q!=NULL) && (comp(q,p)) )
else //查看乘客信息
{
if (q==NULL)
{
step++;
q=head[step];
if(step==m) {cout<<endl<<"**** 信息检索完毕 ****";break;}
}
}
}while(1);//do
}
bool comp(node *x,node *y)//将x->name[i]与y->name[j]进行匹配
{
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;} //p->name[i]已经查完
else//p->name[i]未查完且满足p->name[i] != q->name[j]
{
k=i;
//k和j自增直到p->name[k] 与 q->name[j]不再相等
while ( (p->name[k] == q->name[j]) && (q->name[j]!='\0') ) {k++;j++;}
if (q->name[j]=='\0') return(true); //q->name[k]已经查完
else//q->name[k]未查完且满足p->name[i] != q->name[k]
{
j=0;//q->name[j]从q->name[0]开始,p->name[i]从p->name[i+1]开始重新匹配
i++;
}
}
}while( (q->name[j]!='\0') && (p->name[i] != '\0') );
return(false);
}
void write_to_file()
{
FILE *fp;
int i,j,e;
int x[m];
node *p;
wait *tempw;
tempw=new wait;
tempw=wait_head;//初始化将wait_head指针赋给tempw
fp=fopen("piao.dat","w");
for (i=1;i<=m-1;i++)
{
fwrite(&a[i],sizeof(piao),1,fp);
}
fclose(fp);
fp=fopen("information.dat","w");
x[0]=0;
for(i=0,j=1;j<=m-1;j++) i=i+a[j].seat[0];
j=1;p=head[1]->next;
for(j=1;j<=i;j++)//i=a[1].seat[0]+a[2].seat[0]+a[m-1].seat[0]即所有架飞机的总座位数
{
e=j/n+1;
if(j%n==1) p=head[e]->next;//j==x[1]+1表示查到了第二架飞机的座位
if(p==NULL)break;
fwrite(p,sizeof(node),1,fp);
p=p->next;
}//for
fclose(fp);
fp=fopen("wait.txt","w");
for(j=0;j<=wait_head->count;j++)
{
if(tempw==NULL)break;
fwrite(tempw,sizeof(wait),1,fp);
tempw=tempw->next;
}//for
fclose(fp);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -