#include #include #include #include typedef struct Flight { int FlightNum; //航班号 int MaxFare; //最大载客数 char StartPlace[10]; //起飞地点 char EndPlace[10]; //降落地点 char StartDate[10]; //起飞时间 char EndDate[10]; //降落时间 float price; //单价 int currentFareNum; //当前乘客数 struct Flight* prev; struct Flight* next; }FlightList; typedef struct Fare { int FlightNum; //航班号 char IDnumber[20]; //身份证号 char name[10]; //姓名 char gender[5]; //性别 char birthDate[10]; //出生年月 int SeatNumber; //座位号 struct Fare* prev; struct Fare* next; }FareList; int FlightNum; //航班数 int FareNum; //乘客数 FlightList* FlightHead; //航班头结点 FareList* FareHead; //乘客头结点 /********************文件************************/ void ReadFlightInfo(void); //读取航班信息 void ReadPareInfo(void); //读取乘客信息 void WriteFlightInfo(FILE* Fp); //将航班信息写入文件 void WritePareInfo(FILE* Fp); //将乘客信息写入文件 int CopyArrayToNum(char* str, int start, int end); //拷贝数组转换成数字 void CopyArrayToStr(char* str1, char* str2,int start, int end); //拷贝数组到字符串 float CopyArrayToFloat(char* str, int start, int end); //拷贝数组转换成数字 /*******************链表*************************/ FlightList* FlightMallocFunc(void); //航班申请堆空间 FareList* FareMallocFunc(void); //乘客申请堆空间 FlightList* FlightCreateNewNode(void); //航班创建新节点 FareList* FareCreateNewNode(void); //乘客创建新节点 void FlightDeleteAllNode(void); //删除航班所有节点 void FareDeleteAllNode(void); //删除乘客所有节点 void FlightInsertNode(FlightList *h_node, FlightList *node, FlightList *n_node); //航班调整新节点插入位置 void FareInsertNode(FareList *h_node, FareList *node, FareList *n_node); //乘客调整新节点插入位置 /******************航班**************************/ void ModeFlightMenu(void); //显示航班菜单 void ModeShowAllFlight(void); //显示所有航班 void ModeAddFlight(void); //增加航班 void ModeCancelFlight(void); //取消航班 /*******************乘客*************************/ void ModeFareMenu(void); //显示乘客菜单 void ModeShowAllFare(void); //显示所有乘客 void ModeAddFare(void); //新增乘客 void ModeDeleteFare(void); //删除乘客 void ModeFlightReserve(void); //航班订票 void ModeExitSystem(void); //退出程序 int main(void) { FlightNum = 0; FareNum = 0; FlightHead = FlightMallocFunc(); FareHead = FareMallocFunc(); FlightHead->next = FlightHead->prev = FlightHead; FareHead->next = FareHead->prev = FareHead; ReadFlightInfo(); ReadPareInfo(); while(1) { int menu = 0,role = 0; system("CLS"); //清除窗口 printf("**************《航班信息管理系统》*******************\n"); printf("请选择你的身份 1.管理人员 2.乘客 0.退出程序:"); scanf("%d",&role); if (role != 1 && role != 2 && role != 0) { printf("请输入正确指令!\n"); while(getchar() != '\n'); //清除缓冲区 system("pause"); continue; } while(1) { if (role == 1) { int mode = 0; system("CLS"); //清除窗口 ModeFlightMenu(); scanf("%d",&mode); switch(mode) { case 1: ModeShowAllFlight(); //显示所有航班 break; case 2: ModeAddFlight(); //增加航班 break; case 3: ModeCancelFlight(); //取消航班 break; case 4: menu = 1; //返回菜单 break; case 0: printf("退出程序!\n"); ModeExitSystem(); return 0; default: printf("请输入正确指令!\n"); while(getchar() != '\n'); //清除缓冲区 system("pause"); continue; } } else if (role == 2) { int mode = 0; system("CLS"); //清除窗口 ModeFareMenu(); scanf("%d",&mode); switch (mode) { case 1: ModeShowAllFare(); //显示所有乘客 break; case 2: ModeAddFare(); //新增乘客 break; case 3: ModeDeleteFare(); //删除乘客 break; case 4: ModeFlightReserve(); //航班订票 break; case 5: menu = 1; //返回菜单 break; case 0: printf("退出程序!\n"); ModeExitSystem(); return 0; default: printf("请输入正确指令!\n"); while(getchar() != '\n'); //清除缓冲区 system("pause"); continue; } } else { printf("退出程序!\n"); ModeExitSystem(); return 0; } if (menu == 1) break; } } return 0; } void ReadFlightInfo(void)//读取航班信息 { char Info[100]; char* p; int a, i, start, StrLength; FlightList* node; FILE* FlightFp = fopen(".\\FlightInfo.txt","a+"); if(FlightFp == NULL) { printf("open file failed!\n"); system("pause"); return ; } fseek(FlightFp, 0, SEEK_SET); //将光标置于文件开头 while(1) { a=1; memset(Info, 0, sizeof(Info)); fseek(FlightFp, 0, SEEK_CUR);//继续上一个位置读取 fgets(Info, 100, FlightFp); if (Info[0] == 0)//判断是否读到文件未 break; node = FlightCreateNewNode();//创建新节点 StrLength = strlen(Info); start = 0; for (i = 0,p=Info; iFlightNum = CopyArrayToNum(Info, start, i); //航班号 break; case 2: node->MaxFare = CopyArrayToNum(Info, start, i); //最大载客数 break; case 3: CopyArrayToStr(node->StartPlace, Info, start, i); //起飞地点 break; case 4: CopyArrayToStr(node->EndPlace, Info, start, i); //降落地点 break; case 5: CopyArrayToStr(node->StartDate, Info, start, i); //起飞时间 break; case 6: CopyArrayToStr(node->EndDate, Info, start, i); //降落时间 break; case 7: node->price = CopyArrayToFloat(Info, start, i); //单价 break; case 8: node->currentFareNum = CopyArrayToNum(Info, start, i);//当前乘客人数 break; } start = i+1; a++; continue; } } } fclose(FlightFp); } void ReadPareInfo(void)//读取乘客信息 { char Info[100]; char* p; int a, i, start, StrLength; FareList* node; FILE* pareFp = fopen(".\\PareInfo.txt","a+"); if(pareFp == NULL) { printf("open file failed!\n"); system("pause"); return ; } fseek(pareFp, 0, SEEK_SET); //将光标置于文件开头 while(1) { a=1; memset(Info, 0, sizeof(Info)); fseek(pareFp, 0, SEEK_CUR);//继续上一个位置读取 fgets(Info, 100, pareFp); if (Info[0] == 0)//判断是否读到文件未 break; node = FareCreateNewNode();//创建新节点 StrLength = strlen(Info); start = 0; for (i = 0,p=Info; iFlightNum = CopyArrayToNum(Info, start, i); //航班号 break; case 2: CopyArrayToStr(node->IDnumber, Info, start, i); //身份证号 break; case 3: CopyArrayToStr(node->name, Info, start, i); //姓名 break; case 4: CopyArrayToStr(node->gender, Info, start, i); //性别 break; case 5: CopyArrayToStr(node->birthDate, Info, start, i); //出生年月 break; case 6: node->SeatNumber = CopyArrayToNum(Info, start, i); //座位号 break; } start = i+1; a++; continue; } } } fclose(pareFp); } void CopyArrayToStr(char* str1, char* str2,int start, int end)//拷贝数组到字符串 { int i; int numder = abs(end-start); for (i=0; inext; p!= FlightHead; p = p->next) { fprintf(Fp,"%d %d %s %s %s %s %.2f %d\n",p->FlightNum,p->MaxFare,p->StartPlace,p->EndPlace,p->StartDate,p->EndDate,p->price,p->currentFareNum); } } void WritePareInfo(FILE* Fp)//将乘客信息写入文件 { FareList* p; for (p = FareHead->next; p!= FareHead; p = p->next) { fprintf(Fp,"%d %s %s %s %s %d\n",p->FlightNum,p->IDnumber,p->name,p->gender,p->birthDate,p->SeatNumber); } } void ModeFlightMenu(void)//显示航班菜单 { printf("***********航班菜单***********\n"); printf("1.显示所有航班\n" "2.增加航班\n" "3.取消航班\n" "4.返回首页\n" "0.退出程序\n" "请输入模式:"); } void ModeShowAllFlight(void)//显示所有航班 { FlightList* p; if(FlightNum == 0) { printf("当前航班数为0,请添加后进行操作!\n"); system("pause"); return ; } system("CLS"); //清除窗口 printf("********当前当前航班数:%d**********\n",FlightNum); for (p = FlightHead->next; p!=FlightHead; p = p->next) { printf(">>>>>>当前航班号:%d<<<<<<\n",p->FlightNum); printf("最大载客数:%d\n起飞地点:%s\n降落地点:%s\n起飞时间%s\n降落时间:%s\n单价:%.2f\n当前乘客数:%d\n", p->MaxFare,p->StartPlace,p->EndPlace,p->StartDate,p->EndDate,p->price,p->currentFareNum); } system("pause"); return ; } void ModeAddFlight(void)//增加航班 { FlightList* NewFlight = FlightCreateNewNode(); FlightList* p; printf("*************添加航班信息*****************\n"); while(1) { int Numder = 0; printf("航班号:"); if (!(scanf("%d",&Numder))) { printf("请输入整型数!\n"); while(getchar() != '\n'); //清除缓冲区 system("pause"); continue; } for (p = FlightHead->next; p != FlightHead; p = p->next) { if (p->FlightNum == Numder) break; } if (p != FlightHead) { printf("该航班已存在,请确认后输入!\n"); system("pause"); return ; } else NewFlight->FlightNum = Numder; break; } while(1) { printf("最大载客数:"); if (!(scanf("%d",&(NewFlight->MaxFare)))) { printf("请输入整型数!\n"); while(getchar() != '\n'); //清除缓冲区 system("pause"); continue; } break; } while(1) { printf("起飞地点:"); if (!(scanf("%s",NewFlight->StartPlace))) { printf("输入错误,请重新输入!\n"); while(getchar() != '\n'); //清除缓冲区 system("pause"); continue; } break; } while(1) { printf("降落地点:"); if (!(scanf("%s",NewFlight->EndPlace))) { printf("输入错误,请重新输入!\n"); while(getchar() != '\n'); //清除缓冲区 system("pause"); continue; } break; } while(1) { printf("起飞时间:"); if (!(scanf("%s",NewFlight->StartDate))) { printf("输入错误,请重新输入!\n"); while(getchar() != '\n'); //清除缓冲区 system("pause"); continue; } break; } while(1) { printf("降落时间:"); if (!(scanf("%s",NewFlight->EndDate))) { printf("输入错误,请重新输入!\n"); while(getchar() != '\n'); //清除缓冲区 system("pause"); continue; } break; } while (1) { printf("单价:"); if (!(scanf("%f",&(NewFlight->price)))) { printf("输入错误,请重新输入!\n"); while(getchar() != '\n'); //清除缓冲区 system("pause"); continue; } break; } printf("增加成功!\n"); system("pause"); } void ModeCancelFlight(void)//取消航班 { FlightList* p; FareList* q; int Num =0; printf("*************取消航班*****************\n"); while(1) { printf("请输入要取消的航班号:"); if (!(scanf("%d",&Num))) { printf("请输入整型数!\n"); while(getchar() != '\n'); //清除缓冲区 system("pause"); continue; } for (p = FlightHead->next; p != FlightHead; p = p->next) { if (p->FlightNum == Num) break; } if (p == FlightHead) { printf("没有该航班,请确认后输入!\n"); system("pause"); return ; } if ((p->MaxFare*0.1) < p->currentFareNum) { printf("该航班已超过百分之10的乘客购买,不可取消!\n"); system("pause"); continue; } else { while(1) { int mode = 0; printf("当前航班乘客有:%d\n", p->currentFareNum); printf("是否取消 1.是 2.否:"); scanf("%d",&mode); if (mode != 1 && mode != 2) { printf("请输入正确指令!\n"); while(getchar() != '\n'); //清除缓冲区 system("pause"); continue; } if (mode == 1) { p->currentFareNum = 0; for (q = FareHead->next; q != FareHead; q = q->next) { if (q->FlightNum == p->FlightNum) q->SeatNumber = 0; } printf("已取消%d航班,购买该航班的乘客已退票\n", p->FlightNum); system("pause"); } else { printf("返还乘客首页!\n"); system("pause"); } break; } } break; } } void ModeFareMenu(void)//显示乘客菜单 { printf("***********乘客菜单***********\n"); printf("1.显示所有乘客\n" "2.新增乘客\n" "3.删除乘客\n" "4.航班订票\n" "5.返回首页\n" "0.退出程序\n" "请输入模式:"); } void ModeShowAllFare(void)//显示所有乘客 { FareList* p; if(FareNum == 0) { printf("当前乘客人数为0,请添加后进行操作!\n"); system("pause"); return ; } system("CLS"); //清除窗口 printf("********当前乘客人数:%d**********\n",FareNum); for (p = FareHead->next; p!=FareHead; p = p->next) { printf(">>>>>>当前乘客姓名:%s<<<<<<\n",p->name); printf("航班号:%d\n身份证号:%s\n性别:%s\n出生年月%s\n座位号:%d\n",p->FlightNum,p->IDnumber,p->gender,p->birthDate,p->SeatNumber); } system("pause"); return ; } void ModeAddFare(void)//新增乘客 { FareList* node = FareCreateNewNode(); printf("******请输入乘客信息*******\n"); while(1) { printf("姓名:"); if (!(scanf("%s",node->name))) { printf("输入错误,请重新输入!\n"); while(getchar() != '\n'); //清除缓冲区 system("pause"); continue; } break; } while(1) { printf("身份证号:"); if (!(scanf("%s",node->IDnumber))) { printf("输入错误,请重新输入!\n"); while(getchar() != '\n'); //清除缓冲区 system("pause"); continue; } break; } while(1) { int gender = 0,input = 0; printf("性别1.男 2.女:"); input = scanf("%d",&gender); if (input != 1 && input != 2) { printf("输入错误,请重新输入!\n"); while(getchar() != '\n'); //清除缓冲区 system("pause"); continue; } if (gender == 1) strcpy(node->gender,"男"); else strcpy(node->gender,"女"); break; } while(1) { int year = 0,month = 0,input = 0; char date[10],str[10]; memset(date, 0, sizeof(date)); memset(str, 0, sizeof(str)); printf("出生年份:"); input = scanf("%d",&year); printf("出生月份:"); input = scanf("%d",&month); if (input && (year < 1900 || year > 2019) && (month > 12 || month <= 0)) { printf("输入错误,请输入正确年份和月份!\n"); while(getchar() != '\n'); //清除缓冲区 system("pause"); continue; } strcpy(date,strcat(itoa(year,str,10),"/")); memset(str, 0, sizeof(str)); strcat(date,itoa(month,str,10)); memset(str, 0, sizeof(str)); strcpy(node->birthDate,date); break; } printf("添加成功!\n"); system("pause"); } void ModeDeleteFare(void)//删除乘客 { FareList* p; char name[10]; memset(name, 0, sizeof(name)); printf("***********删除乘客**************\n"); printf("请输入需要删除的乘客姓名:"); scanf("%s", name); for (p = FareHead->next; p != FareHead; p = p->next) { if (!(strcmp(p->name,name))) { p->prev->next = p->next; p->next->prev = p->prev; free(p); FareNum--; break; } } if (p == FareHead) { printf("未找到该乘客,请确认后进行操作!\n"); system("pause"); return ; } printf("删除成功!\n"); system("pause"); } void ModeFlightReserve(void)//航班订票 { FlightList* p; FareList* q; char FindName[10]; memset(FindName,0,sizeof(FindName)); printf("***************航班订票***************\n"); while(1) { printf("请输入要购票的乘客姓名:"); if (!(scanf("%s",FindName))) { printf("输入错误,请重新输入!\n"); while(getchar() != '\n'); //清除缓冲区 system("pause"); continue; } for (q = FareHead->next; q != FareHead; q= q->next) { if(!strcmp(q->name,FindName)) break; } if (q == FareHead) { printf("没有该乘客,请确认后输入!\n"); system("pause"); return ; } else break; } while(1) { int mode = 0; printf("请输入你要查询的航班要求1.航班号 2.起飞地点 3.降落地点 4.起飞时间 5.降落时间:"); scanf("%d",&mode); switch (mode) { case 1: while(1) { printf("航班号:"); if (!(scanf("%d",&(q->FlightNum)))) { printf("请输入整型数!\n"); while(getchar() != '\n'); //清除缓冲区 system("pause"); continue; } for (p = FlightHead->next; p != FlightHead; p = p->next) { if (p->FlightNum == q->FlightNum) break; } if (p == FlightHead) { printf("没有该航班,请确认后输入!\n"); system("pause"); return ; } break; } break; case 2: while(1) { char StartPlace[10]; memset(StartPlace, 0, sizeof(StartPlace)); printf("起飞地点:"); if (!(scanf("%s",StartPlace))) { printf("输入错误,请重新输入!\n"); while(getchar() != '\n'); //清除缓冲区 system("pause"); continue; } for (p = FlightHead->next; p != FlightHead; p = p->next) { if (!strcmp(p->StartPlace,StartPlace)) break; } if (p == FlightHead) { printf("没有该航班,请确认后输入!\n"); system("pause"); return ; } break; } break; case 3: while(1) { char EndPlace[10]; memset(EndPlace, 0, sizeof(EndPlace)); printf("降落地点:"); if (!(scanf("%s",EndPlace))) { printf("输入错误,请重新输入!\n"); while(getchar() != '\n'); //清除缓冲区 system("pause"); continue; } for (p = FlightHead->next; p != FlightHead; p = p->next) { if (!strcmp(p->EndPlace,EndPlace)) break; } if (p == FlightHead) { printf("没有该航班,请确认后输入!\n"); system("pause"); return ; } break; } break; case 4: while(1) { char StartDate[10]; memset(StartDate, 0, sizeof(StartDate)); printf("起飞时间:"); if (!(scanf("%s",StartDate))) { printf("输入错误,请重新输入!\n"); while(getchar() != '\n'); //清除缓冲区 system("pause"); continue; } for (p = FlightHead->next; p != FlightHead; p = p->next) { if (!strcmp(p->StartDate,StartDate)) break; } if (p == FlightHead) { printf("没有该航班,请确认后输入!\n"); system("pause"); return ; } break; } break; case 5: while(1) { char EndDate[10]; memset(EndDate, 0, sizeof(EndDate)); printf("降落时间:"); if (!(scanf("%s",EndDate))) { printf("输入错误,请重新输入!\n"); while(getchar() != '\n'); //清除缓冲区 system("pause"); continue; } for (p = FlightHead->next; p != FlightHead; p = p->next) { if (!strcmp(p->EndDate,EndDate)) break; } if (p == FlightHead) { printf("没有该航班,请确认后输入!\n"); system("pause"); return ; } break; } break; default: printf("输入错误,请重新输入!\n"); while(getchar() != '\n'); //清除缓冲区 system("pause"); continue; } break; } printf("航班号 最大载客数 起飞地点 降落地点 起飞时间 降落时间 单价 当前乘客数\n"); printf(" %d\t %d\t %s\t %s %s %s %.2f\t%d\n", p->FlightNum,p->MaxFare,p->StartPlace,p->EndPlace,p->StartDate,p->EndDate,p->price,p->currentFareNum); while(1) { int mode = 0,num = 0; printf("是否购买当前航班1.是 2.否:"); scanf("%d",&mode); if (mode != 1 && mode != 2) { printf("输入错误,请重新输入!\n"); while(getchar() != '\n'); //清除缓冲区 system("pause"); continue; } if (mode == 1) { int num = 0; if (p->currentFareNum == p->MaxFare) { printf("当前乘客已满,请选择其他航班!\n"); system("pause"); break; } printf("请输入需要购买的票数:"); if (!(scanf("%d",&num))) { printf("请输入整型数!\n"); while(getchar() != '\n'); //清除缓冲区 system("pause"); continue; } if (num > (p->MaxFare-p->currentFareNum)) { printf("当前购买票数大于最大载客数,请重新购买!\n"); system("pause"); continue; } printf("票价:%.2f 是否购买?1.是 2.否\n",(p->price*num)); scanf("%d",&mode); if (mode != 1 && mode != 2) { printf("输入错误,请重新输入!\n"); while(getchar() != '\n'); //清除缓冲区 system("pause"); continue; } printf("购买成功,祝您旅途愉快!\n"); system("pause"); q->SeatNumber = num; p->currentFareNum +=num; break; } else break; } } void ModeExitSystem(void) { FILE* FlightFp = fopen(".\\FlightInfo.txt","w"); FILE* pareFp = fopen(".\\PareInfo.txt","w"); if(FlightFp == NULL || pareFp == NULL) { printf("open file failed!\n"); system("pause"); return ; } WriteFlightInfo(FlightFp); WritePareInfo(pareFp); fclose(FlightFp); fclose(pareFp); FlightDeleteAllNode(); //删除航班所有节点 FareDeleteAllNode(); //删除乘客所有节点 printf("欢迎下次光临!\n"); system("pause"); return ; } FlightList* FlightMallocFunc(void)//航班申请堆空间 { FlightList* node = malloc(sizeof(FlightList)); if(node == NULL) { printf("malloc failed!\n"); exit(-1); } return node; } FareList* FareMallocFunc(void)//乘客申请堆空间 { FareList* node = malloc(sizeof(FareList)); if(node == NULL) { printf("malloc failed!\n"); exit(-1); } return node; } FlightList* FlightCreateNewNode(void)//航班创建新节点 { FlightList *node = FlightMallocFunc(); node->next = node->prev = node; FlightInsertNode(FlightHead->prev, node ,FlightHead); FlightNum++; node->FlightNum = 0; node->MaxFare = 0; memset(node->StartDate, 0, sizeof(node->StartDate)); memset(node->EndPlace, 0, sizeof(node->EndPlace)); memset(node->StartDate, 0, sizeof(node->StartDate)); memset(node->EndDate, 0, sizeof(node->EndDate)); node->price = 0; node->currentFareNum = 0; return node; } FareList* FareCreateNewNode(void)//乘客创建新节点 { FareList *node = FareMallocFunc(); node->next = node->prev = node; FareInsertNode(FareHead->prev, node ,FareHead); FareNum++; node->FlightNum = 0; memset(node->IDnumber, 0, sizeof(node->IDnumber)); memset(node->name, 0, sizeof(node->name)); memset(node->gender, 0, sizeof(node->gender)); memset(node->birthDate, 0, sizeof(node->birthDate)); node->SeatNumber = 0; return node; } void FlightInsertNode(FlightList *h_node, FlightList *node, FlightList *n_node)//航班调整新节点插入位置 { h_node->next = node; node->prev = h_node; node->next = n_node; n_node->prev = node; } void FareInsertNode(FareList *h_node, FareList *node, FareList *n_node)//乘客调整新节点插入位置 { h_node->next = node; node->prev = h_node; node->next = n_node; n_node->prev = node; } void FlightDeleteAllNode(void)//删除航班所有节点 { while(1) { FlightList* p = FlightHead->next; p->prev->next = p->next; p->next->prev = p->prev; free(p); if(FlightHead->next = FlightHead) break; } free(FlightHead); } void FareDeleteAllNode(void)//删除乘客所有节点 { while(1) { FareList* p = FareHead->next; p->prev->next = p->next; p->next->prev = p->prev; free(p); if(FareHead->next = FareHead) break; } free(FareHead); }