好的,鱼油。我们可以通过枚举类型来管理菜单选项,并结合用户输入来实现功能。以下是详细的步骤和代码实现:
### 1. 定义结构体与枚举类型
首先,我们定义一个结构体来存储座位信息,同时定义一个枚举类型来表示不同的菜单选项。
#include <stdio.h>
#include <string.h>
#define TOTAL_SEATS 12
#define NAME_LENGTH 50
// 结构体定义
typedef struct {
int seatNumber;
int isBooked;
char passengerName[NAME_LENGTH];
char passengerGender;
} Seat;
// 枚举类型定义
typedef enum {
SHOW_EMPTY_SEATS = 1,
SHOW_BOOKED_SEATS,
BOOK_SEAT,
CANCEL_SEAT,
EXIT
} MenuOption;
### 2. 初始化座位
我们可以编写一个函数来初始化所有座位,使它们标记为未售出。
void initializeSeats(Seat seats[], int totalSeats) {
for (int i = 0; i < totalSeats; i++) {
seats[i].seatNumber = i + 1;
seats[i].isBooked = 0; // 0表示未售出
strcpy(seats[i].passengerName, "");
seats[i].passengerGender = ' ';
}
}
### 3. 显示菜单选项
我们用一个简单的函数来显示菜单选项,并从用户那里获取输入。
MenuOption getMenuOption() {
int option;
printf("\nMenu:\n");
printf("1. 显示所有未被分配的空座位编号\n");
printf("2. 显示已经卖出去的座位编号,并能知道乘客姓名以及性别\n");
printf("3. 买票\n");
printf("4. 退票\n");
printf("5. 退出系统\n");
printf("请选择一个选项: ");
scanf("%d", &option);
return (MenuOption)option;
}
### 4. 显示空座位
void showEmptySeats(Seat seats[], int totalSeats) {
printf("未被分配的空座位编号:\n");
for (int i = 0; i < totalSeats; i++) {
if (!seats[i].isBooked) {
printf("座位编号: %d\n", seats[i].seatNumber);
}
}
}
### 5. 显示已售出座位
void showBookedSeats(Seat seats[], int totalSeats) {
printf("已经卖出去的座位编号:\n");
for (int i = 0; i < totalSeats; i++) {
if (seats[i].isBooked) {
printf("座位编号: %d, 姓名: %s, 性别: %c\n", seats[i].seatNumber, seats[i].passengerName, seats[i].passengerGender);
}
}
}
### 6. 订票
void bookSeat(Seat seats[], int totalSeats) {
int seatNumber;
printf("请输入要订的座位编号: ");
scanf("%d", &seatNumber);
if (seatNumber <= 0 || seatNumber > totalSeats) {
printf("座位编号无效\n");
return;
}
if (seats[seatNumber - 1].isBooked) {
printf("该座位已经被订了\n");
return;
}
seats[seatNumber - 1].isBooked = 1;
printf("请输入乘客姓名: ");
scanf("%s", seats[seatNumber - 1].passengerName);
printf("请输入乘客性别 (M/F): ");
scanf(" %c", &seats[seatNumber - 1].passengerGender);
printf("订票成功\n");
}
### 7. 退票
void cancelSeat(Seat seats[], int totalSeats) {
int seatNumber;
printf("请输入要退的座位编号: ");
scanf("%d", &seatNumber);
if (seatNumber <= 0 || seatNumber > totalSeats) {
printf("座位编号无效\n");
return;
}
if (!seats[seatNumber - 1].isBooked) {
printf("该座位未被订\n");
return;
}
seats[seatNumber - 1].isBooked = 0;
strcpy(seats[seatNumber - 1].passengerName, "");
seats[seatNumber - 1].passengerGender = ' ';
printf("退票成功\n");
}
### 8. 主函数处理循环
int main() {
Seat seats[TOTAL_SEATS];
initializeSeats(seats, TOTAL_SEATS);
MenuOption option;
do {
option = getMenuOption();
switch (option) {
case SHOW_EMPTY_SEATS:
showEmptySeats(seats, TOTAL_SEATS);
break;
case SHOW_BOOKED_SEATS:
showBookedSeats(seats, TOTAL_SEATS);
break;
case BOOK_SEAT:
bookSeat(seats, TOTAL_SEATS);
break;
case CANCEL_SEAT:
cancelSeat(seats, TOTAL_SEATS);
break;
case EXIT:
printf("退出系统\n");
break;
default:
printf("无效选项,请重新选择\n");
}
} while (option != EXIT);
return 0;
}
这就是整个程序的实现和详细讲解。初始化座位后,程序通过一个循环不断显示菜单,处理用户输入,实现订票、退票和查询功能,直到用户选择退出。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |