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

时间:2018-12-03 01:34:02

标签: c

情况

一家小型航空公司刚刚为其新的自动预订系统购买了一台计算机。总裁要求您对新系统进行编程。您将编写一个程序,在航空公司唯一的飞机的每次飞行中分配座位(容量: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;
 }

1 个答案:

答案 0 :(得分:1)

#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语句进行比较时,必须使用'=='而不是'='。

在将数组作为参数传递时,只需在函数调用中键入名称,然后在函数定义中定义不带大小的数组即可。