📄 01.cpp
字号:
//:01.cpp
//飞机订座系统
#include<iostream>
#include<cstdlib>
using namespace std;
//本系统可以一次订多张票,输入几个一就订几张吸烟舱座位,输入几个二就订几张非吸烟舱座位
int main()
{
int seat[10]={0};//新建数组存放座位号,并初始化为0,代表座位没有订购
char i,a,b;
int k,j;
while(1)//while循环用于多次订票
{
cout<<"Please type 1 for \"Smoking\""<<endl;
cout<<"Please type 2 for \"Nonsmoking\""<<endl;
cin>>i;
choose:
switch(i)//switch用于选择订吸烟舱还是非吸烟舱
{
case '1'://选择吸烟舱
for(j=0;j<5;j++)//吸烟舱的座位号是1~5,for循环依次检验该座位是否被预定
{
if(seat[j]==0)
{
seat[j]=1;//该座位没有被预定,就订该座位,并赋值1表示该座已订
k=j+1;//k表示预定的座位号
break;//订完一个座后跳出循环
}
}
if(j==5)//吸烟舱已满
{
cout<<"The seat for smoking is full,whether to book the nonsmoking"<<endl;
cout<<"Please type 'Y' for \"Yes\""<<endl;
cout<<"Please type 'N' for \"No\""<<endl;//询问是否订购非吸烟舱
char a;
cin>>a;
if('y'==a)//订购非吸烟舱,并跳回switch的case ‘2’中
{
i='2';
goto choose;
}
else//不订购非吸烟舱,跳到程序底部
{
cout<<"Next flight leaves in 3 hours"<<endl;
goto bottom;
}
}
break;
case '2':
for(j=5;j<10;j++)//非吸烟舱的座位号是6~10,for循环依次检验该座位是否被预定
{
if(seat[j]==0)
{
seat[j]=1;//预定该座位
k=j+1;
break;
}
}
if(j==10)//非吸烟舱已满
{
cout<<"The seat for nonsmoking is full,whether to book the smoking"<<endl;
cout<<"Please type 'y' for \"Yes\""<<endl;
cout<<"Please type 'n' for \"No\""<<endl;//询问是否订购吸烟舱
char b;
cin>>b;
if('y'==b)//订购吸烟舱,并跳回switch的case ‘1’中
{
i='1';
goto choose;
}
else//不订购吸烟舱,跳到程序底部
{
cout<<"Next flight leaves in 3 hours"<<endl;
goto bottom;
}
}
break;
default://输入不是1与2,就返回重新选择
cout<<"Please input 1 or 2"<<endl;
char c;
cin>>c;
i=c;
goto choose;
}
cout<<"your boarding check:"<<endl;
cout<<"seat number:"<<k<<endl;//输出座位号
if(k>=1&&k<=5)
cout<<"smoking"<<endl;//输出吸烟舱
if(k>=6&&k<=10)
cout<<"nonsmoking"<<endl;//输出非吸烟舱
bottom:;
for(int sum=0,int d=0;d<10;d++)
sum=sum+seat[d];
if(sum==10)//如果座位已满,则退出程序
{
cout<<"There is no extra seat in this flight,please wait for the next"<<endl;
exit(0);
}
}
}///:~
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -