用c语言写一个简单的飞机订票系统
时间: 2023-11-25 17:04:41 浏览: 192
好的,我们先来设计一下该系统的基本功能和流程:
1. 用户进入系统后,可以选择航班的起飞和到达城市以及日期,系统需要根据输入查找可用的航班信息。
2. 系统将可用的航班信息展示给用户,用户可以选择具体的航班,并输入姓名和联系方式进行订票。
3. 系统完成订票后,需要生成订单号并向用户展示。
下面是一个简单的C语言实现代码:
```c
#include <stdio.h>
#include <string.h>
#define MAX_FLIGHTS_NUM 5
#define MAX_NAMES_NUM 10
typedef struct {
char start_city[20];
char end_city[20];
char date[20];
int available_seats;
float price;
} Flight;
typedef struct {
char name[20];
char phone[20];
int flight_id;
} Passenger;
Flight flights[MAX_FLIGHTS_NUM];
Passenger passengers[MAX_NAMES_NUM];
int num_flights = 0;
int num_passengers = 0;
void add_flight(char *start_city, char *end_city, char *date, int available_seats, float price) {
if (num_flights == MAX_FLIGHTS_NUM) {
printf("航班信息已满,不能再添加新的航班了!\n");
return;
}
strcpy(flights[num_flights].start_city, start_city);
strcpy(flights[num_flights].end_city, end_city);
strcpy(flights[num_flights].date, date);
flights[num_flights].available_seats = available_seats;
flights[num_flights].price = price;
num_flights++;
printf("航班信息添加成功!\n");
}
void show_flights(char *start_city, char *end_city, char *date) {
int i;
printf("可用航班列表:\n");
for (i = 0; i < num_flights; i++) {
Flight *flight = &flights[i];
if (strcmp(flight->start_city, start_city) == 0 && strcmp(flight->end_city, end_city) == 0 && strcmp(flight->date, date) == 0) {
printf("%d. %s->%s,日期:%s,余票:%d,价格:%.2f元\n", i+1, flight->start_city, flight->end_city, flight->date, flight->available_seats, flight->price);
}
}
}
void book_flight(int flight_id, char *name, char *phone) {
if (flight_id < 1 || flight_id > num_flights) {
printf("您选择的航班不存在,请重新输入!\n");
return;
}
Flight *flight = &flights[flight_id-1];
if (flight->available_seats == 0) {
printf("所选航班已无余票,不能完成订票!\n");
return;
}
if (num_passengers == MAX_NAMES_NUM) {
printf("订票数量已满,不能再添加新的乘客了!\n");
return;
}
strcpy(passengers[num_passengers].name, name);
strcpy(passengers[num_passengers].phone, phone);
passengers[num_passengers].flight_id = flight_id;
flight->available_seats--;
num_passengers++;
printf("订票成功,您的订单号是:%d\n", num_passengers);
}
int main() {
add_flight("北京", "上海", "2022-02-01", 100, 1000);
add_flight("上海", "北京", "2022-02-02", 80, 800);
add_flight("北京", "广州", "2022-02-03", 120, 1200);
show_flights("北京", "上海", "2022-02-01");
book_flight(1, "张三", "13800000001");
book_flight(1, "李四", "13800000002");
book_flight(2, "王五", "13800000003");
return 0;
}
```
运行结果如下:
```
航班信息添加成功!
航班信息添加成功!
航班信息添加成功!
可用航班列表:
1. 北京->上海,日期:2022-02-01,余票:100,价格:1000.00元
订票成功,您的订单号是:1
订票成功,您的订单号是:2
订票成功,您的订单号是:3
```
阅读全文