gpt4 book ai didi

c - 在 C 和程序问题中将数组作为函数参数传递

转载 作者:行者123 更新时间:2023-11-30 20:18:35 25 4
gpt4 key购买 nike

情况

一家小型航空公司刚刚为其新的自动预订系统购买了一台计算机。总统要求你为新系统编程。您将编写一个程序来为航空公司唯一飞机的每个航类分配座位(容量:10 个座位)。您的程序应显示以下替代菜单:请输入 1 表示“头等舱”

请输入 2 表示“经济”如果该人输入 1,那么您的程序应在头等舱分配一个座位(座位 1-5)。如果该人输入 2,那么您的程序应在经济舱分配一个座位(座位 6-10)。然后,您的程序应该打印一张登机牌,标明该人的座位号以及是在飞机的头等舱还是经济舱。使用一维数组来表示飞机的座位表。将数组的所有元素初始化为0,表示所有座位都是空的。当每个座位被分配时,将数组的相应元素设置为 1 以指示该座位不再可用。当然,您的程序永远不应该分配已经分配的座位。当头等舱已满时,您的程序应询问该人是否可以被安排在经济舱(反之亦然)。如果是,则进行适当的座位分配。如果不是,则打印消息“下一趟航类将在 3 小时后起飞。”

问题:我的代码有什么问题才能使其正常工作。

 #include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define SIZE 11

int firstClass(int totalSeats[SIZE])
{
int counter;

for (counter = 1; counter <= 5; counter++)
{
if (totalSeats[counter] = 0)
{
totalSeats[counter] = 1;
printf("Your seat number is %d, you are a firstclass flyer\n", totalSeats[counter]);
return 0;
}


}
return -1;
}

int economy(int totalSeats[SIZE])
{
int counter;

for (counter = 6; counter <= SIZE; counter++)
{
if (totalSeats[counter] = 0)
{
totalSeats[counter] = 1;
printf("Your seat number is %d, you are a economy flyer\n", totalSeats[counter]);
return 0;
}

}

return -1;
}


int main()
{
int purchaseCode;
int first = 0;
int econ = 0;
int totalSeats[SIZE] = { 0 };
char economyChoice;
char firstClassChoice;

printf("Please Type '1' for first class '2' for economy: ");
scanf_s("%d", &purchaseCode);

while (purchaseCode > 0)
{
if (purchaseCode = 1)
{
first = firstClass(totalSeats[SIZE]);
if (first = -1)
{
printf("Would you like to try economy? <Y/N>: ");
scanf_s(" %c", &economyChoice, 1);

while (toupper(economyChoice = 'Y'))
{
econ = economy(totalSeats[SIZE]);
if (toupper(economyChoice ='N'))
{
printf("Next flight leaves in 3 hours.\n");
break;
}

}
}
}
else if (purchaseCode = 2)
{
econ = economy(totalSeats[ SIZE]);
if (econ = -1)
{
printf("Would you like to try first class? <Y/N>: ");
scanf_s(" %c", &firstClassChoice, 1);

while (toupper(firstClassChoice == 'Y'))
{
first = firstClass(totalSeats[ SIZE]);
if (toupper(firstClassChoice == 'N'))
{
printf("Next flight leaves in 3 hours.");
printf("Please Type '1' for first class '2' for economy: ");
scanf_s("%d", &purchaseCode);
}

}

}
}
else
{
printf("Invalid Selection\n");
printf("Please Type '1' for first class '2' for economy: ");
scanf_s("%d", &purchaseCode);

}

printf("Please Type '1' for first class '2' for economy: ");
scanf_s("%d", &purchaseCode);

}
system("PAUSE");
return 0;
}

最佳答案

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define SIZE 11

int firstClass(int totalSeats[])
{
int counter;

for (counter = 1; counter <= 5; counter++)
{
if (totalSeats[counter] == 0)
{
totalSeats[counter] = 1;
printf("Your seat number is %d, you are a firstclass flyer\n",
totalSeats[counter]);
return 0;
}


}
return -1;
}

int economy(int totalSeats[])
{
int counter;

for (counter = 6; counter <= SIZE; counter++)
{
if (totalSeats[counter] == 0)
{
totalSeats[counter] = 1;
printf("Your seat number is %d, you are a economy flyer\n",
totalSeats[counter]);
return 0;
}

}

return -1;
}


int main()
{
int purchaseCode;
int first = 0;
int econ = 0;
int totalSeats[SIZE] = { 0 };
char economyChoice;
char firstClassChoice;

printf("Please Type '1' for first class '2' for economy '3' to exit: ");
scanf("%d", &purchaseCode);

while (purchaseCode > 0)
{
if (purchaseCode == 1)
{
first = firstClass(totalSeats);
if (first == -1)
{
printf("Would you like to try economy? <Y/N>: ");
scanf(" %c", &economyChoice);

if(toupper(economyChoice == 'Y'))
{
econ = economy(totalSeats);
}
if (toupper(economyChoice =='N'))
{
printf("Next flight leaves in 3 hours.\n");
break;
}
}
printf("Please Type '1' for first class '2' for economy '3' to exit: ");
scanf("%d", &purchaseCode);
}
else if (purchaseCode == 2)
{
econ = economy(totalSeats);
if (econ == -1)
{
printf("Would you like to try first class? <Y/N>: ");
scanf(" %c", &firstClassChoice);

if(toupper(firstClassChoice == 'Y'))
{
first = firstClass(totalSeats);
}
if (toupper(firstClassChoice == 'N'))
{
printf("Next flight leaves in 3 hours.");
printf("\nPlease Type '1' for first class '2' for economy '3' to exit: ");
scanf("%d", &purchaseCode);
}
}
printf("Please Type '1' for first class '2' for economy '3' to exit: ");
scanf("%d", &purchaseCode);
}
else if(purchaseCode == 3)
{
break;
}
else
{
printf("Invalid Selection\n");
printf("Please Type '1' for first class '2' for economy '3' to exit: ");
scanf("%d", &purchaseCode);

}
}
system("PAUSE");
return 0;
}

在使用 if 语句进行比较时,必须使用“==”而不是“=”。

将数组作为参数传递时,只需在函数调用中键入名称,并在函数定义中定义一个没有大小的数组。

关于c - 在 C 和程序问题中将数组作为函数参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53586357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com