详细写出普通用户功能的代码
时间: 2024-03-23 11:38:03 浏览: 22
好的,以下是普通用户功能的代码示例:
```c
#include <stdio.h>
#include <string.h>
typedef struct FlightInfo {
char flight_no[20]; // 航班号
int seat_num; // 座位数
char destination[20]; // 终点站
char departure_time[20]; // 起飞时间
int remain_seat; // 剩余座位数
} FlightInfo;
typedef struct Order {
char flight_no[20]; // 航班号
int ticket_num; // 票数
} Order;
int main() {
// 读取航班信息文件,并存储到数组中
FlightInfo flights[100];
int flight_num = 0;
FILE *fp = fopen("flights.dat", "rb");
if (fp != NULL) {
while (fread(&flights[flight_num], sizeof(FlightInfo), 1, fp) == 1) {
flight_num++;
}
fclose(fp);
}
// 读取订单信息文件,并存储到数组中
Order orders[100];
int order_num = 0;
char username[20]; // 用户名
printf("请输入用户名:");
scanf("%s", username);
char order_file_name[30];
sprintf(order_file_name, "%s.dat", username);
fp = fopen(order_file_name, "rb");
if (fp != NULL) {
while (fread(&orders[order_num], sizeof(Order), 1, fp) == 1) {
order_num++;
}
fclose(fp);
}
int choice;
do {
printf("\n1.列出所有航班\n2.查询航班余票\n3.订票\n4.退订\n5.列出我的订单\n6.退出\n");
printf("请输入选项:");
scanf("%d", &choice);
switch (choice) {
case 1:
// 列出所有航班信息
printf("航班号\t座位数\t终点站\t起飞时间\t余票\n");
for (int i = 0; i < flight_num; i++) {
printf("%s\t%d\t%s\t%s\t%d\n", flights[i].flight_no, flights[i].seat_num, flights[i].destination, flights[i].departure_time, flights[i].remain_seat);
}
break;
case 2:
// 查询航班余票
printf("按航班号查询请输入1,按终点站查询请输入2:");
int query_choice;
scanf("%d", &query_choice);
if (query_choice == 1) {
char flight_no[20];
printf("请输入航班号:");
scanf("%s", flight_no);
for (int i = 0; i < flight_num; i++) {
if (strcmp(flights[i].flight_no, flight_no) == 0) {
printf("航班号:%s\n终点站:%s\n起飞时间:%s\n余票:%d\n", flights[i].flight_no, flights[i].destination, flights[i].departure_time, flights[i].remain_seat);
break;
}
}
} else if (query_choice == 2) {
char destination[20];
printf("请输入终点站:");
scanf("%s", destination);
for (int i = 0; i < flight_num; i++) {
if (strcmp(flights[i].destination, destination) == 0) {
printf("航班号:%s\n终点站:%s\n起飞时间:%s\n余票:%d\n", flights[i].flight_no, flights[i].destination, flights[i].departure_time, flights[i].remain_seat);
}
}
}
break;
case 3:
// 订票
char flight_no[20];
printf("请输入航班号:");
scanf("%s", flight_no);
int ticket_num;
printf("请输入票数:");
scanf("%d", &ticket_num);
for (int i = 0; i < flight_num; i++) {
if (strcmp(flights[i].flight_no, flight_no) == 0) {
if (flights[i].remain_seat >= ticket_num) {
// 更新航班信息
flights[i].remain_seat -= ticket_num;
// 更新订单信息
strcpy(orders[order_num].flight_no, flight_no);
orders[order_num].ticket_num = ticket_num;
order_num++;
// 更新订单文件
fp = fopen(order_file_name, "wb");
fwrite(orders, sizeof(Order), order_num, fp);
fclose(fp);
printf("订票成功!\n");
} else {
printf("余票不足,订票失败!\n");
}
break;
}
}
break;
case 4:
// 退订
printf("请输入要退订的航班号:");
scanf("%s", flight_no);
for (int i = 0; i < order_num; i++) {
if (strcmp(orders[i].flight_no, flight_no) == 0) {
int refund_num;
printf("请输入退票数目:");
scanf("%d", &refund_num);
if (refund_num <= orders[i].ticket_num) {
// 更新航班信息
for (int j = 0; j < flight_num; j++) {
if (strcmp(flights[j].flight_no, flight_no) == 0) {
flights[j].remain_seat += refund_num;
break;
}
}
// 更新订单信息
orders[i].ticket_num -= refund_num;
if (orders[i].ticket_num == 0) {
for (int j = i; j < order_num - 1; j++) {
orders[j] = orders[j + 1];
}
order_num--;
}
// 更新订单文件
fp = fopen(order_file_name, "wb");
fwrite(orders, sizeof(Order), order_num, fp);
fclose(fp);
printf("退订成功!\n");
} else {
printf("退票数目超过已订票数,退订失败!\n");
}
break;
}
}
break;
case 5:
// 列出我的订单
printf("航班号\t票数\n");
for (int i = 0; i < order_num; i++) {
printf("%s\t%d\n", orders[i].flight_no, orders[i].ticket_num);
}
break;
case 6:
// 退出
break;
default:
printf("选项无效,请重新输入!\n");
break;
}
} while (choice != 6);
// 更新航班信息文件
fp = fopen("flights.dat", "wb");
fwrite(flights, sizeof(FlightInfo), flight_num, fp);
fclose(fp);
return 0;
}
```
这段代码实现了普通用户的所有功能,包括列出所有航班信息、查询航班余票、订票、退订、列出我的订单。其中读取航班信息和订单信息使用了文件存储,每个用户有一个单独的订单信息文件。订票和退订时需要更新航班信息和订单信息,并将更新后的订单信息写入文件中。最后需要更新航班信息文件,以便下次使用。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)