#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
using namespace std;
typedef struct _REC
{
char name[20];//名字
char ID[20];//身份证
char seat[20];//座位
}REC;
int Menu()
{
int choice;
system("cls");
cout<<"1:订票"<<endl;
cout<<"2:查看清单"<<endl;
cout<<"0:退出"<<endl;
cout<<"请输入选择:";
cin>>choice;
return choice;
}
int FindTick(REC rec[],int n,char s[])
{
int i;
for(i=0;i<n;i++)
{
if(!strcmp(rec[i].seat,s))
return i;
}
return -1;
}
void int2seat(int x,char ch,char ans[])
{
int i=0;
if(x>=10)
{
ans[i++]=x/10+'0';
}
ans[i++]=(x%10)+'0';
ans[i++]=ch;
ans[i]='\0';
}
int SelectAll(REC rec[],int n)
{
int i,j,ans;
char seat[20];
char temp[20];
system("cls");
printf("%-4s%-4s%-4s%-4s%-4s\n","","A","B","C","D");
for(i=1;i<=20;i++)
{
printf("%-2d:",i);
for(j='A';j<='D';j++)
{
int2seat(i,j,seat);
if(FindTick(rec,n,seat)!=-1)
{
printf("%-4s","无");
}
else
{
printf("%-4s","有");
}
}
printf("\n");
}
cout<<"是否购买机票?Y/N"<<endl;
cin>>temp;
if(!strcmp("Y",temp)||!strcmp("y",temp))
{
cout<<"请输入要购买的座位号:";
cin>>temp;
ans=FindTick(rec,n,temp);
if(ans==-1)
{
cout<<"请输入购买人姓名:";
cin>>rec[n].name;
cout<<"请输入购买人身份证号:";
cin>>rec[n].ID;
strcpy(rec[n].seat,temp);
cout<<"购买成功!"<<endl;
system("pause");
return n+1;
}
else
{
cout<<"该座位已售出!"<<endl;
system("pause");
return n;
}
}
else
return n;
}
int SelectSmoke(REC rec[],int n)
{
int i,j,ans;
char seat[20];
char temp[20];
system("cls");
printf("%-4s%-4s%-4s%-4s%-4s\n","","A","B","C","D");
for(i=19;i<=20;i++)
{
printf("%-2d:",i);
for(j='A';j<='D';j++)
{
int2seat(i,j,seat);
if(FindTick(rec,n,seat)!=-1)
{
printf("%-4s","无");
}
else
{
printf("%-4s","有");
}
}
printf("\n");
}
cout<<"是否购买机票?Y/N"<<endl;
cin>>temp;
if(!strcmp("Y",temp)||!strcmp("y",temp))
{
cout<<"请输入要购买的座位号:";
cin>>temp;
ans=FindTick(rec,n,temp);
if(ans==-1)
{
cout<<"请输入购买人姓名:";
cin>>rec[n].name;
cout<<"请输入购买人身份证号:";
cin>>rec[n].ID;
strcpy(rec[n].seat,temp);
cout<<"购买成功!"<<endl;
system("pause");
return n+1;
}
else
{
cout<<"该座位已售出!"<<endl;
system("pause");
return n;
}
}
else
return n;
}
int SelectWindow(REC rec[],int n)
{
int i,j,ans;
char seat[20];
char temp[20];
system("cls");
printf("%-4s%-4s%-4s%-4s%-4s\n","","A","B","C","D");
for(i=1;i<=20;i++)
{
printf("%-2d:",i);
for(j='A';j<='D';j++)
{
if(j=='B'||j=='C')
{
printf("%-4s","");
continue;
}
int2seat(i,j,seat);
if(FindTick(rec,n,seat)!=-1)
{
printf("%-4s","无");
}
else
{
printf("%-4s","有");
}
}
printf("\n");
}
cout<<"是否购买机票?Y/N"<<endl;
cin>>temp;
if(!strcmp("Y",temp)||!strcmp("y",temp))
{
cout<<"请输入要购买的座位号:";
cin>>temp;
ans=FindTick(rec,n,temp);
if(ans==-1)
{
cout<<"请输入购买人姓名:";
cin>>rec[n].name;
cout<<"请输入购买人身份证号:";
cin>>rec[n].ID;
strcpy(rec[n].seat,temp);
cout<<"购买成功!"<<endl;
system("pause");
return n+1;
}
else
{
cout<<"该座位已售出!"<<endl;
system("pause");
return n;
}
}
else
return n;
}
int SelectInside(REC rec[],int n)
{
int i,j,ans;
char seat[20];
char temp[20];
system("cls");
printf("%-4s%-4s%-4s%-4s%-4s\n","","A","B","C","D");
for(i=1;i<=20;i++)
{
printf("%-2d:",i);
for(j='A';j<='D';j++)
{
if(j=='A'||j=='D')
{
printf("%-4s","");
continue;
}
int2seat(i,j,seat);
if(FindTick(rec,n,seat)!=-1)
{
printf("%-4s","无");
}
else
{
printf("%-4s","有");
}
}
printf("\n");
}
cout<<"是否购买机票?Y/N"<<endl;
cin>>temp;
if(!strcmp("Y",temp)||!strcmp("y",temp))
{
cout<<"请输入要购买的座位号:";
cin>>temp;
ans=FindTick(rec,n,temp);
if(ans==-1)
{
cout<<"请输入购买人姓名:";
cin>>rec[n].name;
cout<<"请输入购买人身份证号:";
cin>>rec[n].ID;
strcpy(rec[n].seat,temp);
cout<<"购买成功!"<<endl;
system("pause");
return n+1;
}
else
{
cout<<"该座位已售出!"<<endl;
system("pause");
return n;
}
}
else
return n;
}
void BookMenu(REC rec[],int* n)
{
int choice;
system("cls");
do
{
system("cls");
cout<<"1:查看全部余票"<<endl;
cout<<"2:查看吸烟区余票"<<endl;
cout<<"3:查看靠窗区余票"<<endl;
cout<<"4:查看普通区余票"<<endl;
cout<<"0:返回"<<endl;
cout<<"请输入选择:";
cin>>choice;
switch(choice)
{
case 1:
*n=SelectAll(rec,*n);
break;
case 2:
*n=SelectSmoke(rec,*n);
break;
case 3:
*n=SelectWindow(rec,*n);
break;
case 4:
*n=SelectInside(rec,*n);
break;
}
}while(choice!=0);
}
void PrintAll(REC rec[],int n)
{
int i;
system("cls");
if(n==0)
{
cout<<"无订票信息!"<<endl;
system("pause");
return;
}
printf("%-10s%-20s%-10s\n","姓名","身份证","座位号");
for(i=0;i<n;i++)
{
printf("%-10s%-20s%-10s\n",rec[i].name,rec[i].ID,rec[i].seat);
}
system("pause");
}
int main()
{
int choice;
REC rec[100];
int n=0;
do
{
choice=Menu();
if(choice==1)
{
BookMenu(rec,&n);
}
else if(choice==2)
{
PrintAll(rec,n);
}
}while(choice!=0);
return 0;
}
飞机订票系统
最新推荐文章于 2024-05-09 09:54:51 发布