巨人公司的机群由12个座位的飞机组成。它每天飞行一个航班。根据下面的要求,编写一个座位预订程序。
/巨人公司的机群由12个座位的飞机组成。它每天飞行一个航班。根据下面的要求,编写一个座位预订程序。
a.该程序使用一个含12个结构体组成的数组。每个结构体中包括:一个成员表示座位编号,一个成员表示座位号是否已经被预订,一个成员表示预订人的名,一个成员表示预订人的姓。
b.该程序显示下面的菜单:
To choose a function,enter its letter label:
a) Show number of empty seats
b) Show list of empty seats
c) Show alphabetical list of seats
d) Assign a cutomer to a seat assignment
e) Delete a seat assignment
f) Quit
c.该程序能成功执行上面给出的菜单。选择d)和e)要提示用户进行额外输入,每个选项都能让用户中止输入
d.执行特定程序后,该程序再次显示菜单,除非用户选择f)。/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SEAT_NUM 12
typedef struct
{
int seat_number;
int is_booked;
char first_name[30];
char last_name[30];
} Seat;
void show_empty_seat_count(Seat seats[])
{
int count = 0;
for (int i = 0; i < SEAT_NUM; i++)
{
if (!seats[i].is_booked)
{
count++;
}
}
printf("空座位数量: %d\n", count);
}
void show_empty_seats(Seat seats[])
{
printf("空座位编号: ");
for (int i = 0; i < SEAT_NUM; i++)
{
if (!seats[i].is_booked)
{
printf("%d ", seats[i].seat_number);
}
}
printf("\n");
}
void show_alphabetical_seats(Seat seats[])
{
Seat booked_seats[SEAT_NUM];
int count = 0;
for (int i = 0; i < SEAT_NUM; i++)
{
if (seats[i].is_booked)
{
booked_seats[count++] = seats[i];
}
}
for (int i = 0; i < count - 1; i++)
{
for (int j = i + 1; j < count; j++)
{
if (strcmp(booked_seats[i].last_name, booked_seats[j].last_name) > 0)
{
Seat temp = booked_seats[i];
booked_seats[i] = booked_seats[j];
booked_seats[j] = temp;
}
}
}
printf("已预订座位(按姓氏排序):\n");
for (int i = 0; i < count; i++)
{
printf("座位编号: %d, 预订人: %s %s\n",
booked_seats[i].seat_number,
booked_seats[i].first_name,
booked_seats[i].last_name);
}
}
void assign_customer_to_seat(Seat seats[])
{
int seat_number;
printf("请输入座位编号(1-%d): ", SEAT_NUM);
scanf("%d", &seat_number);
seat_number--;
if (seat_number < 0 || seat_number >= SEAT_NUM)
{
printf("座位编号无效!\n");
return;
}
if (seats[seat_number].is_booked)
{
printf("该座位已被预订!\n");
return;
}
printf("请输入预订人名: ");
scanf("%s", seats[seat_number].first_name);
printf("请输入预订人姓: ");
scanf("%s", seats[seat_number].last_name);
seats[seat_number].is_booked = 1;
printf("座位 %d 已成功预订。\n", seats[seat_number].seat_number);
}
void delete_seat_assignment(Seat seats[])
{
int seat_number;
printf("请输入要取消预订的座位编号(1-%d): ", SEAT_NUM);
scanf("%d", &seat_number);
seat_number--;
if (seat_number < 0 || seat_number >= SEAT_NUM)
{
printf("座位编号无效!\n");
return;
}
if (!seats[seat_number].is_booked)
{
printf("该座位尚未被预订!\n");
return;
}
seats[seat_number].is_booked = 0;
memset(seats[seat_number].first_name, 0, sizeof(seats[seat_number].first_name));
memset(seats[seat_number].last_name, 0, sizeof(seats[seat_number].last_name));
printf("座位 %d 的预订已成功取消。\n", seats[seat_number].seat_number);
}
int main(void)
{
Seat seats[SEAT_NUM];
for (int i = 0; i < SEAT_NUM; i++)
{
seats[i].seat_number = i + 1;
seats[i].is_booked = 0;
}
char choice;
do
{
printf("\nTo choose a function,enter its letter label:\n");
printf("a) Show number of empty seats\n");
printf("b) Show list of empty seats\n");
printf("c) Show alphabetical list of seats\n");
printf("d) Assign a cutomer to a seat assignment\n");
printf("e) Delete a seat assignment\n");
printf("f) Quit\n");
printf("选择: ");
choice = getchar();
getchar(); //清除缓冲区,getchar()函数每次读取一个字符,换行符也是字符
switch (choice)
{
case 'a':
show_empty_seat_count(seats);
break;
case 'b':
show_empty_seats(seats);
break;
case 'c':
show_alphabetical_seats(seats);
break;
case 'd':
assign_customer_to_seat(seats);
break;
case 'e':
delete_seat_assignment(seats);
break;
case 'f':
printf("退出程序。\n");
//exit(0);
break;
default:
printf("无效选择,请重试。\n");
break;
}
}while(choice!='f');
return 0;
}