问题描述:
6.21(航班预订系统)一个小的航空公司刚刚为新的自动航班预订系统购买了一台计算机。公司总裁要求编程实现这个新系统,要求程序必须为公司唯一的一架飞机(10个座位)的每一次飞行分配座位,程序先显示如下选择界面:
Please type 1 for "first classPlease type 2 for "economy"
若用户键入1,则程序将为他在first class (一等舱)区(座位号是1~5)分配一个座位;若用户键入2,则程序将为他在economy(经济舱)区(座位号是6~10)分配一个座位。然后程序将打印出显示有座位号和舱区的登机牌。
使用一个单下标数组来表示飞机座位分布图。数组的所有元素被初始化为О以表示座位是空的。一旦座位被分配出去,相应的数组元素将被置成1以表示该座位不可再分配。
当然,不允许将一个已经分配的座位再次分配。当一等舱区满座之后,程序将询问乘客是否愿意
调到经济舱区(反之亦然)。若乘客愿意,则为他分配一个适当的座位。否则打印信息“Next flight leaves in 3 hours”(下一个航班将在3小时以后起飞)。
以下为源代码》:
#include <stdio.h>
int firstclass (int a[]);
int ecoflight(int b[]);
int main()
{
int option=5,i,num1=0,num2=0,h;
int select1,select2;
int cnt1=0,cnt2=0;
int result1,result2;
int a[6]={0},b[6]={0};
do {
printf("Please type 1 for \"first class\"\n");
printf("Please type 2 for \"economy\"\n");
printf("Please type -1 for to exit\n");
scanf("%d", &option);
if(option==1){
if (num1==5){
printf("the first class is full,would you like to change to the economy?\n");
printf("please input 1(meaning yes) or 2(meaning no).");
scanf("%d",&select1);
if (select1==1){
result2=ecoflight(b);
if(result2==1){
num2++;
}
} else {
printf("next flight leaves in 3 hours.\n");
}
}else {
result1=firstclass(a);
if(result1==1){
num1++;
}
}
} else if(option==2){
//num2=ecoflight(b,cnt2);
if (num2==5){
printf("the economy is full,would you like to change to the first class?\n");
printf("please input 1(meaning yes) or 2(meaning no).");
scanf("%d",&select2);
if (select2==1){
result1=firstclass(a);
if(result1==1){
num1++;
}
}else {
printf("next flight leaves in 3 hours.\n");
}
}else {
result2=ecoflight(b);
if(result2==1){
num2++;
}
}
}
}while ((num2<5||num1<5)&&option!=-1);
printf("the seats's situation is as followed:\n");
printf("first class:\n");
for(h=1;h<6;h++){
printf("flight[%d]=",h);
if(a[h]==1){
printf("已售出");
}else {
printf("空座位");
}
printf("\t");
}
printf("\n\n");
printf("economy:\n");
for(h=1;h<6;h++){
printf("flight[%d]=",h);
if(b[h]==1){
printf("已售出");
}else {
printf("空座位");
}
printf("\t");
}
return 0;
}
int firstclass (int a[]){
int j,cnt1=0;
//i=rand()%5+1;
for (j=1;j<6;j++){
if(a[j]==0){
a[j]=1;
cnt1=1;
printf("first class[%d]=%d\n",j,a[j]);
break;
}
}
return cnt1;
}
int ecoflight(int b[]){
int j,cnt2=0;
//i=rand()%5+1;
for (j=1;j<6;j++){
if(b[j]==0){
b[j]=1;
cnt2=1;
printf("economy[%d]=%d\n",j,b[j]);
break;
}
}
return cnt2;
}
附上运行结果: