用 C语言编写软件,完成以下任务: (1)系统用户界面菜单中至少应包括“信息录入”“显示航线信息”、“退票”“退出”等4项;(2)将每条航线的有关信息存入 fight.txt,如:终点站名、航班号、飞机号,飞行周日(星期几)、时间、乘员定额、余票量、票价,已定票客户名单和等候替补客户名单(包括姓名,所需票数), (3)显示航线信息, (4)退票:根据客户提供的情况(日期、航班),为客户办理退票手续,然后查询该航班是否有人排队候补,首先询问排在第一的客户,若退票额满足客户要求,则为他办理订票手续,否则依次询问其他候补客户。(5)退出程序。
时间: 2024-02-15 20:04:12 浏览: 161
好的,这是一个简单的航班管理系统,以下是代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_FLIGHTS 100
#define MAX_PASSENGERS 1000
// 航线结构体
typedef struct {
char dest[20]; // 终点站名
char flight_no[10]; // 航班号
char plane_no[10]; // 飞机号
char week_day[10]; // 飞行周日(星期几)
char time[10]; // 时间
int capacity; // 乘员定额
int remain_tickets; // 余票量
float price; // 票价
char passengers[MAX_PASSENGERS][20]; // 已定票客户名单
char waiting_list[MAX_PASSENGERS][20]; // 等候替补客户名单
} Flight;
// 航线数组
Flight flights[MAX_FLIGHTS];
int num_flights = 0; // 航线数量
// 信息录入
void add_flight() {
if (num_flights >= MAX_FLIGHTS) {
printf("航线数量已达上限,无法添加新航线!\n");
return;
}
Flight *flight = &flights[num_flights++];
printf("请输入终点站名:");
scanf("%s", flight->dest);
printf("请输入航班号:");
scanf("%s", flight->flight_no);
printf("请输入飞机号:");
scanf("%s", flight->plane_no);
printf("请输入飞行周日(星期几):");
scanf("%s", flight->week_day);
printf("请输入时间:");
scanf("%s", flight->time);
printf("请输入乘员定额:");
scanf("%d", &flight->capacity);
printf("请输入余票量:");
scanf("%d", &flight->remain_tickets);
printf("请输入票价:");
scanf("%f", &flight->price);
}
// 显示航线信息
void show_flights() {
if (num_flights == 0) {
printf("暂无航线信息!\n");
return;
}
printf("终点站名\t航班号\t飞机号\t飞行周日\t时间\t乘员定额\t余票量\t票价\n");
for (int i = 0; i < num_flights; i++) {
Flight *flight = &flights[i];
printf("%s\t%s\t%s\t%s\t%s\t%d\t%d\t%.2f\n", flight->dest, flight->flight_no, flight->plane_no,
flight->week_day, flight->time, flight->capacity, flight->remain_tickets, flight->price);
}
}
// 退票
void refund_ticket() {
char flight_no[10], name[20];
printf("请输入航班号:");
scanf("%s", flight_no);
printf("请输入客户姓名:");
scanf("%s", name);
// 查找航班
int index = -1;
for (int i = 0; i < num_flights; i++) {
if (strcmp(flights[i].flight_no, flight_no) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("未找到航班号为%s的航班!\n", flight_no);
return;
}
Flight *flight = &flights[index];
// 查找客户
int passenger_index = -1;
for (int i = 0; i < MAX_PASSENGERS; i++) {
if (strcmp(flight->passengers[i], name) == 0) {
passenger_index = i;
break;
}
}
if (passenger_index == -1) {
printf("未找到姓名为%s的客户!\n", name);
return;
}
// 退票
strcpy(flight->passengers[passenger_index], "");
flight->remain_tickets++;
// 查找等候替补客户
int waiting_index = -1;
for (int i = 0; i < MAX_PASSENGERS; i++) {
if (strcmp(flight->waiting_list[i], "") != 0) {
waiting_index = i;
break;
}
}
if (waiting_index == -1) {
printf("未找到等候替补客户!\n");
return;
}
// 询问等候替补客户
for (int i = waiting_index; i < MAX_PASSENGERS; i++) {
char answer;
printf("航班%s有一张退票,您是否需要订票?(y/n)", flight_no);
scanf(" %c", &answer);
if (answer == 'y') {
printf("请输入您的姓名:");
scanf("%s", flight->waiting_list[i]);
printf("请输入订票数量:");
int tickets;
scanf("%d", &tickets);
if (tickets <= flight->remain_tickets) {
flight->remain_tickets -= tickets;
printf("订票成功!\n");
break;
} else {
printf("余票不足,无法订票!\n");
}
} else {
printf("感谢您的关注,下次再见!\n");
break;
}
}
}
// 退出程序
void exit_program() {
// 将航线信息写入文件
FILE *fp = fopen("flight.txt", "w");
if (fp != NULL) {
for (int i = 0; i < num_flights; i++) {
Flight *flight = &flights[i];
fprintf(fp, "%s %s %s %s %s %d %d %.2f\n", flight->dest, flight->flight_no, flight->plane_no,
flight->week_day, flight->time, flight->capacity, flight->remain_tickets, flight->price);
}
fclose(fp);
}
printf("感谢使用本系统,再见!\n");
exit(0);
}
// 加载航线信息
void load_flights() {
FILE *fp = fopen("flight.txt", "r");
if (fp != NULL) {
while (!feof(fp)) {
Flight *flight = &flights[num_flights++];
fscanf(fp, "%s %s %s %s %s %d %d %f", flight->dest, flight->flight_no, flight->plane_no,
flight->week_day, flight->time, &flight->capacity, &flight->remain_tickets, &flight->price);
}
fclose(fp);
}
}
int main() {
// 加载航线信息
load_flights();
while (1) {
printf("请选择操作:\n");
printf("1. 信息录入\n");
printf("2. 显示航线信息\n");
printf("3. 退票\n");
printf("4. 退出\n");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
add_flight();
break;
case 2:
show_flights();
break;
case 3:
refund_ticket();
break;
case 4:
exit_program();
break;
default:
printf("输入无效,请重新输入!\n");
break;
}
}
}
```
注意,这只是一个简单的示例,还有很多细节需要完善,比如输入的合法性检查、数据格式化等。编写软件时应该根据实际需求进行完善。
阅读全文