航班订票系统中订票,退票功能,C语言代码
时间: 2023-08-18 10:41:32 浏览: 69
以下是一个简单的航班订票系统的C语言代码,包括订票和退票功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_FLIGHTS 10
#define MAX_NAME_LEN 20
typedef struct {
char name[MAX_NAME_LEN];
int seats;
int booked;
} flight;
flight flights[MAX_FLIGHTS] = {
{"Flight 1", 50, 0},
{"Flight 2", 100, 0},
{"Flight 3", 75, 0},
{"Flight 4", 25, 0},
{"Flight 5", 200, 0},
{"Flight 6", 150, 0},
{"Flight 7", 50, 0},
{"Flight 8", 100, 0},
{"Flight 9", 75, 0},
{"Flight 10", 25, 0}
};
void display_flights() {
printf("Flights available:\n");
printf("Name\tSeats\tBooked\n");
for(int i=0; i<MAX_FLIGHTS; i++) {
printf("%s\t%d\t%d\n", flights[i].name, flights[i].seats, flights[i].booked);
}
printf("\n");
}
void book_flight() {
char name[MAX_NAME_LEN];
int num_seats;
int found = 0;
int index = 0;
printf("Enter flight name: ");
scanf("%s", name);
for(int i=0; i<MAX_FLIGHTS; i++) {
if(strcmp(flights[i].name, name) == 0) {
found = 1;
index = i;
break;
}
}
if(found == 0) {
printf("Flight not found!\n");
return;
}
printf("Enter number of seats to book: ");
scanf("%d", &num_seats);
if(num_seats > flights[index].seats - flights[index].booked) {
printf("Not enough seats available!\n");
return;
}
flights[index].booked += num_seats;
printf("Booking confirmed for %d seats on flight %s.\n", num_seats, name);
}
void cancel_flight() {
char name[MAX_NAME_LEN];
int num_seats;
int found = 0;
int index = 0;
printf("Enter flight name: ");
scanf("%s", name);
for(int i=0; i<MAX_FLIGHTS; i++) {
if(strcmp(flights[i].name, name) == 0) {
found = 1;
index = i;
break;
}
}
if(found == 0) {
printf("Flight not found!\n");
return;
}
printf("Enter number of seats to cancel: ");
scanf("%d", &num_seats);
if(num_seats > flights[index].booked) {
printf("Invalid number of seats to cancel!\n");
return;
}
flights[index].booked -= num_seats;
printf("Cancellation confirmed for %d seats on flight %s.\n", num_seats, name);
}
int main() {
int choice;
do {
printf("Choose an option:\n");
printf("1. Display flights\n");
printf("2. Book a flight\n");
printf("3. Cancel a booking\n");
printf("4. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
display_flights();
break;
case 2:
book_flight();
break;
case 3:
cancel_flight();
break;
case 4:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice!\n");
}
} while(choice != 4);
return 0;
}
```
在这个代码中,我们定义了一个 `flight` 结构体来表示每个航班的名称、座位数和已预订座位数。我们还定义了一个 `flights` 数组来存储所有航班的信息。然后我们实现了三个函数来显示所有航班信息、订票和取消订票。在 `main` 函数中,我们使用一个循环来让用户选择不同的功能,直到用户选择退出程序。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)