C语言课设–航班订票系统

    • 大一的C语言课设,极简航班订票系统,希望能够对大家有所帮助
      • 0.文件结构
      • 1.代码
      • 2.航班信息录入
      • 3.查看航班信息
      • 4.航班信息查询
      • 5.修改航班信息
      • 6.删除航班信息
      • 7.订票
      • 8.查看订单
      • 9.修改订单
      • 10.退票
      • 如果有用还请点个赞~

大一的C语言课设,极简航班订票系统,希望能够对大家有所帮助

0.文件结构

首先需要在代码文件同级文件夹下创建一个flight文件夹,然后在flight文件夹下创建flight.dat和order.dat文件分别用于保存航班信息和订票信息
文件结构1

文件结构2

1.代码

#include#include#include#include#define SIZE 20#define NUM 100typedef struct{char flightNum[SIZE];char take_off_Time[SIZE];char land_Time[SIZE];char take_off_City[SIZE];char land_City[SIZE];float pre_Price;float discount;float now_Price;int tickets;}FLIGHT,*PFLIGHT;typedef struct{char flightNum[SIZE];char take_off_Time[SIZE];char land_Time[SIZE];char take_off_City[SIZE];char land_City[SIZE];float price;}ORDERFLIGHT,*PORDERFLIGHT;typedef struct{char orderId[SIZE];char name[SIZE];char userId[SIZE];ORDERFLIGHT order;}ORDER,*PORDER;void input_flight(){FILE* fp;int i=0;int n;FLIGHT f[NUM];if((fp=fopen("./flight/flight.dat","ab"))==NULL){printf("无法打开文件!系统将返回上一级菜单!\n");return;}printf("请输入预计要录入的航班数: ");scanf("%d",&n);printf("请按照顺序(航班号,起飞时间,降落时间,起飞城市,降落城市,原票价,折扣,剩余票数)依次输入相应信息;\n其中起飞和降落时间请按照形如\"2020-01-01/12:45\"的形式输入,若无折扣请输入1.0:\n");while(1){scanf("%s%s%s%s%s%f%f%d",f[i].flightNum,f[i].take_off_Time,f[i].land_Time,f[i].take_off_City,f[i].land_City,&f[i].pre_Price,&f[i].discount,&f[i].tickets);f[i].now_Price=f[i].pre_Price*f[i].discount;if((fwrite(&f[i],sizeof(FLIGHT),1,fp))!=1)printf("信息写入文件错误!\n");i++;if(i==n){int x; printf("已达到您预计要录入的数量,是否想要继续录入,若需要请输入要继续录入的数量,若需要退出请按0: ");scanf("%d",&x);if(x==0)break;else {printf("请输入:\n");n+=x;}}}printf("航班信息录入完成!\n");printf("\n");fclose(fp);}void check_flight(){FLIGHT f[NUM];FILE* fp;int i=0;if((fp=fopen("./flight/flight.dat","rb"))==NULL){printf("无法打开文件!系统将返回上一级!\n");return;}printf("所有航班的信息如下所示:\n");printf("航班号 起飞时间降落时间 起飞地目的地 原价 折扣现价剩余票数\n");while(fread(&f[i],sizeof(FLIGHT),1,fp)) {printf("%-s %-s %-s %-8s%-s %8.2f%8.2f %8.2f%3d\n",f[i].flightNum,f[i].take_off_Time,f[i].land_Time,f[i].take_off_City,f[i].land_City,f[i].pre_Price,f[i].discount,f[i].now_Price,f[i].tickets);i++;}printf("\n");fclose(fp);}void change_flight(){FLIGHT f[NUM];ORDER o[NUM];FILE* fp1;FILE* fp2;FILE* op1;FILE* op2; char str[SIZE];char str1[SIZE];int i=0;int a=0;int x;int flag=0;if((fp1=fopen("./flight/flight.dat","rb"))==NULL){printf("无法打开文件!系统将返回上一级!\n");return;}while(fread(&f[i],sizeof(FLIGHT),1,fp1)) {i++;}if((op1=fopen("./flight/order.dat","rb"))==NULL){printf("无法打开文件!系统将返回上一级!\n");return;}while(fread(&o[a],sizeof(ORDER),1,op1)) {a++;}fclose(fp1);fclose(op1);printf("请输入需要修改航班的航班号: ");scanf("%s",str);for(int j=0;j<i;j++){if(strcmp(str,f[j].flightNum)==0){printf("已找到该航班:\n");printf("航班号 起飞时间降落时间 起飞地目的地 原价 折扣现价剩余票数\n");printf("%-s %-s %-s %-8s%-s %8.2f%8.2f %8.2f%3d\n",f[j].flightNum,f[j].take_off_Time,f[j].land_Time,f[j].take_off_City,f[j].land_City,f[j].pre_Price,f[j].discount,f[j].now_Price,f[j].tickets);printf("请重新输入该航班的信息;\n请按照(起飞时间,降落时间,起飞城市,降落城市,原票价,折扣,剩余票数)顺序依次输入相应信息;\n其中起飞和降落时间请按照形如\"2020-01-01/12:45\"的形式输入,若无折扣请输入1.0:\n");scanf("%s%s%s%s%f%f%d",f[j].take_off_Time,f[j].land_Time,f[j].take_off_City,f[j].land_City,&f[j].pre_Price,&f[j].discount,&f[j].tickets);f[j].now_Price=f[j].pre_Price*f[j].discount;x=j;flag=1;}}if(flag==0)printf("您所要修改的航班并不存在!\n");if((fp2=fopen("./flight/flight.dat","wb"))==NULL){printf("无法打开文件!系统将返回上一级!\n");return;}for(int j=0;j<i;j++){if((fwrite(&f[j],sizeof(FLIGHT),1,fp2))!=1)printf("信息写入文件错误!\n");}fclose(fp2);for(int b=0;b<a;b++){if(strcmp(str,o[b].order.flightNum)==0){strcpy(o[b].order.take_off_Time,f[x].take_off_Time);strcpy(o[b].order.land_Time,f[x].take_off_Time);strcpy(o[b].order.take_off_City,f[x].take_off_City);strcpy(o[b].order.land_City,f[x].land_City);strcpy(str1,f[x].flightNum);strcpy(o[b].orderId,strcat(str1,o[b].userId));o[b].order.price=f[x].now_Price;}}if((op2=fopen("./flight/order.dat","wb"))==NULL){printf("无法打开文件!系统将返回上一级!\n");return;}for(int j=0;j<a;j++){if((fwrite(&o[j],sizeof(ORDER),1,op2))!=1)printf("信息写入文件错误!\n");}fclose(op2);printf("\n");}void delete_flight(){FLIGHT f[NUM];ORDER o[NUM];FILE* fp1;FILE* fp2;FILE* op1;FILE* op2;char str[SIZE];int i=0;int j=0;int a=0;int b=0;int x;int flag=0;int c=0;if((fp1=fopen("./flight/flight.dat","rb"))==NULL){printf("无法打开文件!系统将返回上一级!\n");return;}while(fread(&f[i],sizeof(FLIGHT),1,fp1)) {i++;}if((op1=fopen("./flight/order.dat","rb"))==NULL){printf("无法打开文件!系统将返回上一级!\n");return;}while(fread(&o[a],sizeof(ORDER),1,op1)) {a++;}fclose(fp1);fclose(op1);printf("请输入需要删除航班的航班号:");scanf("%s",str);for(j=0;j<i;j++){if(strcmp(str,f[j].flightNum)==0){printf("已找到该航班:\n");printf("航班号 起飞时间降落时间 起飞地目的地 原价 折扣现价剩余票数\n");printf("%-s %-s %-s %-8s%-s %8.2f%8.2f %8.2f%3d\n",f[j].flightNum,f[j].take_off_Time,f[j].land_Time,f[j].take_off_City,f[j].land_City,f[j].pre_Price,f[j].discount,f[j].now_Price,f[j].tickets);printf("确认要删除吗" />);scanf("%d",&c);printf("\n");flag=1;x=j;}}if(flag==0)printf("您所要删除的航班并不存在!\n\n");if(c==1){if((fp2=fopen("./flight/flight.dat","wb"))==NULL){printf("无法打开文件!系统将返回上一级!\n");return;}for(int k=0;k<x;k++){if((fwrite(&f[k],sizeof(FLIGHT),1,fp2))!=1)printf("信息写入文件错误!\n");}for(int k=x+1;k<i;k++){if((fwrite(&f[k],sizeof(FLIGHT),1,fp2))!=1)printf("信息写入文件错误!\n");}printf("\n");fclose(fp2); }for(b=0;b<a;b++){if(strcmp(str,o[b].order.flightNum)==0){if(c==1){if((op2=fopen("./flight/order.dat","wb"))==NULL){printf("无法打开文件!系统将返回上一级!\n");return;}for(int k=0;x<b;k++){if((fwrite(&o[k],sizeof(ORDER),1,op2))!=1)printf("信息写入文件错误!\n");}for(int k=b+1;k<a;k++){if((fwrite(&o[k],sizeof(ORDER),1,op2))!=1)printf("信息写入文件错误!\n");}fclose(op2);}}}}void searchByFligtNum(){FLIGHT f[NUM];char str[SIZE];FILE* fp;int i=0;int flag=0;printf("请输入您想要查询的航班号: ");scanf("%s",str);if((fp=fopen("./flight/flight.dat","rb"))==NULL){printf("无法打开文件!系统将返回上一级!\n");return;}while(fread(&f[i],sizeof(FLIGHT),1,fp)){i++;}for(int j=0;j<i;j++){if(strcmp(str,f[j].flightNum)==0){printf("查询到相关信息,如下所示:\n");printf("航班号 起飞时间降落时间 起飞地目的地 原价 折扣现价剩余票数\n");printf("%-s %-s %-s %-8s%-s %8.2f%8.2f %8.2f%3d\n",f[j].flightNum,f[j].take_off_Time,f[j].land_Time,f[j].take_off_City,f[j].land_City,f[j].pre_Price,f[j].discount,f[j].now_Price,f[j].tickets);flag=1;}}if(flag==0)printf("您所要查询的航班并不存在!\n");printf("\n");fclose(fp);} void searchByAddr(){FLIGHT f[NUM];char str1[SIZE];char str2[SIZE];FILE* fp;int i=0;int flag=0;printf("请输入您想要查询的航班的起飞地和目的地: ");scanf("%s%s",str1,str2);if((fp=fopen("./flight/flight.dat","rb"))==NULL){printf("无法打开文件!系统将返回上一级!\n");return;}while(fread(&f[i],sizeof(FLIGHT),1,fp)){i++;}for(int j=0;j<i;j++){if((strcmp(str1,f[j].take_off_City)==0)&&(strcmp(str2,f[j].land_City)==0)){printf("查询到相关信息,如下所示:\n");printf("航班号 起飞时间降落时间 起飞地目的地 原价 折扣现价剩余票数\n");printf("%-s %-s %-s %-8s%-s %8.2f%8.2f %8.2f%3d\n",f[j].flightNum,f[j].take_off_Time,f[j].land_Time,f[j].take_off_City,f[j].land_City,f[j].pre_Price,f[j].discount,f[j].now_Price,f[j].tickets);flag=1;}}if(flag==0)printf("您所要查询的航班并不存在!\n");printf("\n");fclose(fp);}void search(){int i;system("cls");while(1){printf("**************************欢迎使用航班搜索系统**************************\n");printf(" @1.按航班号搜索@\n");printf(" @2.按起飞和降落地址搜索@\n");printf(" @3.退 出 @\n");printf("************************************************************************\n");printf("请选择: ");scanf("%d",&i);switch(i){case 1:searchByFligtNum();break;case 2:searchByAddr();break; case 3:printf("\n");return; default:printf("\n");return;}}}void add_order(){FILE* op;FILE* fp1;FILE* fp2;ORDER o[NUM];FLIGHT f[NUM];char str[SIZE];int i=0;int k=0;int n;int x;int flag=0; if((op=fopen("./flight/order.dat","ab"))==NULL){printf("无法打开文件!系统将返回上一级!\n");return;}if((fp1=fopen("./flight/flight.dat","rb"))==NULL){printf("无法打开文件!系统将返回上一级!\n");return;}while(fread(&f[i],sizeof(FLIGHT),1,fp1)){i++;}fclose(fp1);printf("请输入预计要订购的机票数: ");scanf("%d",&n);printf("请按照顺序(用户姓名,证件号,航班号)依次输入相应信息:\n");while(1){scanf("%s%s%s",o[k].name,o[k].userId,o[k].order.flightNum);for(int j=0;j<i;j++){if(strcmp(o[k].order.flightNum,f[j].flightNum)==0){if(f[j].tickets>=n){strcpy(o[k].order.take_off_Time,f[j].take_off_Time);strcpy(o[k].order.land_Time,f[j].take_off_Time);strcpy(o[k].order.take_off_City,f[j].take_off_City);strcpy(o[k].order.land_City,f[j].land_City);strcpy(str,f[j].flightNum);strcpy(o[k].orderId,strcat(str,o[k].userId));o[k].order.price=f[j].now_Price;f[j].tickets--;flag=1;}else {printf("余票不足!您可以通过查询的结果订购其他航班!\n");return;}}}if(flag==0){printf("没有相应的航班!\n\n");break;} if((fwrite(&o[k],sizeof(ORDER),1,op))!=1)printf("信息写入文件错误!\n");k++;if(k==n){int x; printf("已达到您预计要订购的机票数量,是否想要继续订购,若需要请输入要继续订购的数量,若要退出请按0: ");scanf("%d",&x);if(x==0)break;else {printf("请输入:\n");n+=x;}}}if((fp2=fopen("./flight/flight.dat","wb"))==NULL){printf("无法打开文件!系统将返回上一级!\n");return;}for(int j=0;j<i;j++){if((fwrite(&f[j],sizeof(FLIGHT),1,fp2))!=1)printf("信息写入文件错误!\n");}if(flag==1)printf("订票完成!\n\n");fclose(fp2);fclose(op);}void check_all(){ORDER o[NUM];FILE* op;int i=0;if((op=fopen("./flight/order.dat","rb"))==NULL){printf("无法打开文件!系统将返回上一级!\n");return;}printf("所有订单的信息如下所示:\n");printf(" 订单号姓名证件号 航班号起飞时间降落时间 起飞地目的地 价格\n");while(fread(&o[i],sizeof(ORDER),1,op)) {printf("%s %-7s %7s %7s %7s %7s %7s %7s %6.2f\n",o[i].orderId,o[i].name,o[i].userId,o[i].order.flightNum,o[i].order.take_off_Time,o[i].order.land_Time,o[i].order.take_off_City,o[i].order.land_City,o[i].order.price);i++;}printf("\n");fclose(op);}void check_person(){ORDER o[NUM];char str[SIZE];FILE* op;int i=0;printf("请输入用户的证件号: ");scanf("%s",str);if((op=fopen("./flight/order.dat","rb"))==NULL){printf("无法打开文件!系统将返回上一级!\n");return;}while(fread(&o[i],sizeof(ORDER),1,op)){i++;}printf("查询到相关信息,如下所示:\n");printf(" 订单号姓名证件号 航班号起飞时间降落时间 起飞地目的地 价格\n");for(int j=0;j<i;j++){if(strcmp(str,o[j].userId)==0){printf("%s %-7s %7s %7s %7s %7s %7s %7s %6.2f\n",o[j].orderId,o[j].name,o[j].userId,o[j].order.flightNum,o[j].order.take_off_Time,o[j].order.land_Time,o[j].order.take_off_City,o[j].order.land_City,o[j].order.price);}}printf("\n");fclose(op);}void check_order(){int i;system("cls");while(1){printf("**************************欢迎使用订单查看系统**************************\n");printf(" @1.查看所有订单@\n");printf(" @2.查看单个用户订单@\n");printf(" @3.退 出 @\n");printf("************************************************************************\n");printf("请选择: ");scanf("%d",&i);switch(i){case 1:check_all();break;case 2:check_person();break; case 3:printf("\n");return; default:printf("\n");break;}}}void change_order(){FILE* fp1;FILE* fp2;FILE* op1;FILE* op2;FLIGHT f[NUM];ORDER o[NUM];char str1[SIZE];char str2[SIZE];char str3[SIZE];char str4[SIZE];int i=0;int j=0;int k;int x;int flag=0;int fflag=0;if((op1=fopen("./flight/order.dat","rb"))==NULL){printf("无法打开文件!系统将返回上一级!\n");return;}if((fp1=fopen("./flight/flight.dat","rb"))==NULL){printf("无法打开文件!系统将返回上一级!\n");return;}while(fread(&f[i],sizeof(FLIGHT),1,fp1)){i++;}while(fread(&o[j],sizeof(ORDER),1,op1)){j++;} fclose(fp1);fclose(op1);printf("请输入需要修改订单的订单号: ");scanf("%s",str1);for(k=0;k<j;k++){if(strcmp(str1,o[k].orderId)==0){printf("已找到该订单,信息如下:\n");printf(" 订单号姓名证件号 航班号起飞时间降落时间 起飞地目的地 价格\n");printf("%s %-7s %7s %7s %7s %7s %7s %7s %6.2f\n",o[k].orderId,o[k].name,o[k].userId,o[k].order.flightNum,o[k].order.take_off_Time,o[k].order.land_Time,o[k].order.take_off_City,o[k].order.land_City,o[k].order.price);strcpy(str2,o[k].order.flightNum);flag=1;x=k;}}if(flag==1){printf("请输入新航班的航班号: ");scanf("%s",str3);for(int a=0;a<i;a++){if(strcmp(str3,f[a].flightNum)==0){if(f[a].tickets>=1){strcpy(o[x].order.take_off_Time,f[a].take_off_Time);strcpy(o[x].order.land_Time,f[a].take_off_Time);strcpy(o[x].order.take_off_City,f[a].take_off_City);strcpy(o[x].order.land_City,f[a].land_City);strcpy(str4,f[a].flightNum);strcpy(o[x].orderId,strcat(str4,o[x].userId));o[x].order.price=f[a].now_Price;f[a].tickets--;fflag=1; }else {printf("余票不足!\n\n");return;}}}if(fflag==1){for(int b=0;b<i;b++){if(strcmp(str2,f[b].flightNum)==0){f[b].tickets++;}}}if(fflag==0){printf("该航班并不存在!\n\n");return;} }if(flag==0){printf("未查询到该订单信息!\n\n");return;}if(flag==1&&fflag==1){if((fp2=fopen("./flight/flight.dat","wb"))==NULL){printf("无法打开文件!系统将返回上一级!\n");return;} for(int c=0;c<i;c++){if((fwrite(&f[c],sizeof(FLIGHT),1,fp2))!=1)printf("信息写入文件错误!\n");}if((op2=fopen("./flight/order.dat","wb"))==NULL){printf("无法打开文件!系统将返回上一级!\n");return;} for(int c=0;c<j;c++){if((fwrite(&o[c],sizeof(ORDER),1,op2))!=1)printf("信息写入文件错误!\n");}fclose(fp2);fclose(op2);printf("订单修改完成!\n\n");}}void return_order(){FILE* op1;FILE* op2;FILE* fp1;FILE* fp2;char str[SIZE];ORDER o[NUM];FLIGHT f[NUM];int i=0;int j=0;int c=0;int flag=0; if((op1=fopen("./flight/order.dat","rb"))==NULL){printf("无法打开文件!系统将返回上一级!\n");return;}if((fp1=fopen("./flight/flight.dat","rb"))==NULL){printf("无法打开文件!系统将返回上一级!\n");return;}while(fread(&f[i],sizeof(FLIGHT),1,fp1)){i++;}while(fread(&o[j],sizeof(ORDER),1,op1)){j++;} fclose(fp1);fclose(op1);printf("请输入订单号:\n");scanf("%s",str);for(int k=0;k<j;k++){if(strcmp(str,o[k].orderId)==0){printf("已查找到该订单,信息如下:\n");printf(" 订单号姓名证件号 航班号起飞时间降落时间 起飞地目的地 价格\n");printf("%s %-7s %7s %7s %7s %7s %7s %7s %6.2f\n",o[k].orderId,o[k].name,o[k].userId,o[k].order.flightNum,o[k].order.take_off_Time,o[k].order.land_Time,o[k].order.take_off_City,o[k].order.land_City,o[k].order.price);printf("确认要退票吗?(1 确认/0 取消):");scanf("%d",&c);if(c==1){if((op2=fopen("./flight/order.dat","wb"))==NULL){printf("无法打开文件!系统将返回上一级!\n");return;}for(int x=0;x<k;x++){if((fwrite(&o[x],sizeof(ORDER),1,op2))!=1)printf("信息写入文件错误!\n");}for(int x=k+1;x<j;x++){if((fwrite(&o[x],sizeof(ORDER),1,op2))!=1)printf("信息写入文件错误!\n");} f[k].tickets++;flag=1;fclose(op2);}else {printf("取消退票!\n\n");return;}}} if((fp2=fopen("./flight/flight.dat","wb"))==NULL){printf("无法打开文件!系统将返回上一级!\n");return;} for(int x=0;x<i;x++){if((fwrite(&f[x],sizeof(FLIGHT),1,fp2))!=1)printf("信息写入文件错误!\n");}fclose(fp2);if(flag==0)printf("您所要退订的航班并不存在!\n\n");if(flag==1)printf("退票完成!\n\n");}int main(){int i;while(1){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(" ###9 退 票 ###\n");printf(" @@@0 退 出 程 序 @@@\n");printf(" #####!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!#####\n");printf("请选择: ");scanf("%d",&i);switch(i){case 1:input_flight();break;case 2:check_flight();break;case 3:search();break; case 4:change_flight();break;case 5:delete_flight();break; case 6:add_order();break; case 7:check_order();break; case 8:change_order();break; case 9:return_order();break; case 0:exit(0); default:break;}}return 0;}

2.航班信息录入

3.查看航班信息

4.航班信息查询

5.修改航班信息

6.删除航班信息

7.订票

8.查看订单

9.修改订单

10.退票

如果有用还请点个赞~