本篇内容介绍了“C语言如何实现航空订票系统”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
利用二进制将一整个结构体中的数据存入文件,然后读取文件时再用二进制的形式将结构体中的数据读取出来,边读取边创建链表,将结构体中的数据保存到链表中。
如果有第一次写这种大作业不知道如何下手,但又不想直接复制粘贴,想搞清楚这个代码的原理的可以私信我,毕竟我当时也第一次写这种大作业时也很苦恼(我当时写的要是有这么完整的代码该多好啊,我直接就是复制粘贴运行看效果)
// author:吴泽琛// nylg#include <stdio.h>#include <stdlib.h>#include <windows.h>#include <string.h>#include <conio.h>#include <iostream>#define PRINTF "%s\t\t%d\t\t%s\t\t\t%s\t\t星期%d\t\t\t%d\n",airlinep->airlinenum,airlinep->airplanenum,airlinep->dep,airlinep->des,airlinep->time,airlinep->ticksusing namespace std;typedef class mange{ public: char mangername[20]; char scert[20]; char securityscert[20]; class mange *next;}manger;typedef class use{ public:// char userairlinenum[20];//航班号// int userairplanenum;//飞机号 // char userdes;//目的地 // char userdep;//出发时间 // int usertime;//起飞时间 int ticks;//票数 int bookcnt;//订票次数 char username[20]; char scert[20]; char securityscert[20]; char bookticks[2000][2000];//机票名字 class use *next;}user;typedef class airline{ public: char airlinenum[20];//航班号 int airplanenum;//飞机号 char dep[1000];//出发地点 char des[1000];//目的地 int time;//飞行时间(星期几) int ticks;//票数 class airline *next;}airline;manger* mhead = NULL, *mend = NULL;user *uhead = NULL, *uend = NULL;airline *ahead = NULL, *aend = NULL;airline *ahead1 = (airline*)malloc(sizeof(airline));int mcnt, ucnt, acnt;char mscert[200] = {"wsgly"};void load();void main1();void changemenu();void mainmenu();void Registermenu();void Loginmenu();void mlmenu();void ulmenu();void Login();void Register();void uRegister();void mRegister();void mLogin();void uLogin();void userfunction();void search();void searchmenu();void printairline();void printdes();void printtime();void printticks();void airlinesort();void dessort();void bookticks(user *userp);void returnticks(user *userp);void findairline(user *userp); void mangerfunction();void deleteusernum(char *airlinenum);void addairline();void deletenum();void changeairline();void searchuser();void changeuserairline(char *userbookticks, char *bookticks);void rmanger(manger *mhead);void rairline(airline *ahead);void ruser(user *uhead);void readmangerfile();void readuserfile();void readairlinefile();void title();void date();void cls();void over();void writefile(int flag);int exist(char *name, int flag);void hualihushao1();void hualihushao2();void searchscert();void searchmangerscert();void searchuserscert();void changescert();void changemangerscert();void changeuserscert();void print_message();int main(){ //hualihushao1(); system("color F6"); title(); mhead = (manger*)malloc(sizeof(manger)); uhead = (user*)malloc(sizeof(user)); ahead = (airline*)malloc(sizeof(airline)); ahead1->next = ahead; ruser(uhead); rmanger(mhead); rairline(ahead); // test// user *q = uhead;// while (q)// {// printf("%d %s %s %d %s\n",q->ticks,q->username,q->scert,q->bookcnt,q->securityscert);// q = q->next;// } airline *p = ahead; while (p) { printf("%s %d %s %s %d %d\n",p->airlinenum,p->airplanenum,p->dep,p->des,p->time,p->ticks); p = p->next; } main1(); return 0;}void main1(){ int choose; int sign; while (1) { cls(); date(); mainmenu(); char choose = getch(); switch(choose) { case '1': cls(); Login(); break; case '2': cls(); Register(); break; case '3': cls(); searchscert(); break; case '4': cls(); changescert(); break; case '5': cls(); over(); cls(); break; default : printf("输入无效!"); break; } } }void Login() { int sign; while (1) { cls(); date(); Loginmenu(); char choose = getch(); switch(choose) { case '1': cls(); mLogin(); break; case '2': cls(); uLogin(); break; case '3': sign = 1; break; case '4': cls(); over(); break; default : printf("输入无效!"); break; } if (sign) { break; } } }void Register(){ int sign; while (1) { cls(); date(); Registermenu(); char choose = getch(); switch(choose) { case '1': cls(); mRegister(); break; case '2': cls(); uRegister(); break; case '3': sign = 1; cls(); break; case '4': cls(); over(); cls(); break; default : printf("输入无效!"); break; } if (sign) { break; } } }void userfunction(user *userp){ int sign; while (1) { cls(); date(); ulmenu(); char choose = getch(); switch(choose) { case '1': cls(); bookticks(userp); break; case '2': cls(); returnticks(userp); break; case '3': cls(); findairline(userp); break; case '4': cls(); search(); break; case '5': sign = 1; cls(); break; case '6': cls(); over(); cls(); break; default : printf("输入无效!"); break; } if (sign) { break; } } }void mangerfunction(){ int sign = 0; int choose ; while (1) { cls(); date(); mlmenu(); char choose = getch(); switch(choose) { case '1': cls(); addairline(); break; case '2': cls(); deletenum(); break; case '3': cls(); changeairline(); break; case '4': cls(); searchuser(); break; case '5': cls(); search(); break; case '6': sign = 1; cls(); break; case '7': cls(); over(); cls(); break; default : printf("输入无效!"); break; } if (sign) { break; } } }void mlmenu(){ printf(" 管理员页面\n"); printf(" **************************************************\n"); printf(" ** 1.增加航班信息 **\n"); printf(" ** **\n"); printf(" ** 2.删除航班信息 **\n"); printf(" ** **\n"); printf(" ** 3.修改航班信息 **\n"); printf(" ** **\n"); printf(" ** 4.查看客户信息 **\n"); printf(" ** **\n"); printf(" ** 5.浏览航班信息 **\n"); printf(" ** **\n"); printf(" ** 6.返回 **\n"); printf(" ** **\n"); printf(" ** 7.退出 **\n"); printf(" **************************************************\n"); printf("请按键选择:");}void ulmenu(){ printf(" 乘客页面\n"); printf(" **************************************************\n"); printf(" ** 1.订票 **\n"); printf(" ** **\n"); printf(" ** 2.退票 **\n"); printf(" ** **\n"); printf(" ** 3.查看已订机票 **\n"); printf(" ** **\n"); printf(" ** 4.浏览航班信息 **\n"); printf(" ** **\n"); printf(" ** 5.返回 **\n"); printf(" ** **\n"); printf(" ** 6.退出 **\n"); printf(" **************************************************\n"); printf("请按键选择:");}void mainmenu(){ printf("\n\n"); printf(" 欢 迎 使 用 航 空 管 理 系 统 !\n"); printf(" **************************************************\n"); printf(" ** 1.登录 **\n"); printf(" ** **\n"); printf(" ** 2.注册 **\n"); printf(" ** **\n"); printf(" ** 3.找回密码 **\n"); printf(" ** **\n"); printf(" ** 4.修改密码 **\n"); printf(" ** **\n"); printf(" ** 5.退出 **\n"); printf(" **************************************************\n"); printf("请按键选择:");}void Registermenu(){ printf(" 注册页面\n"); printf(" **************************************************\n"); printf(" ** 1.管理员注册 **\n"); printf(" ** **\n"); printf(" ** 2.乘客注册 **\n"); printf(" ** **\n"); printf(" ** 3.返回 **\n"); printf(" ** **\n"); printf(" ** 4.退出 **\n"); printf(" **************************************************\n"); printf("请按键选择:");}void Loginmenu(){ printf(" 登录页面\n"); printf(" **************************************************\n"); printf(" ** 1.管理员登录 **\n"); printf(" ** **\n"); printf(" ** 2.用户登录 **\n"); printf(" ** **\n"); printf(" ** 3.返回 **\n"); printf(" ** **\n"); printf(" ** 4.退出 **\n"); printf(" **************************************************\n"); printf("请按键选择:");}void changemenu(){ printf("\n\n"); printf("\t\t\t\t*******************************\n"); printf("\t\t\t\t** 1.修改航班号 **\n"); printf("\t\t\t\t** **\n"); printf("\t\t\t\t** 2.修改飞机号 **\n"); printf("\t\t\t\t** **\n"); printf("\t\t\t\t** 3.修改出发地点 **\n"); printf("\t\t\t\t** **\n"); printf("\t\t\t\t** 4.修改目的地 **\n"); printf("\t\t\t\t** **\n"); printf("\t\t\t\t** 5.修改起飞时间 **\n"); printf("\t\t\t\t** **\n"); printf("\t\t\t\t** 6.修改票数 **\n"); printf("\t\t\t\t** **\n"); printf("\t\t\t\t** 7.返回 **\n"); printf("\t\t\t\t*******************************"); printf("\n\n");}void searchmenu(){ printf("\n\n\n\n"); printf("\t\t\t\t***********************************\n"); printf("\t\t\t\t** 1.查看航班信息 **\n"); printf("\t\t\t\t** **\n"); printf("\t\t\t\t** 2.按航班号浏览 **\n"); printf("\t\t\t\t** **\n"); printf("\t\t\t\t** 3.按起飞时间浏览 **\n"); printf("\t\t\t\t** **\n"); printf("\t\t\t\t** 4.按机票数浏览 **\n"); printf("\t\t\t\t** **\n"); printf("\t\t\t\t** 5.返回 **\n"); printf("\t\t\t\t** **\n"); printf("\t\t\t\t** 6.退出 **\n"); printf("\t\t\t\t***********************************\n"); printf("请按键选择:");}void ruser(user *uhead){ FILE *fp; fp = fopen("user","a+"); ucnt = fread(uhead,sizeof(user),1,fp); fclose(fp); uend = uhead; readuserfile();}void rmanger(manger *mhead){ FILE *fp; fp = fopen("manger","a+"); mcnt = fread(mhead,sizeof(manger),1,fp); fclose(fp); mend = mhead; readmangerfile();}void rairline(airline *ahead){ FILE *fp; fp = fopen("airline","a+"); acnt = fread(ahead,sizeof(airline),1,fp); fclose(fp); aend = ahead; readairlinefile();}void uRegister(){ date(); user *p; char username[1000]; char scert[1000]; char securityscert[2000]; FILE *fp; if (ucnt == 0) //第一次添加 { printf("\n\n\n\n\n\n\n"); printf("-------------请输入账户(按0退出):"); while (gets(username)) { if (strcmp(username,"0") == 0) { return ; } if (strlen(username) <= 16 && !exist(username, 0)) break; else if (exist(username, 0)) { printf("-------------该账户已存在!请重新输入(按0退出):"); } else { printf("-------------请输入长度为1到16位的账户:"); } } strcpy(uhead->username, username); printf("-------------请输入密码:"); while (gets(scert)) { if (strlen(scert) <= 16) break; else printf("-------------请输入长度为1到16的密码:"); } strcpy(uhead->scert, scert); printf("-------------请输入您的密保:"); while (gets(securityscert)) { if (strlen(securityscert) <= 16) { break; } else { printf("-------------请输入长度为1到16位的密保:"); } } strcpy(uhead->securityscert,securityscert); uhead->bookcnt = 0; uend = uhead; uend->next = NULL; } else { p = (user*)malloc(sizeof(user)); printf("\n\n\n\n\n\n\n"); printf("-------------请输入账户(按0退出):"); while (gets(username)) { if (strcmp(username,"0") == 0) { return ; } if (strlen(username) <= 16 && !exist(username, 0)) break; else if (exist(username, 0)) { printf("-------------该账户已存在!请重新输入(按0退出):"); } else { printf("-------------请输入长度为1到16位的账户:"); } } strcpy(p->username, username); printf("-------------请输入密码:"); while (gets(scert)) { if (strlen(scert) <= 16) break; else printf("-------------请输入长度为1到16的密码:"); } strcpy(p->scert, scert); printf("-------------请输入您的密保:"); while (gets(securityscert)) { if (strlen(securityscert) <= 16) { break; } else { printf("-------------请输入长度为1到16位的密保:"); } } strcpy(p->securityscert,securityscert); p->bookcnt = 0; uend->next = p; uend = p; uend->next = NULL; } ucnt++; writefile(0); cls(); printf("\n\n\n\n\n\n\n"); printf("\t\t\t\t\t*******************************\n"); printf("\t\t\t\t\t** **\n"); printf("\t\t\t\t\t 乘客%s创建成功! \n",uend->username); printf("\t\t\t\t\t** 即将跳转至主页面... **\n"); printf("\t\t\t\t\t** **\n"); printf("\t\t\t\t\t*******************************\n"); Sleep(3000);}int exist(char *name, int flag)//1 为存在 0 为不存在 { if (flag == 0) { user *p = uhead; while (p != NULL && ucnt != 0) { if (strcmp(name,p->username) == 0) { return 1; } p = p->next; } } else if (flag == 1) { manger *p = mhead; while (p != NULL) { if (strcmp(name,p->mangername) == 0) { return 1; } p = p->next; } } return 0;}void uLogin() { date(); int cnt = 4; int flag = 0; int k = 0; int i; char c; user *p = uhead; char username[2000]; char scert[2000]; printf("\n\n\n\n\n\n\n"); FILE *fp; fp = fopen("user","r"); char ch; ch = fgetc(fp); rewind(fp); if (ch == EOF) { printf("-------------暂无乘客账号信息!!\n-------------请先前往注册!"); getchar(); return ; } printf("-------------请输入您的账户(按0退出):");// gets(username);// getch(); while (gets(username)) { if (strcmp(username,"0") == 0) { return ; } if (strlen(username) <= 16) { break; } else { printf("-------------请输入长度为1到16位的账户:"); } } printf("-------------请输入您的密码(按0退出):");// gets(scert for (i = 0; (c = getch()) != '\r';) { if (c == '0') { return ; } if (i <= 15 && isprint(c)) { scert[i] = c; i ++; k ++; printf("*"); } if ( k > 0 && c > 0 && c == '\b') { i --; k --; printf("\b"); printf(" "); printf("\b"); } } scert[i] = '\0'; printf("\n"); while (p != NULL) { if (strcmp(p->username,username) == 0) { flag = 1; while (strcmp(p->scert,scert) != 0) { cnt --; if (cnt == 1) { cls(); printf("\n\n\n\n\n\n\n"); printf("-------------密码输入次数已用完!\n-------------即将退出登录页面..."); Sleep(3000); cls(); return ; } printf("-------------密码错误!您还有%d次机会!\n",cnt - 1); printf("-------------请重新输入:"); gets(scert); } printf("-------------乘客%s登录成功!\n-------------即将跳转至乘客页面...",p->username); Sleep(3000); cls(); userfunction(p); } p = p->next; } if (flag == 0) { printf("-------------不存在此账号!\n-------------请先前往注册!\n"); printf("-------------即将跳转至主页面..."); Sleep(2000); }}void mLogin(){ date(); int cnt = 4; int flag = 0; int k = 0; int i; char c; manger *p = mhead; char mangername[2000]; char scert[2000]; printf("\n\n\n\n\n\n\n"); FILE *fp; fp = fopen("manger","r"); char ch; ch = fgetc(fp); rewind(fp); if (ch == EOF) { printf("-------------暂无管理员账号信息!!\n-------------请先前往注册!"); getchar(); return ; } printf("-------------请输入您的账户(按0退出):"); while (gets(mangername)) { if (strcmp(mangername,"0") == 0) { return ; } if (strlen(mangername) <= 16) { break; } else { printf("-------------请输入长度为1到16位的账户:"); } } printf("-------------请输入您的密码(按0退出):"); for (i = 0; (c = getch()) != '\r';) { if (c == '0') { return ; } if (i <= 15 && isprint(c)) { scert[i] = c; i ++; k ++; printf("*"); } if ( k > 0 && c > 0 && c == '\b') { i --; k --; printf("\b"); printf(" "); printf("\b"); } } scert[i] = '\0'; printf("\n"); while (p) { if (strcmp(p->mangername,mangername) == 0) { flag = 1; while (strcmp(p->scert,scert) != 0) { cnt --; if (cnt == 1) { cls(); printf("\n\n\n\n\n\n\n"); printf("-------------密码输入次数已用完!\n-------------即将退出登录页面..."); Sleep(3000); cls(); return ; } printf("-------------密码错误!您还有%d次机会!\n",cnt - 1); printf("-------------请重新输入:"); // gets(scert);for (i = 0; (c = getch()) != '\r';) { if (c == '0') { return ; } if (i <= 15 && isprint(c)) { scert[i] = c; i ++; k ++; printf("*"); } if ( k > 0 && c > 0 && c == '\b') { i --; k --; printf("\b"); printf(" "); printf("\b"); } } scert[i] = '\0'; printf("\n"); } printf("-------------管理员%s登录成功!\n-------------即将跳转至管理员页面...",p->mangername);// load(); //cls(); mangerfunction(); return ; } p = p->next; } if (flag == 0) { printf("-------------不存在此账号!\n-------------请先前往注册!\n"); printf("-------------即将跳转至主页面..."); Sleep(2000); }}void mRegister(){ date(); FILE *fp; char mangerscert[1000]; char mname[1000]; char scert[1000]; char securityscert[2000]; printf("\n\n\n\n\n\n\n"); printf("-------------请输入管理员验证码:");// getchar(); gets(mangerscert); int cnt = 2; while (--cnt >= 0) { if (strcmp(mangerscert,mscert) == 0) { printf("-------------管理员验证成功!!\n-------------即将进入注册页面..."); Sleep(1000); cls(); break; } else { printf("-------------管理员验证失败!!\n-------------您还有%d次机会!!\n",cnt + 1); printf("-------------请重新输入:"); gets(mangerscert); } if (cnt == 0) { printf("-------------输入次数已用完!!\n-------------即将退出管理员注册系统..."); Sleep(2000); cls(); return ; } } manger *p; if (mcnt == 0) { printf("\n\n\n\n\n\n\n"); printf("-------------请输入账户(按0退出):"); while (gets(mname)) { if (strcmp(mname,"0") == 0) { return ; } if (strlen(mname) <= 16 ) { break; } else { printf("-------------请输入长度为1到16位的账户:"); } } strcpy(mhead->mangername,mname); printf("-------------请输入密码:"); while (gets(scert)) { if (strlen(scert) <= 16) { break; } else { printf("-------------请输入长度为1到16位密码:"); } } strcpy(mhead->scert,scert); printf("-------------请输入您的密保:"); while (gets(securityscert)) { if (strlen(securityscert) <= 16) { break; } else { printf("-------------请输入长度为1到16位的密保:"); } } strcpy(mhead->securityscert,securityscert); mend = mhead; mend->next = NULL; } else { p = (manger*)malloc(sizeof(manger)); printf("\n\n\n\n\n\n\n"); printf("-------------请输入账户(按0退出):"); while (gets(mname)) { if (strcmp(mname,"0") == 0) { return ; } if (strlen(mname) <= 16 && !exist(mname,1)) { break; } else if (exist(mname,1)) { printf("-------------该账户已存在!请重新输入!(按0退出)"); } else { printf("-------------请输入长度为1到16位的账户:"); } } strcpy(p->mangername,mname); printf("-------------请输入密码:"); while (gets(scert)) { if (strlen(scert) <= 16) { break; } else { printf("-------------请输入长度为1到16位的密码:"); } } strcpy(p->scert,scert); printf("-------------请输入您的密保:"); while (gets(securityscert)) { if (strlen(securityscert) <= 16) { break; } else { printf("-------------请输入长度为1到16位的密保:"); } } strcpy(p->securityscert,securityscert); mend->next = p; mend = p; mend->next = NULL; } mcnt ++; writefile(1); cls(); printf("\n\n\n\n\n\n\n"); printf("\t\t\t\t\t*******************************\n"); printf("\t\t\t\t\t** **\n"); printf("\t\t\t\t\t 管理员%s创建成功! \n",mend->mangername); printf("\t\t\t\t\t** 即将跳转至主页面... **\n"); printf("\t\t\t\t\t** **\n"); printf("\t\t\t\t\t*******************************\n"); Sleep(3000);}void title(){ system("color F2"); system("title **************************************************************************欢迎使用航空订票系统****************************************************************************"); }void date(){ system("date/t"); system("time/t");}void cls(){ system("cls");}void over(){ //hualihushao2(); cls(); system("color 6"); int time = 6; printf("\n\n\n\n\n\n\n\n\n"); printf("\t\t\t\t\t\t感谢您的使用!!\n\n\n\n\n"); printf(" 程序即将关闭..."); while (--time >= 1) { printf("\r%d",time); printf("\b"); Sleep(1000); } cls(); exit(0); }void readuserfile(){ user *p = uhead; FILE *fp = fopen("user","ab+"); while (fread(p,sizeof(user),1,fp)) { if (p->next != NULL) { p = (user*)malloc(sizeof(user)); uend->next = p; uend = p; uend->next = NULL; } }}void readmangerfile(){ manger *p; FILE *fp; p = mhead; fp = fopen("manger", "ab+"); while (fread(p, sizeof(manger), 1, fp)) { if (p->next != NULL) { p = (manger*)malloc(sizeof(manger)); mend->next = p; mend = p; mend->next = NULL; } }}void readairlinefile(){ airline *p; FILE *fp; p = ahead; fp = fopen("airline","ab+"); while (fread(p,sizeof(airline),1,fp)) { if(p->next != NULL) { p = (airline*)malloc(sizeof(airline)); aend->next = p; aend = p; aend->next = NULL; } }}void addairline(){ FILE *fp; airline *p; char airlinenum[2000]; if (acnt == 0) { printf("-------------请输入航班号(按0返回):");// getchar(); while (gets(airlinenum)) { if (strcmp(airlinenum,"0") == 0) { return ; } if (strlen(airlinenum) <= 16) { break; } else { printf("-------------请输入长度为1到16位的航班号:"); } } strcpy(ahead->airlinenum,airlinenum); printf("-------------请输入飞机号:");// getchar(); long long int airplanenum; while (scanf("%lld",&airplanenum)) { if (airplanenum <= 0) { printf("-------------请输入正确格式的飞机号:"); } else if (airplanenum > 999999) { printf("-------------请输入长度为1-6位的飞机号:"); } else { ahead->airplanenum = airplanenum; break; } } printf("-------------请输入出发地点:"); getchar(); char dep[1000]; while (gets(dep)) { if (strlen(dep) <= 16) { strcpy(ahead->dep,dep); break; } else { printf("-------------请输入长度为1到16位的出发地点:"); } }// getchar(); printf("-------------请输入目的地:"); char des[1000]; while (gets(des)) { if (strlen(des) <= 16) { strcpy(ahead->des,des); break; } else { printf("-------------请输入长度为1到16位的目的地:"); } }; printf("-------------请输入机票数:"); int ticks; while (scanf("%d",&ticks)) { if (ticks <= 0) { printf("-------------请输入正确格式的机票数!"); } else if (ticks > 999) { printf("-------------请输入0-999以内的机票数!"); } else { ahead->ticks = ticks; break; } } int time; printf("-------------请输入起飞时间(1-7):"); while (scanf("%d",&time)) { if (time < 1 || time > 7) { printf("-------------请输入正确的起飞时间(周一到周日用1-7表示):"); } else { ahead->time = time; break; } } aend = ahead; aend->next = NULL; } else { printf("-------------请输入航班号(按0返回):"); getchar(); while (gets(airlinenum)) { if (strcmp(airlinenum,"0") == 0) { return ; } if (strlen(airlinenum) <= 16) { break; } else { printf("-------------请输入长度为1到16位的航班号:"); } } int flag = 0; p = ahead; while (p) { if (strcmp(p->airlinenum,airlinenum) == 0) { flag = 1; break; } p = p->next; } if(flag == 1) { int ticks; printf("-------------请输入要增加票的数量:"); while (scanf("%d",&ticks)) { if (ticks <= 0) { printf("-------------请输入正确格式的机票数!"); } else if (ticks > 999) { printf("-------------请输入0-999以内的机票数!"); } else { p->ticks += ticks; printf("-------------航班号%s票数添加成功!",p->airlinenum); break; } } } else { p = (airline*)malloc(sizeof(airline)); strcpy(p->airlinenum,airlinenum); printf("-------------请输入飞机号:"); long long int airplanenum; while (scanf("%lld",&airplanenum)) { if (airplanenum <= 0) { printf("-------------请输入正确格式的飞机号:"); } else if (airplanenum > 999999) { printf("-------------请输入长度为1-6位的飞机号:"); } else { p->airplanenum = airplanenum; break; } } printf("-------------请输入出发地点:"); getchar(); char dep[1000]; while (gets(dep)) { if (strlen(dep) <= 16) { strcpy(p->dep,dep); break; } else { printf("-------------请输入长度为1到16位的出发地点:"); } } printf("-------------请输入目的地:"); char des[1000]; while (gets(des)) { if (strlen(des) <= 16) { strcpy(p->des,des); break; } else { printf("-------------请输入长度为1到16位的目的地:"); } } printf("-------------请输入机票数:"); int ticks; while (scanf("%d",&ticks)) { if (ticks < 0) { printf("-------------请输入正确格式的机票数!"); } else if (ticks > 999) { printf("-------------请输入0-999以内的机票数!"); } else { p->ticks = ticks; break; } } int time; printf("-------------请输入起飞时间(1-7):"); while (scanf("%d",&time)) { if (time < 1 || time > 7) { printf("-------------请输入正确的起飞时间(周一到周日用1-7表示):"); } else { p->time = time; break; } } getchar(); aend->next = p; aend = p; aend->next = NULL; } } acnt ++; writefile(2); cls(); printf("\n\n\n\n\n\n"); printf("\t\t\t\t\t********************************\n"); printf("\t\t\t\t\t** **\n"); printf("\t\t\t\t\t 航班号%s添加成功! \n",aend->airlinenum); printf("\t\t\t\t\t** **\n"); printf("\t\t\t\t\t********************************\n"); printf("页面即将跳转..."); Sleep(2000); }void writefile(int flag)// 0 写用户 1 写管理员 2 写航班 { user *userp = uhead; manger *mangerp = mhead; airline *airlinep = ahead; FILE *fp; if (flag == 0) { fp = fopen("user","w"); while (userp) { fwrite(userp,sizeof(user),1,fp); userp = userp->next; } } else if(flag == 1) { fp = fopen("manger","w"); while (mangerp) { fwrite(mangerp,sizeof(manger),1,fp); mangerp = mangerp->next; } } else if(flag == 2) { fp = fopen("airline","w"); while (airlinep) { fwrite(airlinep,sizeof(airline),1,fp); airlinep = airlinep->next; } } fclose(fp);}void deletenum(){ airline *p, *sign; p = ahead; char airlinenum[1000]; int flag = 0; FILE *fp; fp = fopen("airline","r"); char ch; ch = fgetc(fp); rewind(fp); if (ch == EOF) { printf("\n\n\n\n\n\n\n"); printf("-------------暂无航班信息!!"); getchar(); printf("pause"); return ; } printf("-------------请输入您要删除的航班号(按0退出):"); while (scanf("%s",airlinenum)) { if (strcmp("0",airlinenum) == 0) { getchar(); return ; } while (p) { if (strcmp(p->airlinenum,airlinenum) == 0) { flag = 1; break; } p = p->next; } if (flag == 0) { printf("-------------查无此航班!"); printf("请重新输入(按0退出):"); p = ahead; } else { break; } } deleteusernum(airlinenum); p = ahead; if (strcmp(ahead->airlinenum,airlinenum) == 0) { sign = ahead; strcpy(sign->airlinenum,ahead->airlinenum); ahead = ahead->next; ahead1->next = ahead; } while (p->next != NULL) { if (strcmp(p->next->airlinenum,airlinenum) == 0) { sign = p->next; strcpy(sign->airlinenum,p->next->airlinenum); p->next = p->next->next; break; } p = p->next; } getchar(); printf("\n\n\n\n\n\n"); printf("-------------航班号%s删除成功!\n",sign->airlinenum); printf("-------------页面即将跳转..."); Sleep(1000);// free(sign); writefile(2);}void deleteusernum(char *airlinenum){ user *userp = uhead; int flag; while (userp) { flag = 1; for (int i = 0; i < userp->bookcnt; i ++) { if (strcmp(userp->bookticks[i],airlinenum) == 0) { flag = i; break; } } if (flag != 1) { for (int j = flag; j < userp->bookcnt - 1; j ++) { strcpy(userp->bookticks[j],userp->bookticks[j + 1]); } userp->bookcnt --; } userp = userp->next; } writefile(0);}void changeairline(){ airline *airlinep = ahead1->next; char airlinenum[2000]; int airplanenum, time, ticks; char dep[2000], des[2000]; int exist = 0; int flag = 0; printf("\n\n\n\n\n\n"); FILE *fp; fp = fopen("airline","r"); char ch; ch = fgetc(fp); rewind(fp); if (ch == EOF) { printf("\n\n\n\n\n\n\n"); printf("-------------暂无航班信息!!"); getchar(); getchar(); return ; } printf("航班号\t\t飞机号\t\t出发地点\t\t目的地\t\t出发时间\t\t票数\n"); while (airlinep) { printf(PRINTF); airlinep = airlinep->next; } airlinep = ahead; printf("-------------请输入您要修改的航班号(按0返回):");// getchar(); while (gets(airlinenum)) { if (strcmp("0",airlinenum) == 0) { return ; } while (airlinep) { if (strcmp(airlinep->airlinenum,airlinenum) == 0) { exist = 1; break; } airlinep = airlinep->next; } if (!exist) { airlinep = ahead; printf("-------------未查询到该航班!请重新输入:"); } else { break; } } if (!exist) { return ; } else { int choose ; cls(); changemenu(); while (1) { printf("-------------请输入您要进行的操作:"); scanf("%d",&choose); switch(choose) { case 1: printf("-------------请输入修改后的航班号:"); scanf("%s",airlinenum); changeuserairline(airlinep->airlinenum,airlinenum); strcpy(airlinep->airlinenum,airlinenum); break; case 2: printf("-------------请输入修改后的飞机号:"); scanf("%lld",&airplanenum); airlinep->airplanenum = airplanenum; break; case 3: printf("-------------请输入修改后的出发地点:"); scanf("%s",dep); strcpy(airlinep->dep,dep); break; case 4: printf("-------------请输入修改后的目的地:"); scanf("%s",des); strcpy(airlinep->des,des); break; case 5: printf("-------------请输入修改后的起飞时间:"); while (scanf("%d",&time)) { if (time < 1 || time > 7) { printf("-------------请输入正确的起飞时间(周一到周日用1-7表示):"); } else { airlinep->time = time; break; } } break; case 6: printf("-------------请输入修改后的机票数:"); while (scanf("%d",&ticks)) { if (ticks <= 0) { printf("-------------请输入正确格式的机票数!"); } else if (ticks > 999) { printf("-------------请输入0-999以内的机票数!"); } else { airlinep->ticks = ticks; break; } } break; case 7: flag = 1; break; } if (flag) { break; } } } writefile(2);}void changeuserairline(char *userbookticks, char *bookticks){ user *userp = uhead; while (userp) { for (int i = 0; i < userp->bookcnt; i ++) { if (strcmp(userp->bookticks[i],userbookticks) == 0) { strcpy(userp->bookticks[i],bookticks); } } userp = userp->next; } writefile(0);}void searchuser(){ user *userp = uhead; char username[2000]; int flag = 0; printf("-------------请输入您要查询的乘客名(按0退出):"); gets(username); if (strcmp(username,"0") == 0) { return ; } while (userp) { if (strcmp(userp->username,username) == 0) { flag = 1; break; } userp = userp->next; } if (!flag) { cls(); printf("\n\n\n\n\n\n\n"); printf("\t\t\t\t\t*******************************\n"); printf("\t\t\t\t\t** **\n"); printf("\t\t\t\t\t** 不存在该乘客信息! **\n"); printf("\t\t\t\t\t** 即将跳转至上一页面... **\n"); printf("\t\t\t\t\t** **\n"); printf("\t\t\t\t\t*******************************\n"); Sleep(2000); return ; } else { long long int sign; while (1) { cls(); printf("\n\n"); printf("\t\t\t\t\t 乘客%s的个人信息\n",userp->username); printf("\t\t\t\t***********************************\n"); printf("\t\t\t\t 乘客账号:"); puts(userp->username); printf("\t\t\t\t** **\n"); printf("\t\t\t\t 乘客密码:"); puts(userp->scert); printf("\t\t\t\t** **\n"); printf("\t\t\t\t 订票次数:"); printf("%d\n",userp->bookcnt); printf("\t\t\t\t** **\n"); if (userp->bookcnt) { printf("\t\t\t\t 订票航班号:"); for (int i = 0; i < userp->bookcnt; i ++) { printf("%s ",userp->bookticks[i]); } printf("\n"); printf("\t\t\t\t** **\n"); } else { printf("\t\t\t 此乘客还未预订任何航班!\n"); } printf("请输入0返回上一页面!"); while (scanf("%lld",&sign)) { if (sign == 0) { return ; } else { printf("无效的输入!请重新输入:"); } } } }}void bookticks(user *userp){ char airlinenum[2000]; airline *airlinep = ahead1->next; int exist = 0; int sign = 1; printf("\n\n\n\n\n\n"); FILE *fp; fp = fopen("airline","r"); char ch; ch = fgetc(fp); if (ch == EOF) { printf("-------------暂无航班信息!!"); getchar(); return ; } printf("航班号\t\t飞机号\t\t出发地点\t\t目的地\t\t出发时间\t\t票数\n"); while (airlinep) { printf(PRINTF); airlinep = airlinep->next; } printf("-------------请输入您要预订的航班号(按0退出):"); getchar(); gets(airlinenum); airlinep = ahead; if (strcmp("0",airlinenum) == 0) { return ; } while (airlinep) { if (strcmp(airlinep->airlinenum,airlinenum) == 0) { if (airlinep->ticks == 0) { printf("-------------此航班无余票!\n"); sign = 0; } else { int n; printf("-------------请输入要预订的机票数量:"); while (scanf("%d",&n)) { if (airlinep->ticks < n) { printf("-------------机票数量不够!请重新输入:"); } else if (airlinep->ticks >= n) { userp->ticks += n; airlinep->ticks -= n; break; } } userp->bookcnt ++; strcpy(userp->bookticks[userp->bookcnt - 1],airlinenum);// userp->usertime = airlinep->time; writefile(0); writefile(2); exist = 1; break; } } airlinep = airlinep->next; } if (exist == 0 && sign == 1) { printf("-------------不存在此航班!\n"); } else if (exist == 1) { printf("-------------航班%s订票成功!\n",airlinep->airlinenum); } printf("-------------页面即将跳转..."); Sleep(2000);}void returnticks(user *userp){ airline *airlinep = ahead; char airlinenum[2000]; int exist = 0; int flag = 0; int cnt = userp->bookcnt; if (cnt == 0) { printf("\n\n\n\n\n\n\n"); printf("\t\t\t\t\t\t您还未预订任何航班!"); getchar(); return ; } printf("\n\n\n\n\n\n"); for (int i = 0; i < cnt; i ++) { printf("-------------第%d次预订的航班号:",i + 1); printf("%s\n",userp->bookticks[i]); } printf("-------------请输入要退还机票的航班号(按0退出):"); getchar(); while (gets(airlinenum)) { if (strcmp(airlinenum,"0") == 0) { return ; } for (int i = 0; i < userp->bookcnt; i ++) { if (strcmp(airlinenum,userp->bookticks[i]) == 0) { flag = i; exist = 1; break; } } if (!exist) { printf("-------------为查询到该航班号!\n"); printf("-------------请重新输入航班号(按0退出):"); } else { break; } } int n; while (airlinep) { if (strcmp(airlinep->airlinenum,airlinenum) == 0) { printf("-------------请输入要退订票数量:"); while (scanf("%d",&n)) { if (userp->ticks < n) { printf("-------------您未持有此数量票量!\n"); printf("-------------请重新输入:"); } else if (userp->ticks >= n) { userp->ticks -= n; airlinep->ticks += n; break; } } } airlinep = airlinep->next; } if (userp->ticks == 0) { for (int i = flag; i < userp->bookcnt - 1; i ++) { strcpy(userp->bookticks[i],userp->bookticks[i + 1]); } userp->bookcnt --; } writefile(0); writefile(2); printf("-------------%s号航班退票成功!\n",userp->bookticks[flag]); printf("-------------即将返回上一页面..."); Sleep(2000);}void findairline(user *userp){ int cnt = userp->bookcnt; long long sign; if (cnt == 0) { printf("\n\n\n\n\n\n\n"); printf("\t\t\t\t\t\t您还未预订任何航班!\n"); } printf("\n\n\n\n\n\n"); for (int i = 0; i < cnt; i ++) { printf("\t\t\t\t\t\t第%d次预订的航班号:%s\t第%d预定航班的票数:%d\n",i + 1, userp->bookticks[i], i+1, userp->ticks); } printf("\n\n请输入0退出:"); while (scanf("%lld",&sign)) { if (sign == 0) { break; } else { printf("-------------无效的输入!请重新输入!"); } }}void search(){ int sign; while (1) { cls(); date(); searchmenu(); char choose = getch(); switch(choose) { case '1': cls(); print_message(); break; case '2': cls(); printairline(); break; case '3': cls(); printtime(); break; case '4': cls(); printticks(); break; case '5': cls(); sign = 1; break; case '6': cls(); over(); cls(); default : break; } if (sign) { break; } }}void airlinesort(){ airline *f, *x, *y, *p; f = NULL; if (ahead1->next == NULL || ahead1->next->next == NULL) { return ; } while (f != ahead1->next->next) { for (p = ahead1; p->next->next != f; p = p->next) { if (strcmp(p->next->airlinenum,p->next->next->airlinenum) > 0) { x = p->next; y = p->next->next; p->next = y; x->next = y->next; y->next = x; } } f = p->next; } return ;}void timesort(){ airline *f, *x, *y, *p; f = NULL; if (ahead1->next == NULL || ahead1->next->next == NULL) { return ; } while (f != ahead1->next->next) { for (p = ahead1; p->next->next != f; p = p->next) { if (p->next->time > p->next->next->time) { x = p->next; y = p->next->next; p->next = y; x->next = y->next; y->next = x; } } f = p->next; } return ;}void tickssort(){ airline *f, *x, *y, *p; f = NULL; if (ahead1->next == NULL || ahead1->next->next == NULL) { return ; } while (f != ahead1->next->next) { for (p = ahead1; p->next->next != f; p = p->next) { if (p->next->ticks > p->next->next->ticks) { x = p->next; y = p->next->next; p->next = y; x->next = y->next; y->next = x; } } f = p->next; } return ;}void printairline(){ date(); airlinesort(); airline *airlinep = ahead1->next; printf("\n\n\n\n\n\n"); FILE *fp; fp = fopen("airline","r"); char ch; ch = fgetc(fp); rewind(fp); if (ch == EOF) { printf("-------------暂无航班信息!!"); getchar(); return ; } printf("航班号\t\t飞机号\t\t出发地点\t\t目的地\t\t出发时间\t\t票数\n"); while (airlinep) { printf(PRINTF); airlinep = airlinep->next; } system("pause");}void printticks(){ date(); tickssort(); airline *airlinep = ahead1->next; printf("\n\n\n\n\n\n"); FILE *fp; fp = fopen("airline","r"); char ch; ch = fgetc(fp); rewind(fp); if (ch == EOF) { printf("-------------暂无航班信息!!"); getchar(); return ; } printf("航班号\t\t飞机号\t\t出发地点\t\t目的地\t\t出发时间\t\t票数\n"); while (airlinep) {// printf("666\n"); printf(PRINTF); airlinep = airlinep->next; } system("pause");}void printtime(){ date(); timesort(); airline *airlinep = ahead1->next; printf("\n\n\n\n\n\n"); FILE *fp; fp = fopen("airline","r"); char ch; ch = fgetc(fp); rewind(fp); if (ch == EOF) { printf("-------------暂无航班信息!!"); getchar(); return ; } printf("航班号\t\t飞机号\t\t出发地点\t\t目的地\t\t出发时间\t\t票数\n"); while (airlinep) { printf(PRINTF); airlinep = airlinep->next; } system("pause");}void searchscert(){ printf("\n\n\n\n\n\n"); printf("\t\t\t\t\t********************************\n"); printf("\t\t\t\t\t** **\n"); printf("\t\t\t\t\t** 1.管理员找回密码 **\n"); printf("\t\t\t\t\t** **\n"); printf("\t\t\t\t\t** 2.乘客找回密码 **\n"); printf("\t\t\t\t\t** **\n"); printf("\t\t\t\t\t** 3.返回 **\n"); printf("\t\t\t\t\t** **\n"); printf("\t\t\t\t\t********************************\n"); printf("请按键选择:"); int sign = 0; char choose = getch(); while (1) { cls(); switch (choose) { case '1': cls(); searchmangerscert(); sign = 1; break; case '2': cls(); searchuserscert(); sign = 1; break; case '3': sign = 1; break; default : printf("输入无效!"); break; } if (sign) { break; } }}void searchmangerscert(){ date(); manger *p = mhead; char mangername[2000]; int flag = 0; int sign = 0; printf("\n\n\n\n\n\n\n"); FILE *fp; fp = fopen("manger","r"); char ch; ch = fgetc(fp); rewind(fp); if (ch == EOF) { printf("-------------暂无管理员账号!!\n-------------请先前往注册!"); getchar(); return ; } printf("-------------请输入您的账号(按0退出):");// getchar(); while (gets(mangername)) { if (strcmp(mangername,"0") == 0) { return ; } while (p) { if (strcmp(p->mangername,mangername) == 0) { flag = 1; break; } p = p->next; } if (flag) { break; } else { printf("-------------此账号不存在!请重新输入(按0退出):"); p = mhead; } } if (flag) { p = mhead; char securityscert[2000]; printf("-------------请输入您设置的密保(按0退出):"); while (gets(securityscert)) { if (strcmp(securityscert,"0") == 0) { return ; } while (p) { if (strcmp(p->securityscert,securityscert) == 0) { sign = 1; break; } p = p->next; } if (sign) { printf("-------------密保输入正确!\n"); printf("-------------您的密码为%s\n",p->scert); getchar(); return ; } else { printf("-------------密保输入错误!请重新输入(按0退出):"); p = mhead; } } }}void searchuserscert(){ date(); user *p = uhead; char username[2000]; int flag = 0; int sign = 0; printf("\n\n\n\n\n\n\n"); FILE *fp; fp = fopen("user","r"); char ch; ch = fgetc(fp); rewind(fp); if (ch == EOF) { printf("-------------暂无乘客账号!!\n-------------请先前往注册!"); getchar(); return ; } printf("-------------请输入您的账号(按0退出):");// getchar(); while (gets(username)) { if (strcmp(username,"0") == 0) { return ; } while (p) { if (strcmp(p->username,username) == 0) { flag = 1; break; } p = p->next; } if (flag) { break; } else { printf("-------------此账号不存在!请重新输入(按0退出):"); p = uhead; } } if (flag) { p = uhead; char securityscert[2000]; printf("-------------请输入您设置的密保(按0退出):"); while (gets(securityscert)) { if (strcmp(securityscert,"0") == 0) { return ; } while (p) { if (strcmp(p->securityscert,securityscert) == 0) { sign = 1; break; } p = p->next; } if (sign) { printf("-------------密保输入正确!\n"); printf("-------------您的密码为%s\n",p->scert); getchar(); return ; } else { printf("-------------密保输入错误!请重新输入(按0退出):"); p = uhead; } } }}void changescert(){ printf("\n\n\n\n\n\n"); printf("\t\t\t\t\t********************************\n"); printf("\t\t\t\t\t** **\n"); printf("\t\t\t\t\t** 1.管理员修改密码 **\n"); printf("\t\t\t\t\t** **\n"); printf("\t\t\t\t\t** 2.乘客修改密码 **\n"); printf("\t\t\t\t\t** **\n"); printf("\t\t\t\t\t** 3.返回 **\n"); printf("\t\t\t\t\t** **\n"); printf("\t\t\t\t\t********************************\n"); printf("请按键选择:"); char choose = getch(); int sign = 0; while (1) { cls(); switch (choose) { case '1': cls(); changemangerscert(); sign = 1; break; case '2': cls(); changeuserscert(); sign = 1; break; case '3': sign = 1; break; default : printf("输入无效!"); break; } if (sign) { break; } }}void changemangerscert(){ date(); manger *p = mhead; char mangername[2000]; char scert[2000]; int flag = 0; int sign = 0; char securityscert[2000]; FILE *fp; fp = fopen("manger","r"); char ch; ch = fgetc(fp); rewind(fp); if (ch == EOF) { printf("-------------暂无管理员账号信息!!\n-------------请先前往注册!"); getchar(); return ; } printf("\n\n\n\n\n\n\n"); printf("-------------请输入您的账号(按0退出):"); while (gets(mangername)) { if (strcmp(mangername,"0") == 0) { return ; } while (p) { if (strcmp(p->mangername,mangername) == 0) { flag = 1; break; } p = p->next; } if (flag) { break; } else { printf("-------------此账号不存在!请重新输入:"); p = mhead; } } if (flag) { p = mhead; printf("-------------请输入您设置的密保(按0退出):"); while (gets(securityscert)) { if (strcmp(securityscert,"0") == 0) { return ; } while (p) { if (strcmp(p->securityscert,securityscert) == 0) { sign = 1; break; } p = p->next; } if (sign) { printf("-------------请输入修改后的密码(按0退出):"); while (gets(scert)) { if (strcmp(scert,"0") == 0) { return ; } if (strlen(scert) <= 16) { break; } else { printf("-------------请输入1到16位的密码:"); } } strcpy(p->scert,scert); printf("-------------密码修改成功!"); writefile(1); getchar(); return ; } else { printf("-------------密保输入错误!请重新输入:"); p = mhead; } } }}void changeuserscert(){ date(); user *p = uhead; char username[2000]; char scert[2000]; char securityscert[2000]; int flag = 0; int sign = 0; FILE *fp; fp = fopen("user","r"); char ch; ch = fgetc(fp); rewind(fp); if (ch == EOF) { printf("-------------暂无管理员账号信息!!\n-------------请先前往注册!"); getchar(); return ; } printf("\n\n\n\n\n\n\n"); printf("-------------请输入您的账号(按0退出):"); while (gets(username)) { if (strcmp(username,"0") == 0) { return ; } while (p) { if (strcmp(p->username,username) == 0) { flag = 1; break; } p = p->next; } if (flag) { break; } else { printf("-------------此账号不存在!请重新输入:"); p = uhead; } } if (flag) { p = uhead; printf("-------------请输入您设置的密保(按0退出):"); while (gets(securityscert)) { if (strcmp(securityscert,"0") == 0) { return ; } while (p) { if (strcmp(p->securityscert,securityscert) == 0) { sign = 1; break; } p = p->next; } if (sign) { printf("-------------请输入修改后的密码(按0退出):"); while (gets(scert)) { if (strcmp(scert,"0") == 0) { return ; } if (strlen(scert) <= 16) { break; } else { printf("-------------请输入1到16位的密码:"); } } strcpy(p->scert,scert); printf("-------------密码修改成功!"); writefile(0); getchar(); return ; } else { printf("-------------密保输入错误!请重新输入:"); p = uhead; } } }}void print_message(){ date(); airline *airlinep = ahead1->next; printf("\n\n\n\n\n\n"); FILE *fp; fp = fopen("airline","r"); char ch; ch = fgetc(fp); rewind(fp); if (ch == EOF) { printf("-------------暂无航班信息!!"); getchar(); return ; } printf("航班号\t\t飞机号\t\t出发地点\t\t目的地\t\t出发时间\t\t票数\n"); while (airlinep) {// printf("666\n"); printf(PRINTF); airlinep = airlinep->next; } system("pause");}void hualihushao1(){ printf("\n\n\n\n\n\n\n\n\n\n\n\n"); printf("\t\t\t\t\t\t即将进入订票系统..."); system("color 40"); Sleep(250); system("color 04"); Sleep(250); system("color 60"); Sleep(250); system("color 24"); Sleep(250); system("color AF"); Sleep(250); system("color 92"); Sleep(250); system("color 57"); Sleep(250); system("color F4"); Sleep(250); system("color F4"); Sleep(250);}void hualihushao2(){ printf("\n\n\n\n\n\n\n\n\n\n\n\n"); printf("\t\t\t\t\t\t订票系统即将关闭..."); system("color 40"); Sleep(250); system("color 04"); Sleep(250); system("color 60"); Sleep(250); system("color 24"); Sleep(250); system("color AF"); Sleep(250); system("color 92"); Sleep(250); system("color 57"); Sleep(250); system("color F4"); Sleep(250); system("color F4"); Sleep(250);}void load(){ system("color 8b"); //F1 cout << "\n\n\n\n\n\n\n\n\n\n\n\n"; cout << "\t\t\t\t\t正在进入系统..请稍等..."; for (int i = 0; i <= 100; i += 2) { cout << "\r\t\t\t\t\t\t\t\t" << i << "%"; Sleep(50); if (i == 48) { Sleep(1000); } if (i == 76) { Sleep(800); } if (i == 100) { Sleep(500); } }
“C语言如何实现航空订票系统”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!