2201_75314472 2023-04-06 10:00 采纳率: 50%
浏览 102
已结题

C语言 实现飞机订票问题

飞机订票 问题描述: 某公司每天有10航班(航班号、价格),每个航班的飞机,共有80个坐位,分20排,每排4个位子。编号为A,B,C,D。如座位号:10D表示10排D座。设计一程序,完成以下功能:

1.能从文件中导入订票信息
2.能从键盘录入订票信息,:乘客姓名,身份证 号,航班号,乘坐日期,座位号
3.能根据身份证,航班号,乘坐日期修改乘坐座位
4.能根据身份证,航班号,乘坐日期删除订票信息
5.查询指定航班,指定日期的乘客信息
6.能根据航班日期计算销售额
7.能将订票信息保存文件

  • 写回答

5条回答 默认 最新

  • 百晓生2023 2023-04-06 10:11
    关注
    该回答引用ChatGPT
    这个问题需要设计一个航班订票系统,涉及许多功能模块,下面是大致的代码实现思路,具体实现还需要自行完成。


    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #define MAX_FLIGHTS 10
    #define ROWS 20
    #define SEATS_PER_ROW 4
    #define SEATS_PER_FLIGHT (ROWS*SEATS_PER_ROW)

    // 定义订票信息的结构体
    typedef struct {
    char name[20];
    char id[20];
    char flight[10];
    char date[10];
    char seat[5];
    } Reservation;

    // 定义航班信息的结构体
    typedef struct {
    char flight[10];
    int price;
    int seats[ROWS][SEATS_PER_ROW];
    } Flight;

    // 定义全局变量
    Flight flights[MAX_FLIGHTS];
    int num_flights = 0;
    Reservation reservations[SEATS_PER_FLIGHT*MAX_FLIGHTS];
    int num_reservations = 0;

    // 从文件中导入航班信息
    void load_flights(const char *filename) {
    FILE *fp = fopen(filename, "r");
    if (fp) {
    while (num_flights < MAX_FLIGHTS && fscanf(fp, "%s %d", flights[num_flights].flight, &flights[num_flights].price) == 2) {
    for (int i = 0; i < ROWS; i++) {
    for (int j = 0; j < SEATS_PER_ROW; j++) {
    flights[num_flights].seats[i][j] = 0; // 0 表示空座位
    }
    }
    num_flights++;
    }
    fclose(fp);
    }
    }

    // 从键盘录入订票信息
    void make_reservation() {
    // 从标准输入读取订票信息,存入一个临时的 Reservation 结构体中
    Reservation temp;
    printf("Name: ");
    scanf("%s", temp.name);
    printf("ID: ");
    scanf("%s", temp.id);
    printf("Flight: ");
    scanf("%s", temp.flight);
    printf("Date: ");
    scanf("%s", temp.date);
    printf("Seat: ");
    scanf("%s", temp.seat);

    // 在航班信息中查找该航班,如果存在则更新相应的座位信息,否则返回错误信息
    int found = 0;
    for (int i = 0; i < num_flights; i++) {
    if (strcmp(flights[i].flight, temp.flight) == 0) {
    int row = temp.seat[0] - '0' - 1;
    int col = temp.seat[1] - 'A';
    if (row >= 0 && row < ROWS && col >= 0 && col < SEATS_PER_ROW && flights[i].seats[row][col] == 0) {
    flights[i].seats[row][col] = 1; // 1 表示已订票
    strcpy(reservations[num_reservations].name, temp.name);
    strcpy(reservations[num_reservations].id, temp.id);
    strcpy(reservations[num_reservations].flight, temp.flight);
    strcpy(reservations[num_reservations].date, temp.date);
    strcpy(reservations[num_reservations].seat, temp.seat);
    num_reservations++;
    found = 1;
    break;
    }
    }
    }
    if (!found) {
    printf("Error: flight not found or seat already taken.
    ");
    }
    }

    // 根据身份证,航班号,乘坐日期修改乘坐座位
    void modify_reservation() {
    char id[20], flight[10], date[10], seat[5];
    printf("ID: ");
    scanf("%s", id);
    printf("Flight: ");
    scanf("%s", flight);
    printf("Date: ");
    scanf("%s", date);
    printf("Seat: ");
    scanf("%s", seat);

    // 在订票信息中查找相应的记录,如果存在则更新相应的座位信息,否则返回错误信息
    for (int i = 0; i < num_reservations; i++) {
    if (strcmp(reservations[i].id, id) == 0 && strcmp(reservations[i].flight, flight) == 0 && strcmp(reservations[i].date, date) == 0) {
    int row = seat[0] - '0' - 1;
    int col = seat[1] - 'A';
    if (row >= 0 && row < ROWS && col >= 0 && col < SEATS_PER_ROW && flights[i].seats[row][col] == 0) {
    int old_row = reservations[i].seat[0] - '0' - 1;
    int old_col = reservations[i].seat[1] - 'A';
    flights[i].seats[old_row][old_col] = 0;
    flights[i].seats[row][col] = 1;
    strcpy(reservations[i].seat, seat);
    }
    break;
    }
    }
    }

    // 根据身份证,航班号,乘坐日期删除订票信息
    void cancel_reservation() {
    char id[20], flight[10], date[10];
    printf("ID: ");
    scanf("%s", id);
    printf("Flight: ");
    scanf("%s", flight);
    printf("Date: ");
    scanf("%s", date);

    // 在订票信息中查找相应的记录,如果存在则删除该记录,否则返回错误信息
    for (int i = 0; i < num_reservations; i++) {
    if (strcmp(reservations[i].id, id) == 0 && strcmp(reservations[i].flight, flight) == 0 && strcmp(reservations[i].date, date) == 0) {
    int row = reservations[i].seat[0] - '0' - 1;
    int col = reservations[i].seat[1] - 'A';
    flights[i].seats[row][col] = 0;
    for (int j = i; j < num_reservations-1; j++) {
    reservations[j] = reservations[j+1];
    }
    num_reservations--;
    break;
    }
    }
    }

    // 查询指定航班,指定日期的乘客信息
    void list_passengers() {
    char flight[10], date[10];
    printf("Flight: ");
    scanf("%s", flight);
    printf("Date: ");
    scanf("%s", date);

    // 在订票信息中查找相应的记录,如果存在则返回相应的乘客信息,否则返回错误信息
    printf("%-20s%-20s%-20s%-10s%-10s
    ", "Name", "ID", "Flight", "Date", "Seat");
    for (int i = 0; i < num_reservations; i++) {
    if (strcmp(reservations[i].flight, flight) == 0 && strcmp(reservations[i].date, date) == 0) {
    printf("%-20s%-20s%-20s%-10s%-10s
    ", reservations[i].name, reservations[i].id, reservations[i].flight, reservations[i].date, reservations[i].seat);
    }
    }
    }

    // 根据航班日期计算销售额
    void calculate_revenue() {
    char flight[10], date[10];
    printf("Flight: ");
    scanf("%s", flight);
    printf("Date: ");
    scanf("%s", date);

    // 在订票信息中查找相应的记录,如果存在则累加销售额,否则返回错误信息
    int revenue = 0;
    for (int i = 0; i < num_reservations; i++) {
    if (strcmp(reservations[i].flight, flight) == 0 && strcmp(reservations[i].date, date) == 0) {
    for (int j = 0; j < num_flights; j++) {
    if (strcmp(flights[j].flight, flight) == 0) {
    revenue += flights[j].price;
    break;
    }
    }
    }
    }
    printf("Revenue: %d
    ", revenue);
    }

    // 将订票信息保存到文件
    void save_reservations(const char *filename) {
    FILE *fp = fopen(filename, "w");
    if (fp) {
    for (int i = 0; i < num_reservations; i++) {
    fprintf(fp, "%s %s %s %s %s
    ", reservations[i].name, reservations[i].id, reservations[i].flight, reservations[i].date, reservations[i].seat);
    }
    fclose(fp);
    }
    }

    // 主函数
    int main() {
    // 从文件中导入航班信息
    load_flights("flights.txt");

    // 循环处理用户的输入,直到用户输入 quit
    char command[10];
    while (1) {
    printf("Enter a command (make, modify, cancel, list, revenue, save, quit): ");
    scanf("%s", command);
    if (strcmp(command, "make") == 0) {
    make_reservation();
    } else if (strcmp(command, "modify") == 0) {
    modify_reservation();
    } else if (strcmp(command, "cancel") == 0) {
    cancel_reservation();
    } else if (strcmp(command, "list") == 0) {
    list_passengers();
    } else if (strcmp(command, "revenue") == 0) {
    calculate_revenue();
    } else if (strcmp(command, "save") == 0) {
    save_reservations("reservations.txt");
    } else if (strcmp(command, "quit") == 0) {
    break;
    } else {
    printf("Invalid command.
    ");
    }
    }
    return 0;
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 4月14日
  • 已采纳回答 4月6日
  • 创建了问题 4月6日

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法