标题: [求助]怎么实现循环? [打印本页] 作者: 封xin封mg 时间: 2009-11-5 01:01 标题: [求助]怎么实现循环? 程序的要求是这样的:
某航空公司购买了一台用于航空订票系统的计算机,要求对新系统编程,对每个航班订座(每班10位)。
要求:
程序显示下列菜单选项:
? Please type 1 for “smoking”
? Please type 2 for “nonsmoking”
如果输入1,则程序指定吸烟舱位(座位1到5),如果输入2,则程序指定非吸烟舱位(座位6到10)。程序应输出一个登机牌,表示座位号和是否为吸烟舱位。
提示:
用一个单下标数组表示飞机的座位图。将数组的所有元素初始化为0,表示所有座位都是空的。订每个座位时,将数组相应元素设置为1,表示该座位已订。当然,程序不能再订已经订过的座位。吸烟舱位已满时,应该询问可否订非吸烟舱位;同样,非吸烟舱位已满时,应询问可否订吸烟舱位。如果同意,再相应订座,否则打印消息:“Next flight leaves in 3 hours”。
#include<iostream>
const int SeatsNum = 5; //定义座位数
using namespace std;
int choice = 0; // 选择输入
int seats1[SeatsNum] = {0}; // 吸烟仓座位
int seats2[SeatsNum] = {0}; //无烟仓座位
int getSeatNum(int* seats); //计算座位号
void getSmokingNum(int* seats);//输入所定的座位号和仓类型
void getUnsmokingNum(int* seats);//输入所定的座位号和仓类型
void menu(); //选择菜单
bool seatsOrNot(int* seats); //判断是否有座位
bool seatsOrNot(int* seats) //判断是否有座位
{
for(int i = 0; i < SeatsNum; i++)
{
if(seats[i] == 0)
return true;
}
return false;
}
int getSeatNum(int* seats) //计算座位号
{
int seatNum = 0;
for(int i = 0; i < SeatsNum; i++)
{
if(seats[i] == 0)
{
seats[i] == 1;
seatNum == i+1; //座位号
return seatNum;
}
}
return 0;
}
void getSmokingNum(int* seats) //输出座位号和仓类型
{
int input = 0;
if(seatsOrNot(seats))
cout<< "The seat you booked is " << getSeatNum(seats) << " in the smoking.";
else
{
cout<<"The smoking seats have been sold out!"<<endl;
cout<<"If you want to book a unsmoking seat enter 2,press other keys to return......"<<endl;
cin>>choice;
if(input == 2)
getUnsmokingNum(seats2);
else
{
cout<<"Next flight leaves in three hours......."<<endl;
return;
}
}
}
void getUnsmokingNum(int* seats)//输出座位号和仓类型
{
int input = 0;
if(seatsOrNot(seats))
cout<< "The seat you booked is " << getSeatNum(seats) << " in the unsmoking.";
else
{
cout<<"The smoking seats have been sold out!"<<endl;
cout<<"If you want to book a unsmoking seat enter 1,press other keys to return......"<<endl;
cin>>input;
if(input == 1)
getSmokingNum(seats1);
else
{
cout<<"Next flight leaves in three hours......."<<endl;
return;
}
}
}
void menu()//菜单
{
cout<<"Welcome to use this seats booking system."<<endl;
cout<<"Book the smoking seat press ------1------"<<endl;
cout<<"Book the smoking seat press ------2------"<<endl;
cout<<"Input: ";
}
int main()//主函数
{
menu();
cin>>choice;
switch(choice)
{
case 1: getSmokingNum(seats1);break;
case 2: getUnsmokingNum(seats2);break;
default: ;break;
}
return 0;
}