飞机订票系统(数据结构课设论文)

1、创建一个数组,指定所有的座位,4*20个座位,0和3为靠窗,1和2为过道和一条链表记录所有已售机票(5分)
2、选座系统,打印出所有的座位,已选的和未选的(8分)
3、登录窗口分为管理员(机票的登记,座位状态修改)和用户(查看自己机票信息)(10分)
4、数据进行文件保存,每次打开读取上次保存的数据(5分)


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

#define ROWS 4
#define COLS 20

int seats[ROWS][COLS]; // 0和3为靠窗,1和2为过道
int sold_seats = 0; // 已售机票数

// 定义链表节点结构体
struct Node {
    int row;
    int col;
    struct Node *next;
};

struct Node *sold_list = NULL; // 已售机票的链表头指针

// 初始化座位
void init_seats() {
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            if (j == 0 || j == COLS - 1) {
                seats[i][j] = (i == 0 || i == ROWS - 1) ? 0 : 3; // 靠窗
            } else {
                seats[i][j] = (i == 1 || i == 2) ? 1 : 2; // 过道
            }
        }
    }
}

// 添加已售机票到链表中
void add_sold_seat(int row, int col) {
    struct Node *p = (struct Node *)malloc(sizeof(struct Node));
    p->row = row;
    p->col = col;
    p->next = sold_list;
    sold_list = p;
    sold_seats++;
}

// 删除已售机票链表中的指定节点
void delete_sold_seat(int row, int col) {
    struct Node *p = sold_list;
    struct Node *prev = NULL;
    while (p != NULL) {
        if (p->row == row && p->col == col) {
            if (prev == NULL) {
                sold_list = p->next;
            } else {
                prev->next = p->next;
            }
            free(p);
            sold_seats--;
            return;
        }
        prev = p;
        p = p->next;
    }
}

// 打印所有座位及其状态
void print_seats() {
    printf("    A B C D E F G H I J K L M N O P Q R S T\n");
    for (int i = 0; i < ROWS; i++) {
        printf("%2d ", i + 1);
        for (int j = 0; j < COLS; j++) {
            if (seats[i][j] == 0) {
                printf("X "); // 已售座位
            } else if (seats[i][j] == 3) {
                printf("W "); // 靠窗座位
            } else {
                printf("_ "); // 未售座位和过道座位
            }
        }
        printf("\n");
    }
}

// 选择座位
void select_seat() {
    int row, col;
    printf("Please enter the row number: ");
    scanf("%d", &row);
    printf("Please enter the column letter (A-T): ");
    char col_letter;
    scanf(" %c", &col_letter);
    col = col_letter - 'A';
    if (row < 1 || row > ROWS || col < 0 || col >= COLS) {
        printf("Invalid seat number.\n");
        return;
    }
    if (seats[row - 1][col] == 0) {
        printf("The seat is already sold.\n");
        return;
    }
    add_sold_seat(row - 1, col);
    seats[row - 1][col] = 0;
    printf("The seat is successfully sold.\n");
}

// 取消座位
void cancel_seat() {
    int row, col;
    printf("Please enter the row number: ");
    scanf("%d", &row);
    printf("Please enter the column letter (A-T): ");
    char col_letter;
    scanf(" %c", &col_letter);
    col = col_letter - 'A';
    if (row < 1 || row > ROWS || col < 0 || col >= COLS) {
        printf("Invalid seat number.\n");
        return;
    }
    if (seats[row - 1][col] != 0) {
        printf("Theseat is not sold.\n");
        return;
    }
    delete_sold_seat(row - 1, col);
    seats[row - 1][col] = (col == 0 || col == COLS - 1) ? 3 : 2; // 靠窗或过道
    printf("The seat is successfully canceled.\n");
}

// 登录
void login() {
    int role;
    printf("Please choose your role:\n");
    printf("1. Administrator\n");
    printf("2. User\n");
    scanf("%d", &role);
    if (role == 1) {
        // 管理员
        int choice;
        do {
            printf("Please choose operation:\n");
            printf("1. Register ticket\n");
            printf("2. Change seat status\n");
            printf("3. Quit\n");
            scanf("%d", &choice);
            switch (choice) {
                case 1: {
                    select_seat();
                    break;
                }
                case 2: {
                    cancel_seat();
                    break;
                }
                case 3: {
                    break;
                }
                default: {
                    printf("Invalid choice.\n");
                    break;
                }
            }
            print_seats();
        } while (choice != 3);
    } else if (role == 2) {
        // 用户
        int row, col;
        printf("Please enter the row number: ");
        scanf("%d", &row);
        printf("Please enter the column letter (A-T): ");
        char col_letter;
        scanf(" %c", &col_letter);
        col = col_letter - 'A';
        if (row < 1 || row > ROWS || col < 0 || col >= COLS) {
            printf("Invalid seat number.\n");
            return;
        }
        struct Node *p = sold_list;
        while (p != NULL) {
            if (p->row == row - 1 && p->col == col) {
                printf("You have booked this seat.\n");
                return;
            }
            p = p->next;
        }
        printf("This seat is available.\n");
    } else {
        printf("Invalid role.\n");
    }
}

// 保存数据到文件
void save_data() {
    FILE *fp = fopen("data.txt", "w");
    fprintf(fp, "%d\n", sold_seats);
    struct Node *p = sold_list;
    while (p != NULL) {
        fprintf(fp, "%d %d\n", p->row, p->col);
        p = p->next;
    }
    fclose(fp);
}

// 从文件中读取数据
void load_data() {
    FILE *fp = fopen("data.txt", "r");
    if (fp == NULL) {
        return;
    }
    fscanf(fp, "%d", &sold_seats);
    for (int i = 0; i < sold_seats; i++) {
        int row, col;
        fscanf(fp, "%d %d", &row, &col);
        add_sold_seat(row, col);
        seats[row][col] = 0;
    }
    fclose(fp);
}

int main() {
    init_seats();
    load_data();

    int choice;
    do {
        printf("Please choose operation:\n");
        printf("1. Print seats\n");
        printf("2. Login\n");
        printf("3. Quit\n");
        scanf("%d", &choice);
        switch (choice) {
            case 1: {
                print_seats();
                break;
            }
            case 2: {
                login();
                break;
            }
            case 3: {
                break;
            }
            default: {
                printf("Invalid choice.\n");
                break;
            }
        }
    } while (choice != 3);

    save_data();

    return 0;
}