c语言订票系统 任务:通过此系统可以实现如下功能:录入(可以录入航班情况)、查询(可以查询某个航线的情况;可以输入起飞抵达城市,查询飞机航班情况)、订票(可以订票,如果该航班已经无票,可以提供相关可选择航班)、退票(可退票,退票后修改相关数据文件)、修改航班信息(当航班信息改变可以修改航班数据文件)
时间: 2023-05-26 20:05:45 浏览: 52
以下是一个简单的C语言订票系统,包含录入、查询、订票、退票、修改航班信息等功能。
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct flight {
char flight_num[20]; // 航班号
char departure[20]; // 起飞城市
char destination[20]; // 目的地城市
char date[20]; // 起飞日期
int total_seats; // 总座位数
int booked_seats; // 已预订的座位数
float price; // 票价
};
void print_flight(struct flight f) {
printf("航班号:%s\n", f.flight_num);
printf("起飞城市:%s\n", f.departure);
printf("目的地城市:%s\n", f.destination);
printf("起飞日期:%s\n", f.date);
printf("总座位数:%d\n", f.total_seats);
printf("已预订座位数:%d\n", f.booked_seats);
printf("票价:%f\n", f.price);
printf("\n");
}
void input_flight(struct flight *f) {
printf("请输入航班号:");
scanf("%s", f->flight_num);
printf("请输入起飞城市:");
scanf("%s", f->departure);
printf("请输入目的地城市:");
scanf("%s", f->destination);
printf("请输入起飞日期:");
scanf("%s", f->date);
printf("请输入总座位数:");
scanf("%d", &f->total_seats);
printf("请输入票价:");
scanf("%f", &f->price);
f->booked_seats = 0;
}
void save_flight(struct flight f) {
FILE *fp = fopen("flights.dat", "ab");
fwrite(&f, sizeof(struct flight), 1, fp);
fclose(fp);
}
void list_flights() {
FILE *fp = fopen("flights.dat", "rb");
struct flight f;
while (fread(&f, sizeof(struct flight), 1, fp) == 1) {
print_flight(f);
}
fclose(fp);
}
void search_flight(char departure[20], char destination[20], char date[20]) {
FILE *fp = fopen("flights.dat", "rb");
struct flight f;
int found = 0;
while (fread(&f, sizeof(struct flight), 1, fp) == 1) {
if (strcmp(f.departure, departure) == 0 &&
strcmp(f.destination, destination) == 0 &&
strcmp(f.date, date) == 0) {
print_flight(f);
found = 1;
}
}
fclose(fp);
if (!found) {
printf("没有找到匹配的航班。\n");
}
}
void book_flight(char flight_num[20]) {
FILE *fp = fopen("flights.dat", "r+b");
struct flight f;
int found = 0;
while (fread(&f, sizeof(struct flight), 1, fp) == 1) {
if (strcmp(f.flight_num, flight_num) == 0) {
if (f.booked_seats < f.total_seats) {
f.booked_seats++;
fseek(fp, -sizeof(struct flight), SEEK_CUR);
fwrite(&f, sizeof(struct flight), 1, fp);
printf("订票成功!\n");
found = 1;
} else {
printf("该航班已经无票,可选择以下航班:\n");
list_flights();
}
break;
}
}
fclose(fp);
if (!found) {
printf("没有找到匹配的航班。\n");
}
}
void cancel_flight(char flight_num[20]) {
FILE *fp = fopen("flights.dat", "r+b");
struct flight f;
int found = 0;
while (fread(&f, sizeof(struct flight), 1, fp) == 1) {
if (strcmp(f.flight_num, flight_num) == 0) {
if (f.booked_seats > 0) {
f.booked_seats--;
fseek(fp, -sizeof(struct flight), SEEK_CUR);
fwrite(&f, sizeof(struct flight), 1, fp);
printf("退票成功!\n");
found = 1;
} else {
printf("该航班当前没有已订票,无法退票。\n");
}
break;
}
}
fclose(fp);
if (!found) {
printf("没有找到匹配的航班。\n");
}
}
void modify_flight(char flight_num[20]) {
printf("请输入要修改的航班信息:\n");
struct flight f;
input_flight(&f);
FILE *fp = fopen("flights.dat", "r+b");
struct flight f_old;
int found = 0;
while (fread(&f_old, sizeof(struct flight), 1, fp) == 1) {
if (strcmp(f_old.flight_num, flight_num) == 0) {
fseek(fp, -sizeof(struct flight), SEEK_CUR);
fwrite(&f, sizeof(struct flight), 1, fp);
printf("修改成功!\n");
found = 1;
break;
}
}
fclose(fp);
if (!found) {
printf("没有找到匹配的航班。\n");
}
}
int main() {
while (1) {
printf("请输入操作编号:\n");
printf("1. 录入航班信息\n");
printf("2. 查询航班信息\n");
printf("3. 订票\n");
printf("4. 退票\n");
printf("5. 修改航班信息\n");
printf("6. 退出系统\n");
int op;
scanf("%d", &op);
switch (op) {
case 1: {
struct flight f;
input_flight(&f);
save_flight(f);
printf("保存成功!\n");
break;
}
case 2: {
printf("请输入起飞城市、目的地城市和起飞日期:\n");
char departure[20], destination[20], date[20];
scanf("%s", departure);
scanf("%s", destination);
scanf("%s", date);
search_flight(departure, destination, date);
break;
}
case 3: {
printf("请输入要订票的航班号:\n");
char flight_num[20];
scanf("%s", flight_num);
book_flight(flight_num);
break;
}
case 4: {
printf("请输入要退票的航班号:\n");
char flight_num[20];
scanf("%s", flight_num);
cancel_flight(flight_num);
break;
}
case 5: {
printf("请输入要修改的航班号:\n");
char flight_num[20];
scanf("%s", flight_num);
modify_flight(flight_num);
break;
}
case 6: {
exit(0);
}
}
}
return 0;
}
```
该程序中使用了结构体来表示航班信息,同时通过文件读写来实现航班信息的录入、保存和查询等功能。在订票和退票功能中,还需要修改航班文件中的已预订座位数。另外,为了使程序更加灵活,每次操作完成后都会回到菜单界面让用户选择下一步操作。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)