飞机订票系统 c语言代码
时间: 2023-07-31 19:08:07 浏览: 37
模拟飞机票订票系统c语言源代码
5星 · 资源好评率100%
以下是一个简单的飞机订票系统的C语言代码示例:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SEATS 100
struct Seat {
int seat_num;
char passenger_name[30];
int is_booked;
};
struct Seat seats[MAX_SEATS];
void print_menu() {
printf("\n\n");
printf("Airline Ticket Booking System\n");
printf("----------------------------\n");
printf("1. View available seats\n");
printf("2. Book a seat\n");
printf("3. Cancel a booking\n");
printf("4. Exit\n");
}
void view_seats() {
printf("Available seats:\n");
for (int i = 0; i < MAX_SEATS; i++) {
if (!seats[i].is_booked) {
printf("Seat %d: Available\n", seats[i].seat_num);
}
}
}
void book_seat() {
int seat_num;
printf("Enter seat number to book: ");
scanf("%d", &seat_num);
if (seats[seat_num-1].is_booked) {
printf("Seat %d is already booked.\n", seat_num);
return;
}
char name[30];
printf("Enter passenger name: ");
scanf("%s", name);
seats[seat_num-1].seat_num = seat_num;
strcpy(seats[seat_num-1].passenger_name, name);
seats[seat_num-1].is_booked = 1;
printf("Seat %d successfully booked for %s.\n", seat_num, name);
}
void cancel_booking() {
int seat_num;
printf("Enter seat number to cancel booking: ");
scanf("%d", &seat_num);
if (!seats[seat_num-1].is_booked) {
printf("Seat %d is not booked.\n", seat_num);
return;
}
seats[seat_num-1].is_booked = 0;
printf("Booking for seat %d cancelled.\n", seat_num);
}
int main() {
for (int i = 0; i < MAX_SEATS; i++) {
seats[i].seat_num = i+1;
seats[i].is_booked = 0;
}
int choice;
do {
print_menu();
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
view_seats();
break;
case 2:
book_seat();
break;
case 3:
cancel_booking();
break;
case 4:
printf("Exiting program.\n");
return 0;
default:
printf("Invalid choice.\n");
}
} while (1);
return 0;
}
```
这个系统可以显示可用座位,预订座位和取消预订,并使用结构体来表示座位信息。请注意,这只是一个简单的示例,并且可能需要进行更多的错误检查和输入验证。
阅读全文