#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);
}