飞机订票系统 c语言代码
时间: 2023-07-31 11:04:41 浏览: 40
以下是一个简单的飞机订票系统的 C 语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SEATS 45
struct seat {
int seat_id;
char first_name[20];
char last_name[20];
};
struct seat seats[MAX_SEATS];
int menu();
void show_seats();
void add_passenger();
void delete_passenger();
int main() {
int choice;
while ((choice = menu()) != 4) {
switch (choice) {
case 1:
show_seats();
break;
case 2:
add_passenger();
break;
case 3:
delete_passenger();
break;
default:
printf("Invalid choice, please try again.\n");
break;
}
}
return 0;
}
int menu() {
int choice;
printf("\n1. Show Seats\n");
printf("2. Add Passenger\n");
printf("3. Delete Passenger\n");
printf("4. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
return choice;
}
void show_seats() {
printf("\nSeat ID\tFirst Name\tLast Name\n");
printf("---------------------------------\n");
for (int i = 0; i < MAX_SEATS; i++) {
if (seats[i].seat_id != 0) {
printf("%d\t%s\t\t%s\n", seats[i].seat_id, seats[i].first_name, seats[i].last_name);
}
}
}
void add_passenger() {
int seat_id;
printf("\nEnter Seat ID: ");
scanf("%d", &seat_id);
if (seat_id < 1 || seat_id > MAX_SEATS) {
printf("Invalid seat ID, please try again.\n");
return;
}
if (seats[seat_id - 1].seat_id != 0) {
printf("Seat already taken, please try again.\n");
return;
}
seats[seat_id - 1].seat_id = seat_id;
printf("Enter First Name: ");
scanf("%s", seats[seat_id - 1].first_name);
printf("Enter Last Name: ");
scanf("%s", seats[seat_id - 1].last_name);
printf("Passenger added successfully.\n");
}
void delete_passenger() {
int seat_id;
printf("\nEnter Seat ID: ");
scanf("%d", &seat_id);
if (seat_id < 1 || seat_id > MAX_SEATS) {
printf("Invalid seat ID, please try again.\n");
return;
}
if (seats[seat_id - 1].seat_id == 0) {
printf("Seat not taken, please try again.\n");
return;
}
seats[seat_id - 1].seat_id = 0;
strcpy(seats[seat_id - 1].first_name, "");
strcpy(seats[seat_id - 1].last_name, "");
printf("Passenger deleted successfully.\n");
}
```
这个程序中,我们定义了一个 `seat` 结构体,其中包含了座位号、名字和姓氏。我们使用一个 `seats` 数组来存储所有的座位。在 `main` 函数中,我们使用一个循环来显示菜单并等待用户输入。在用户选择不同的选项后,程序会执行相应的操作,例如显示座位、添加乘客或删除乘客。
阅读全文