#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>
#include <time.h>
//航空初始界面
void welcome();
//管理系统密码
void keymenu();
//管理系统
void menu();
//乘客系统
void passenger();
//重置
void fileinit();
//添加
void add();
//删除航班
void delet();
void delflight(int id);
//修改航班
void update();
void updateflight(int id);
//查询
void find();
//排序函数
int compareFlights();
//浏览
void checkall();
//界面颜色
void color();
//退出界面
void checkout();
//是否起飞
int isFly(int id);
//订票
void orderTicket();
void order();
//退票
void returnTicket();
void returned();
//返回主菜单
void returnMain();
//定义结构体存储航班信息
struct flight {
int id; //班次
int startHour;//起飞时间
int startMinute;
char start[20];//起点站
char end[20];//终点站
int time;//飞行时间
int capacity; //额定载客量
int people;
char isFly[10];//是否起飞
};
//定义结构体实现链表
struct node {
struct flight data;
struct node* nxt;
};
int isManager;
int main() {
welcome();
system("cls");
return 0;
}
//航空初始界面
void welcome() {
printf(" _________________ ____ ______________\n");
printf("/ _____/\\______ \\ | | \\__ ___/\n");
printf("\\_____ \\ | | \\| | / | | \n");
printf("/ \\ | ` \\ | / | | \n");
printf("/_______ //_______ /______/ |____| \n");
printf(" \\/ \\/ \n");
printf("*****************************************\n");
printf("* 欢迎使用 *\n");
printf("* 航空 系统 *\n");
printf("* 1(管理员)2(乘客) *\n");
printf("*****************************************\n");
scanf("%d", &isManager);
if (isManager == 1) {
keymenu();
}
else {
passenger();
}
system("pause");
system("cls");
}
void keymenu()
{
while (1)
{
char a[20]; // 用来存储用户输入的密码
printf("请输入管理系统密码:");
scanf("%s", a); // 获取用户输入的密码并存储到数组a中
// 比较输入的密码与正确密码"654321"
if (strcmp(a, "654321") == 0) {
printf("密码正确,进入系统。\n");
break; // 密码正确,退出循环
}
else {
printf("密码错误,请重新输入密码。\n");
}
}
menu();
}
//管理系统
void menu() {
system("cls");
printf("********航空管理系统********\n");
printf("********系统菜单如下********\n");
printf("*****1【重置数据文件】******\n");
printf("*****2【添加 航班】******\n");
printf("*****3【删除 航班】******\n");
printf("*****4【修改 航班】******\n");
printf("*****5【航班信息查询】******\n");
printf("*****6【航班信息浏览】******\n");
printf("*****7【系统颜色设置】******\n");
printf("*****8【应用程序退出】******\n");
printf("****请选择您要选择的菜单****\n");
int key;
while (scanf("%d", &key) != EOF) {
getchar(); //输入回车
if (key == 1) {
fileinit();
}
else if (key == 2) {
add();
}
else if (key == 3) {
delet();
}
else if (key == 4) {
update();
}
else if (key == 5) {
find();
}
else if (key == 6) {
checkall();
}
else if (key == 7) {
color();
}
else if (key == 8) {
checkout();
}
system("cls");
menu();
}
system("cls");
}
//乘客系统
void passenger() {
system("cls");
printf("********系统菜单如下********\n");
printf("*****1【订 票】******\n");
printf("*****2【退 票】******\n");
printf("*****3【航班信息浏览】******\n");
printf("*****4【应用程序退出】******\n");
printf("****请选择您要选择的菜单****\n");
int key;
while (scanf("%d", &key) != EOF) {
getchar(); //输入回车
if (key == 1) {
order();
}
else if (key == 2) {
returned();
}
else if (key == 3) {
checkall();
}
else if (key == 4) {
checkout();
}
system("cls");
passenger();
}
}
//删除航班
void delflight(int id) {
system("cls");
FILE* file;
struct node* head = NULL, * current = NULL, * pre = NULL;
struct flight p;
int size;
file = fopen("flight.data", "r");
if (!file) {
printf("Failed to open file");
return;
}
while ((size = fread(&p, sizeof(struct flight), 1, file)) == 1) {
if (p.id != id) {
struct node* newNode = new node;
newNode->data = p;
newNode->nxt = NULL;
if (!head) {
head = newNode;
current = head;
}
else {
current->nxt = newNode;
current = newNode;
}
}
}
fclose(file);
file = fopen("flight.data", "w");
fclose(file);
file = fopen("flight.data", "a");
if (!file) {
printf("Failed to open file for writing");
return;
}
current = head;
while (current) {
fwrite(¤t->data, sizeof(struct flight), 1, file);
current = current->nxt;
}
fclose(file);
printf("******************************************\n");
printf("*******航班删除成功,是否继续删除?*******\n");
printf("*******按(Y)y继续删除 按(N)n回到主菜单****\n");
printf("******************************************\n");
char key;
scanf("%c", &key);
getchar();
if (key == 'y' || key == 'Y') {
delet();
}
else if (key == 'N' || key == 'n') {
returnMain();
}
system("cls");
}
void delet() {
system("cls");
struct flight p;
int size, ok = 0, id;
printf("******************************************\n");
printf("***********请输入待删除航班的ID***********\n");
printf("******************************************\n");
scanf("%d", &id);
getchar();
FILE* file;
file = fopen("flight.data", "r");
//file没有到文件结尾,并且从file中读取一个struct到p
while (!feof(file) && (size = fread(&p, sizeof(struct flight), 1, file)) == 1) {
if (p.id == id) {
ok = 1;
break;
}
}
fclose(file);
if (ok == 1 && isFly(p.id) == 0) {
printf("*******************************************\n");
printf("*******请问您确定要删除下列航班吗?********\n");
printf("|ID |起飞时间 |始发地 |终点 |时间 |额定载客量 |订票人数 |是否起飞\n");
printf("|%d |%02d:%02d |%s |%s |%d |%d |%d |%s\n", p.id, p.startHour,
p.startMinute, p.start,
p.end, p.time, p.capacity, p.people, p.isFly);
printf("*******************************************\n");
printf("*******按(Y)y继续删除 按(N)n回到主菜单*****\n");
char key;
getchar();
scanf("%c", &key);
if (key == 'y' || key == 'Y') {
delflight(id);
system("cls");
}
else {
returnMain();
}
}
else {
printf("*******************************************\n");
printf("**************未找到该航班或该航班已起飞********\n");
printf("*******按(Y)y继续删除 按(N)n回到主菜单*********\n");
char key;
scanf("%c", &key);
getchar();
if (key == 'y' || key == 'Y') {
delet();
}
else if (key == 'N' || key == 'n') {
returnMain();
}
system("cls");
}
}
//排序函数
int compareFlights(const void *a, const void *b) {
struct flight* flightA = (struct flight*)a;
struct flight* flightB = (struct flight*)b;
// 比较始发地名称
int startComparison = strcmp(flightA->start, flightB->start);
if (startComparison != 0) {
return startComparison; // 如果始发地不同,按始发地排序
}
// 如果始发地相同,再比较起飞时间
if (flightA->startHour != flightB->startHour) {
return flightA->startHour - flightB->startHour;
}
return flightA->startMinute - flightB->startMinute;
}
//浏览航班
/*void checkall() {
system("cls");
FILE* file;
file = fopen("flight.data", "r");
struct flight p;
int size;
printf("|ID |起飞时间 |始发地 |终点 |时间 |额定载客量 |订票人数 |是否起飞\n");
while (!feof(file) && (size = fread(&p, sizeof(struct flight), 1, file)) == 1) {
if (isFly(p.id) == 1)
printf("|%d |%02d:%02d |%s |%s |%d |%d |%d |此班已发出\n", p.id, p.startHour,
p.startMinute, p.start,
p.end, p.time, p.capacity, p.people);
else
{
printf("|%d |%02d:%02d |%s |%s |%d |%d |%d |%s\n", p.id, p.startHour,
p.startMinute, p.start,
p.end, p.time, p.capacity, p.people, p.isFly);
}
}
printf("按回车键返回主菜单");
getchar();
fclose(file);
system("cls");
}*/
void checkall() {
system("cls");
FILE* file;
file = fopen("flight.data", "r");
struct flight p;
int size;
printf("|ID |起飞时间 |始发地 |终点 |时间 |额定载客量 |订票人数 |是否起飞\n");
while (!feof(file) && (size = fread(&p, sizeof(struct flight), 1, file)) == 1) {
if (isFly(p.id) == 1)
printf("|%d |%02d:%02d |%s |%s |%d |%d |%d |%s\n", p.id, p.startHour,
p.startMinute, p.start,
p.end, p.time, p.capacity, p.people, p.isFly);
else
{
printf("|%d |%02d:%02d |%s |%s |%d |%d |%d |%s\n", p.id, p.startHour,
p.startMinute, p.start,
p.end, p.time, p.capacity, p.people, p.isFly);
}
}
printf("按回车键返回主菜单");
getchar();
fclose(file);
system("cls");
}
//修改
void updateflight(int id) {
system("cls");
FILE* file;
file = fopen("flight.data", "r");
struct node* head = NULL, * current = NULL, * pre = NULL;
struct flight p, p1;
int size, i;
while (!feof(file) && (size = fread(&p, sizeof(struct flight), 1, file)) == 1) {
if (p.id != id) {
struct node* newNode = new node;
newNode->data = p;
newNode->nxt = NULL;
if (!head) {
head = newNode;
current = head;
}
else {
current->nxt = newNode;
current = newNode;
}
}
else if (p.id == id) {
p1.id = p.id;
p1.people = 0;
strcpy(p1.isFly, "NO");
printf("请输入起飞时间(小时)\n");
scanf("%d", &p1.startHour);
p1.startHour %= 24;
printf("请输入起飞时间(分钟)\n");
scanf("%d", &p1.startMinute);
p1.startMinute %= 60;
printf("请输入始发地\n");
scanf("%s", p1.start);
printf("请输入终点\n");
scanf("%s", &p1.end);
printf("请输入额定载客量\n");
scanf("%d", &p1.capacity);
printf("请输入飞行时间(小时)\n");
scanf("%d", &p1.time);
struct node* newNode = new node;
newNode->data = p1;
newNode->nxt = NULL;
if (!head) {
head = newNode;
current = head;
}
else {
current->nxt = newNode;
current = newNode;
}
}
}
fclose(file);
file = fopen("flight.data", "w");
fclose(file);
file = fopen("flight.data", "a");
if (!file) {
printf("Failed to open file for writing");
return;
}
current = head;
while (current) {
fwrite(¤t->data, sizeof(struct flight), 1, file);
current = current->nxt;
}
fclose(file);
printf("******************************************\n");
printf("*******航班修改成功,是否继续修改?*******\n");
printf("*******按(Y)y继续修改 按(N)n回到主菜单****\n");
printf("******************************************\n");
char key;
getchar();
scanf("%c", &key);
if (key == 'y' || key == 'Y') {
update();
}
else if (key == 'N' || key == 'n') {
returnMain();
}
system("cls");
}
void update() {
system("cls");
int size, id, ok = 0;
struct flight p;
printf("请输入待修改的航班ID\n");
scanf("%d", &id);
getchar();
FILE* file;
file = fopen("flight.data", "r");
while (!feof(file) && (size = fread(&p, sizeof(struct flight), 1, file)) == 1) {
if (p.id == id) {
ok = 1;
break;
}
}
fclose(file);
if (ok == 1) {
printf("*******************************************\n");
printf("*******请问您确定要修改下列航班吗?********\n");
printf("|ID |起飞时间 |始发地 |终点 |时间 |额定载客量 |订票人数 |是否起飞\n");
printf("|%d |%02d:%02d |%s |%s |%d |%d |%d |%s\n", p.id, p.startHour,
p.startMinute, p.start,
p.end, p.time, p.capacity, p.people, p.isFly);
printf("*******************************************\n");
printf("*******按(Y)y继续修改 按(N)n回到主菜单*****\n");
char key;
scanf("%c", &key);
getchar();
if (key == 'y' || key == 'Y') {
updateflight(id);
system("cls");
}
}
else {
fclose(file);
printf("*******************************************\n");
printf("**************未找到该航班*****************\n");
printf("*******按(Y)y继续修改 按(N)n回到主菜单*****\n");
char key;
scanf("%c", &key);
getchar();
if (key == 'y' || key == 'Y') {
update();
}
else if (key == 'N' || key == 'n') {
returnMain();
}
}
system("cls");
}
//查询
void find() {
system("cls");
FILE* file;
file = fopen("flight.data", "r");
struct flight p;
int size, id, ok = 0, choice;
printf("******************************************\n");
printf("*************请输入查询方式***********\n");
printf("********1【航班号 查询】*********\n");
printf("********2【终点站 查询】*********\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("******************************************\n");
printf("*************请输入待查询航班ID***********\n");
printf("******************************************\n");
scanf("%d", &id);
while (!feof(file) && (size = fread(&p, sizeof(struct flight), 1, file)) == 1) {
if (p.id == id) {
ok = 1;
if (isFly(p.id) == 1)
{
printf("|ID |起飞时间 |始发地 |终点 |时间 |额定载客量 |订票人数 |是否起飞\n");
printf("|%d |%02d:%02d |%s |%s |%d |%d |%d |此班已发出\n", p.id, p.startHour,
p.startMinute, p.start,
p.end, p.time, p.capacity, p.people);
}
else
{
printf("|ID |起飞时间 |始发地 |终点 |时间 |额定载客量 |订票人数 |是否起飞\n");
printf("|%d |%02d:%02d |%s |%s |%d |%d |%d |%s\n", p.id, p.startHour,
p.startMinute, p.start,
p.end, p.time, p.capacity, p.people, p.isFly);
}
break;
}
}
break;
case 2:
printf("******************************************\n");
printf("*************请输入待查询航班终点站***********\n");
printf("******************************************\n");
char destination[20];
scanf("%s", destination);
while (!feof(file) && (size = fread(&p, sizeof(struct flight), 1, file)) == 1) {
if (strcmp(destination, p.end) == 0)
{
ok = 1;
printf("|ID |起飞时间 |始发地 |终点 |时间 |额定载客量 |订票人数 |是否起飞\n");
if (isFly(p.id) == 1)
printf("|%d |%02d:%02d |%s |%s |%d |%d |%d |此班已发出\n", p.id, p.startHour,
p.startMinute, p.start,
p.end, p.time, p.capacity, p.people);
else
{
printf("|%d |%02d:%02d |%s |%s |%d |%d |%d |%s\n", p.id, p.startHour,
p.startMinute, p.start,
p.end, p.time, p.capacity, p.people, p.isFly);
}
break;
}
}
break;
}
if (ok == 0)
printf("未找到航班!请检查航班ID或终点站是否有误!\n");
fclose(file);
printf("******************************************\n");
printf("*******航班查询结束,是否继续查询?*******\n");
printf("*******按(Y)y继续查询 按(N)n回到主菜单****\n");
printf("******************************************\n");
char key;
scanf("%c", &key);
getchar();
if (key == 'y' || key == 'Y') {
find();
}
else if (key == 'N' || key == 'n') {
returnMain();
}
system("cls");
}
//界面颜色
void color() {
system("cls");
int a;
printf("1:蓝底白字 2:黑底白字 3:绿底白字 4:灰底白字 5:淡蓝底白字\n");
printf("6:白底黑字 7:蓝底黑字 8:亮白底黑字 9:绿底黑字 10:紫底黑字\n");
scanf("%d", &a);
if (a == 1)
system("color 17");
if (a == 2)
system("color 07");
if (a == 3)
system("color 27");
if (a == 4)
system("color 87");
if (a == 5)
system("color 97");
if (a == 6)
system("color 70");
if (a == 7)
system("color 10");
if (a == 8)
system("color F0");
if (a == 9)
system("color 20");
if (a == 10)
system("color 50");
system("cls");
}
//重置
void fileinit() {
system("cls");
FILE* file;
file = fopen("flight.data", "w");
if (file == NULL) {
//创建文件失败
printf("*******************\n");
printf("** *\n");
printf("** 文件初始化失败 *\n");
printf("** *\n");
printf("*******************\n");
return;
}
printf("*******************\n");
printf("** *\n");
printf("** 文件初始化成功 *\n");
printf("** *\n");
printf("*******************\n");
system("pause");
fclose(file);
system("cls");
}
//添加
void add() {
system("cls");
struct flight p;
system("cls");
p.people = 0;
strcpy(p.isFly, "NO");
printf("请输入ID\n");
scanf("%d", &p.id);
printf("请输入起飞时间(小时)\n");
scanf("%d", &p.startHour);
p.startHour %= 24;
printf("请输入起飞时间(分钟)\n");
scanf("%d", &p.startMinute);
p.startMinute %= 60;
printf("请输入始发地\n");
scanf("%s", p.start);
printf("请输入终点\n");
scanf("%s", &p.end);
printf("请输入额定载客量\n");
scanf("%d", &p.capacity);
printf("请输入飞行时间(小时)\n");
scanf("%d", &p.time);
getchar();
FILE* file;
file = fopen("flight.data", "a");
fwrite(&p, sizeof(struct flight), 1, file);
printf("|ID |起飞时间 |始发地 |终点 |时间 |额定载客量 |订票人数 |是否起飞\n");
printf("|%d |%02d:%02d |%s |%s |%d |%d |%d |%s\n", p.id, p.startHour,
p.startMinute, p.start,
p.end, p.time, p.capacity, p.people, p.isFly);
fclose(file);
printf("***************************************\n");
printf("********航班添加成功,是否继续?*******\n");
printf("*****按(Y)y继续添加 按(N)n回到主菜单***\n");
printf("***************************************\n");
char key;
scanf("%c", &key);
getchar();
if (key == 'y' || key == 'Y') {
add();
}
else if (key == 'N' || key == 'n') {
returnMain();
}
system("cls");
}
//退出
void checkout() {
system("cls");
printf("***************************************\n");
printf("********请问您确定退出本系统吗?*******\n");
printf("*****按(Y)y安全退出 按(N)n回到主菜单***\n");
printf("***************************************\n");
char key;
scanf("%c", &key);
getchar();
if (key == 'y' || key == 'Y') {
printf("***************************************\n");
printf("***************************************\n");
printf("********已安全退出 祝您生活愉快********\n");
printf("***************************************\n");
exit(0);
}
else if (key == 'N' || key == 'n') {
menu();
}
system("cls");
}
//订票
void orderTicket(int id, int people) {
system("cls");
FILE* file;
file = fopen("flight.data", "r");
struct flight p, p1;
int size;
struct node* head = NULL, * current = NULL, * pre = NULL;
while (!feof(file) && (size = fread(&p, sizeof(struct flight), 1, file)) == 1) {
if (p.id != id) {
struct node* newNode = new node;
newNode->data = p;
newNode->nxt = NULL;
if (!head) {
head = newNode;
current = head;
}
else {
current->nxt = newNode;
current = newNode;
}
}
else if (p.id == id) {
p1 = p;
p1.people += people;
struct node* newNode = new node;
newNode->data = p1;
newNode->nxt = NULL;
if (!head) {
head = newNode;
current = head;
}
else {
current->nxt = newNode;
current = newNode;
}
}
}
fclose(file);
file = fopen("flight.data", "w");
fclose(file);
file = fopen("flight.data", "a");
current = head;
while (current) {
fwrite(¤t->data, sizeof(struct flight), 1, file);
current = current->nxt;
}
fclose(file);
}
void order() {
system("cls");
FILE* file;
file = fopen("flight.data", "r");
struct flight p;
int size, people, id, ok = 0;
printf("******************************************\n");
printf("*************请输入航班号和出行人数***********\n");
printf("******************************************\n");
scanf("%d", &id);
scanf("%d", &people);
while (!feof(file) && (size = fread(&p, sizeof(struct flight), 1, file)) == 1) {
if ((p.id == id) && isFly(p.id) == 0 && p.people + people <= p.capacity) {
ok = 1;
orderTicket(p.id, people);
break;
}
}
if (ok == 0)
printf("操作有误!请检查航班号和订票人数是否有误!\n");
fclose(file);
printf("******************************************\n");
printf("*******订票结束,是否继续订票?*******\n");
printf("*******按(Y)y继续订票 按(N)n回到主菜单****\n");
printf("******************************************\n");
char key;
scanf("%c", &key);
getchar();
if (key == 'y' || key == 'Y') {
order();
}
else if (key == 'N' || key == 'n') {
returnMain();
}
system("cls");
}
//退票
void returnTicket(int id, int people) {
system("cls");
FILE* file;
file = fopen("flight.data", "r");
struct flight p, p1;
int size;
struct node* head = NULL, * current = NULL, * pre = NULL;
while (!feof(file) && (size = fread(&p, sizeof(struct flight), 1, file)) == 1) {
if (p.id != id) {
struct node* newNode = new node;
newNode->data = p;
newNode->nxt = NULL;
if (!head) {
head = newNode;
current = head;
}
else {
current->nxt = newNode;
current = newNode;
}
}
else if (p.id == id) {
p1 = p;
p1.people -= people;
struct node* newNode = new node;
newNode->data = p1;
newNode->nxt = NULL;
if (!head) {
head = newNode;
current = head;
}
else {
current->nxt = newNode;
current = newNode;
}
}
}
fclose(file);
file = fopen("flight.data", "w");
fclose(file);
file = fopen("flight.data", "a");
current = head;
while (current) {
fwrite(¤t->data, sizeof(struct flight), 1, file);
current = current->nxt;
}
fclose(file);
}
void returned() {
system("cls");
FILE* file;
file = fopen("flight.data", "r");
struct flight p;
int size, id, people, ok = 0;
printf("******************************************\n");
printf("*************请输入班次和退票人数***********\n");
printf("******************************************\n");
scanf("%d", &id);
scanf("%d", &people);
while (!feof(file) && (size = fread(&p, sizeof(struct flight), 1, file)) == 1) {
if (p.id == id && isFly(p.id) == 0 && (p.people - people >= 0)) {
ok = 1;
returnTicket(p.id, people);
break;
}
}
if (ok == 0)
printf("操作有误!请检查班次和退票人数是否有误!\n");
fclose(file);
printf("******************************************\n");
printf("*******退票结束,是否继续退票?*******\n");
printf("*******按(Y)y继续退票 按(N)n回到主菜单****\n");
printf("******************************************\n");
char key;
scanf("%c", &key);
getchar();
if (key == 'y' || key == 'Y') {
returned();
}
else if (key == 'N' || key == 'n') {
returnMain();
}
system("cls");
}
//是否起飞
int isFly(int id) {
FILE* file;
file = fopen("flight.data", "r");
struct flight p;
int size, ok = 0;
getchar();
time_t now;
struct tm* local;
// 获取当前系统的时分
time(&now);
local = localtime(&now);
while (!feof(file) && (size = fread(&p, sizeof(struct flight), 1, file)) == 1) {
if (p.id == id) {
if (local->tm_hour > p.startHour || (local->tm_hour == p.startHour && local->tm_min >= p.startMinute))
return 1;
return 0;
}
}
}
//返回主菜单
void returnMain() {
if (isManager == 1)
menu();
passenger();
}
标签:flight,file,111,key,printf,id,struct
From: https://www.cnblogs.com/hoshino-/p/18624426