C语言飞机票管理系统

文章介绍了用C语言编写的航班管理系统,包括航班信息的添加、查看、查询、修改、删除以及订票功能,所有操作涉及文件I/O和数据结构的使用。
摘要由CSDN通过智能技术生成

在这里插入图片描述
在这里插入图片描述

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

#define MAX_FLIGHTS 100
#define MAX_ORDERS 100
#define MAX_USERS 100

// 航班结构体
typedef struct {
int id;
char name[50];
char destination[50];
int seats;
} Flight;

// 订单结构体
typedef struct {
int id;
char name[50];
int flight_id;
} Order;

用户结构体
//typedef struct {
// char username[50];
// char password[50];
//} User;

Flight flights[MAX_FLIGHTS];
Order orders[MAX_ORDERS];
//User users[MAX_USERS];
int num_flights = 0;
int num_orders = 0;
int num_users = 0;

void addFlight() {
// 实现航班添加功能
// 可以使用结构体来存储航班信息
struct Flight {
char flightNo[10];
char departure[20];
char destination[20];
char date[10];
char time[6];
int price;
int seats;
} flight;

printf("请输入航班号:");
scanf("%s", flight.flightNo);
printf("请输入起点城市:");
scanf("%s", flight.departure);
printf("请输入终点城市:");
scanf("%s", flight.destination);
printf("请输入日期(格式为yyyy-mm-dd):");
scanf("%s", flight.date);
printf("请输入时间(格式为hh:mm):");
scanf("%s", flight.time);
printf("请输入票价:");
scanf("%d", &flight.price);
printf("请输入座位数:");
scanf("%d", &flight.seats);

// 将航班信息保存到文件中
FILE *fp = fopen("flights.dat", "ab");
if (fp == NULL) {
    printf("无法打开文件。\n");
    return;
}
fwrite(&flight, sizeof(flight), 1, fp);
fclose(fp);

printf("航班添加成功。\n");

}

void viewFlights() {
// 实现查看航班功能
// 从文件中读取航班信息并输出
struct Flight {
char flightNo[10];
char departure[20];
char destination[20];
char date[10];
char time[6];
int price;
int seats;
} flight;

FILE *fp = fopen("flights.dat", "rb");
if (fp == NULL) {
    printf("无法打开文件。\n");
    return;
}

printf("航班号\t起点城市\t终点城市\t日期\t时间\t票价\t座位数\n");
while (fread(&flight, sizeof(flight), 1, fp) == 1) {
    printf("%s\t%s\t%s\t%s\t%s\t%d\t%d\n", flight.flightNo, flight.departure, flight.destination, flight.date, flight.time, flight.price, flight.seats);
}

fclose(fp);

}

void searchFlight() {
// 实现查询航班功能
// 可以根据航班号或起点城市和终点城市来查询航班信息
struct Flight {
char flightNo[10];
char departure[20];
char destination[20];
char date[10];
char time[6];
int price;
int seats;
} flight;

char keyword[20];
printf("请输入航班号、起点城市或终点城市:");
scanf("%s", keyword);

FILE *fp = fopen("flights.dat", "rb");
if (fp == NULL) {
    printf("无法打开文件。\n");
    return;
}

printf("航班号\t起点城市\t终点城市\t日期\t时间\t票价\t座位数\n");
while (fread(&flight, sizeof(flight), 1, fp) == 1) {
    if (strcmp(flight.flightNo, keyword) == 0 || (strcmp(flight.departure, keyword) == 0 || strcmp(flight.destination, keyword) == 0)) {
        printf("%s\t%s\t%s\t%s\t%s\t%d\t%d\n", flight.flightNo, flight.departure, flight.destination, flight.date, flight.time, flight.price, flight.seats);
    }
}

fclose(fp);

}

void modifyFlight() {
// 实现修改航班功能
// 可以根据航班号来选择需要修改的航班信息,然后进行修改
struct Flight {
char flightNo[10];
char departure[20];
char destination[20];
char date[10];
char time[6];
int price;
int seats;
} flight;

char keyword[10];
printf("请输入需要修改的航班号:");
scanf("%s", keyword);

FILE *fp = fopen("flights.dat", "r+b");
if (fp == NULL) {
    printf("无法打开文件。\n");
    return;
}

while (fread(&flight, sizeof(flight), 1, fp) == 1) {
    if (strcmp(flight.flightNo, keyword) == 0) {
        printf("请输入新的航班号:");
        scanf("%s", flight.flightNo);
        printf("请输入新的起点城市:");
        scanf("%s", flight.departure);
        printf("请输入新的终点城市:");
        scanf("%s", flight.destination);
        printf("请输入新的日期(格式为yyyy-mm-dd):");
        scanf("%s", flight.date);
        printf("请输入新的时间(格式为hh:mm):");
        scanf("%s", flight.time);
        printf("请输入新的票价:");
        scanf("%d", &flight.price);
        printf("请输入新的座位数:");
        scanf("%d", &flight.seats);

        fseek(fp, -sizeof(flight), SEEK_CUR);
        fwrite(&flight, sizeof(flight), 1, fp);

        printf("航班信息修改成功。\n");
        break;
    }
}

fclose(fp);

}

void deleteFlight() {
// 实现删除航班功能
// 可以根据航班号来选择需要删除的航班信息
struct Flight {
char flightNo[10];
char departure[20];
char destination[20];
char date[10];
char time[6];
int price;
int seats;
} flight;

char keyword[10];
printf("请输入需要删除的航班号:");
scanf("%s", keyword);

FILE *fp = fopen("flights.dat", "rb");
if (fp == NULL) {
    printf("无法打开文件。\n");
    return;
}

FILE *temp = fopen("temp.dat", "wb");
if (temp == NULL) {
    printf("无法创建临时文件。\n");
    fclose(fp);
    return;
}

int found = 0;
while (fread(&flight, sizeof(flight), 1, fp) == 1) {
    if (strcmp(flight.flightNo, keyword) != 0) {
        fwrite(&flight, sizeof(flight), 1, temp);
    } else {
        found = 1;
    }
}

fclose(fp);
fclose(temp);

if (found) {
    remove("flights.dat");
    rename("temp.dat", "flights.dat");
    printf("航班信息删除成功。\n");
} else {
    remove("temp.dat");
    printf("未找到指定的航班信息,删除失败。\n");
}

}

void bookTicket() {
// 实现订票功能
// 可以根据航班号和乘客信息来进行订票操作
struct Flight {
char flightNo[10];
char departure[20];
char destination[20];
char date[10];
char time[6];
int price;
int seats;
} flight;

char keyword[10];
printf("请输入需要预订的航班号:");
scanf("%s", keyword);

FILE *fp = fopen("flights.dat", "r+b");
if (fp == NULL) {
    printf("无法打开文件。\n");
    return;
}

while (fread(&flight, sizeof(flight), 1, fp) == 1) {
    if (strcmp(flight.flightNo, keyword) == 0) {
        if (flight.seats == 0) {
            printf("很抱歉,该航班已经售罄。\n");
            fclose(fp);
            return;
        }

        printf("请输入您的姓名:");
        char name[20];
        scanf("%s", name);
        printf("请输入您的电话号码:");
        char phone[20];
        scanf("%s", phone);

        // 将订单信息保存到文件中
        FILE *order = fopen("orders.dat", "ab");
        if (order == NULL) {
            printf("无法打开文件。\n");
            fclose(fp);
            return;
        }
        fprintf(order, "%s %s %s %s %s %d\n", flight.flightNo, flight.date, flight.time, name, phone, flight.price);
        fclose(order);

        // 更新航班信息
        flight.seats--;
        fseek(fp, -sizeof(flight), SEEK_CUR);
        fwrite(&flight, sizeof(flight), 1, fp);

        printf("订票成功,订单已保存。\n");
        fclose(fp);
        return;
    }
}

printf("未找到指定的航班信息,订票失败。\n");
fclose(fp);

}

  • 36
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值