飞机航班管理系统 愿景&源代码
实训设计课程:飞机航班管理系统
任务:
用C程序设计语言设计一个飞机航班管理系统,能够完成一定的实用功能,比如进行航班信息的输入、查询等等。做到学以致用。
功能介绍:
一个功能菜单显示所有可选功能;
通过一个主函数,调用函数,其他通过分级调用函数完成,调用功能模块:
A.航班信息管理(调用以下函数,进行信息管理):
- 航班信息输入
- 删除航班信息
- 显示当前所有航班
- 保存所有信息
B.航班信息查询(调用以下函数实现):
- 按航班号进行查询
- 按终点站进行查询
- 按同时输入的时间终点进行查询
C.对航班信息进行排序(调用以下函数实现):
- 按飞行时间进行排序
- 按航班号进行排序
D.进行订票(查询航班完毕后,输入航班号进行订购机票);
E.进行退票(输入航班号进行退票);
F进行统计相关信息(在输入的时间段内,统计所有已售机票,营业总金额);
0.退出系统。
主要技术:
- 利用数组
- 调用函数
- 构建结构体
- 定义变量
- 利用顺序结构、选择结构、循环结构
- 使用指针
- 运用预处理命令
- 文件管理实现方法:
通过定义结构体数组,把各航班信息录入进去,然后就可以根据航班号(或终点站、飞行时间)来对结构体变量进行管理(比如录入、删除、订票、退票),通过循环控制,找到相关的信息,确认后就可以通过对地址的操作,达到对结构体数组的管理,然后输出显示。
通过函数的嵌套调用、递归调用,达到要求的功能,让源代码的直观性更加强大,安全性,可修改性更加高。有利于源代码的阅读、修改、优化。
合理地利用全局变量,让程序的实用性、可靠性更高,使用函数原型的方式对函数进行声明,把函数原型放在一块,更好管理函数模块。
额外加入了“密码管理“,通过密码管理,可以增强航班管理系统的安全性能,也可以修改密码,修改的密码以文件的信息录入磁盘,所以修改的密码在下次使用依然奏效。当管理员输入密码后,可以得到旅客所不具有的权限,比如能录入航班信息,删除航班等等。
用了文件管理的方法,对录入的信息进行保存,然后就算停止运行释放了内存,在下次登录的时候,进行读操作,把上次录入保存到磁盘的信息重新录入到内存中,直接利用存取;达到文件管理的效果,让航班管理系统的功能更加强大,让系统的实用性更加高。
参考书:
《 C程序设计(第二版) 》
#include<stdio.h>
#include<string.h>
#define NUM 16
struct Date {
int year;
int month;
int day;
}
;
struct System {
int number;
char start[10];
char end[10];
struct Date time;
int alltickets;
int booktickets;
int remaintickets;
int price;
}
flight[NUM],TEMP;
void load_password_data();
void loadFlightData();
void test_if_load();
char menu();
int password_test();
void manageflight();
void changepassword();
void flightinput();
void flightdelete();
void flightoutput();
void savemanage();
void flightsearch();
void flightno();
void flightto();
void flightunion();
void sortinformation();
void sorttime();
void sortno();
void flightbook();
void flightdishonnor();
void flightstatic();
int nb=0;
int i,j;
int l;
char k;
int TESTifsavefile=0;
int TESTifmanagerLoad=0;
int TESTifloadfile=0;
int TESTif_main_logout=0;
char setpassword[20]= {
"123"
}
;
int main() {
char chose;
int res;
load_password_data();
test_if_load();
if(TESTif_main_logout == 1) {
return 0;
}
while(1) {
printf("\n\n");
chose=menu();
switch(chose) {
case 'A': {
if(TESTifmanagerLoad==0) {
res=password_test();
if(TESTifmanagerLoad==1) {
manageflight();
} else if(res==2) {
break;
} else {
password_test();
}
} else if(TESTifmanagerLoad==1) {
manageflight();
}
break;
}
case 'B':
flightsearch();
break;
case 'C':
sortinformation();
break;
case 'D':
flightbook();
break;
case 'E':
flightdishonnor();
break;
case 'F':
flightstatic();
break;
case '0': {
printf("Are you sure that you want to logout really ?(0/1) \n");
scanf("%d",&l);
if(l==1 && TESTifsavefile == 0) {
printf("88888888888888888888888888888888888888888888888\n");
return 0;
} else if(TESTifsavefile == 1) {
printf("save_it\n");
manageflight();
} else {
printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
break;
}
}
default:
printf("\n <---------------input error !--------------->\n\n");
printf("\n-> press any key to comtinue........");
break;
}
getchar();
}
printf("\n");
printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>> * <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
printf(" *****.****.*****.****<<< GOOD BYE >>>****.*****.*****.***** \n");
printf("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
printf("\n");
return 0;
}
void load_password_data() {
FILE *lodpswd;
lodpswd=fopen("set_passwd_data","rb");
if(lodpswd==NULL) {
return;
}
if(fread(&setpassword,20,1,lodpswd)!=1) {
printf("cache error!\n");
}
fclose(lodpswd);
return;
}
void loadFlightData() {
int flen;
char filename[50];
FILE *load;
printf("Which FlightData do you want to Load?\n");
printf("(file name): ");
scanf("%s",filename);
getchar();
load=fopen(filename,"rb");
if(load==NULL) {
printf("\n-------can't find this file ! -------\n");
TESTifloadfile=0;
return;
} else
TESTifloadfile=1;
fseek(load,0,2);
flen=ftell(load);
nb=(flen/sizeof(struct System));
fseek(load,0,0);
for (i=0;i<nb;i++) {
if(fread(&flight[i],sizeof(struct System),1,load)!=1) {
printf("can't read this file ! \n");
return;
}
}
fclose(load);
printf(" ****************** Load file successfull! *****************\n\n");
return;
}
void test_if_load() {
loadFlightData();
for (;TESTifloadfile==0;) {
printf("\n >>>Is the input_name error?\n");
printf(" -------retry to input? (y/n)");
scanf("%c",&k);
getchar();
if( k == 'y' ) {
loadFlightData();
return;
} else if(k == 'n') {
printf("\n<<<<<<<<<<<< No file have you Load >>>>>>>>>>>>>\n");
printf("well. creat it...\n\n\n");
TESTifloadfile=1;
} else {
TESTif_main_logout=1;
printf("-------bad choice !!! \n-------- bye_bye -----\n");
return;
}
}
return;
}
char menu() {
char choice;
printf("\012\012\012");
printf("<<<<<<<<<<<<<<<<<<<<<<^^^^^^^^^^^^^^^^^^^^^^^^^>>>>>>>>>>>>>>>>>>>>\n");
printf("*'*'*'*'*'*'*'*'*'*.. Aircraft Booking System ..*'*'*'*'*'*'*'*'*'*\n");
printf(">>>>>>>>>>>>>>>>>____________ Welcome ____________<<<<<<<<<<<<<<<<<\n");
printf("** A. manage<<<<>>>> B.search**\n");
printf("** C. sort <<<>>> D.bookTicket **\n");
printf("**E. dishonnor<<>> F.staticticket**\n");
printf("** <>**\n");
printf("** 0.Exit the Aircraft Booking System! **\n");
printf("*******************************************************************\n");
printf(" -----What do you want to do :");
scanf("%c",&choice);
getchar();
return choice;
}
int password_test() {
char password[20];
if(TESTifmanagerLoad!=1) {
printf("\n\n###This point just for manager use !\n");
printf(" To Load,you need a password(Press '0' to Esc)!\n");
}
while(TESTifmanagerLoad==0) {
printf("-------please input the Password:");
scanf("%s",password);
if(strcmp(password,setpassword)==0) {
printf("\n -------------Load successfull !--------------\n\n");
TESTifmanagerLoad=1;
} else if(strcmp(password,"0")==0) {
return 2;
} else {
printf("Sorry,Password error !\n********************\n\n");
printf("please input password again(Or '0' to logout)!\n");
}
}
return 0;
}
/*----------Guan-Li ------------------------------- */
void manageflight() {
int choice;
while(1) {
printf("\n");
printf(" ************** manage the flight system! ****************\n");
printf(" 1.Add a flight\n");
printf(" 2.Delete flight\n");
printf(" 3.Print all theflight\n");
printf(" 4.Savemanage(save all the change)\n");
printf(" 5.Change the manager password !\n");
printf(">0.Escape\n");
printf(" *DO what(1/2/3/4/0):");
scanf("%d",&choice);
getchar();
switch(choice) {
case 1:
flightinput();
break;
case 2:
flightdelete();
break;
case 3:
flightoutput();
break;
case 4:
savemanage();
break;
case 5:
changepassword();
break;
case 0:
break;
default:
printf("\n choice error!\n>please input again...\n");
break;
}
if(choice==0 && TESTifsavefile != 0) //to test wether save the change... {
printf("\n!!!Warning:\nYou have change the Data !\n");
printf("Are you sure exit without save:\n");
printf("0=don't exit\n");
printf("1=exit and save\n");
printf("2=exit without save\n");
printf("your choice(0/1/2):");
scanf("%d",&l);
if(l==1) {
savemanage();
return;
} else if(l==2) {
return;
} else
printf("\012");
} else if(choice==0)
break;
if(choice>=0 && choice<=5) {
printf("go on...\n");
} else {
break;
}
}
return;
}
void changepassword() {
FILE *chgpswd;
if(TESTifmanagerLoad==1) {
printf("manager,please input a new password !\n");
printf("(least than =50= chars):");
scanf("%s",setpassword);
printf("\nset new password successfull !\n***\n**\n*\n\n");
}
chgpswd=fopen("set_passwd_data","wb");
if(chgpswd==NULL && setpassword== "\n") {
printf("you shuld set a new passwd\n");
scanf("%s",&setpassword);
return;
}
if(fwrite(setpassword,20,1,chgpswd)!=1) {
printf("cache error!\n");
}
fclose(chgpswd);
return;
}
void flightinput() {
int choice;
struct System *p;
p=&flight[nb];
printf("How many flight do you want to add(lessthan %d):",16-nb);
scanf("%d",&choice);
if(choice>16-nb) {
printf("\nInput error!(I have told you:lessthan %d)\n\n",16-nb);
return;
} else if(choice<=16-nb && choice>0) {
for (i=0;i<choice;i++) {
printf("***\nPlease input the flightinfo>>%d\n",nb+1);
printf("FlightNumber%d:",nb+1);
scanf("%d",&p[i].number);
printf("Start%d:",nb+1);
scanf("%s",p[i].start);
printf("End%d:",nb+1);
scanf("%s",p[i].end);
printf("StartTime%d:\n",nb+1); {
printf("Year:");
scanf("%d",&p[i].time.year);
printf("Month:");
scanf("%d",&p[i].time.month);
printf("Day:");
scanf("%d",&p[i].time.day);
}
printf("All The Tickets%d=",nb+1);
scanf("%d",&p[i].alltickets);
printf("Has Been Book Tickets%d=",nb+1);
scanf("%d",&p[i].booktickets);
printf("What is the %d flight price=",nb+1);
scanf("%d",&p[i].price);
p[i].remaintickets=p[i].alltickets-p[i].booktickets;
printf(" ----------- input a flight successfull ! -----------\n");
nb++;
}
} else
return;
printf(" --------------- input successfull ! --------------------- \n");
printf("---------------<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>------------------\n");
TESTifsavefile++;
return;
}
void flightdelete() {
int choice;
printf("\n Which flight do you want to delete(flight number):");
scanf("%d",&choice);
for (i=0;i<nb-1;i++) {
if(flight[i].number==choice) {
for (;i<nb;i++) {
memcpy(&flight[i],&flight[i+1],sizeof(struct System));
}
nb--;
}
}
printf("\n---------------Delete successfull !-----------------\n");
printf("You have delete a filght!\n\n");
TESTifsavefile++;
return;
}
void flightoutput() {
if(nb==0) {
printf("\n\nNo Data in the hard disk !!***\n**\n*\n");
return;
}
printf("\n");
printf("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
printf(" <<<<<<<<<<<<<<<< The Remainfiles Informations >>>>>>>>>>>>>>>\n");
printf(" NO. Start End YYYYMMDD AllTK BookTK remainTK Price");
printf("\n");
for (i=0;i<nb;i++) {
printf("%6d ",flight[i].number);
printf("%-10s",flight[i].start);
printf("%-10s",flight[i].end);
printf("%4d",flight[i].time.year);
printf("%2d",flight[i].time.month);
printf("%2d",flight[i].time.day);
printf(" %3d ",flight[i].alltickets);
printf("%3d",flight[i].booktickets);
printf("%3d ",(flight[i].alltickets-flight[i].booktickets));
printf("%5d",flight[i].price);
printf("\n");
}
printf(" -----------there are <<<<<<<%d>>>>>>> flight!-------------\n",nb);
printf("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
return;
}
void savemanage() {
FILE *fp;
char filename[20];
printf("What's the filename do you want to named(SAVE TO HD DATA)?\n");
printf("Tips: <Input '0000' to name 'FlightData'>\n");
printf(" <press '0' to Esc without change..>.\n\n");
printf(">>>>>please input the file name: ");
scanf("%s",filename);
if(strcmp(filename,"0000")==0) {
fp=fopen("FlightData","wb");
} else if(strcmp(filename,"0")==0) {
return;
} else {
fp=fopen(filename,"wb");
}
if(fp==NULL) {
printf("can't open this file!\n");
return;
}
for (i=0;i<nb;i++) {
if(fwrite(&flight[i],sizeof(struct System),1,fp)!=1) {
printf("can't write the file!\n");
return;
}
}
fclose(fp);
printf("\012");
printf("````````````````````congratulations!```````````````````` \n");
printf("`` Save all the information had input successfull!`` \n");
printf("```````````````````<.><.><.><.><.><.>``````````````````` \n");
printf("\012");
TESTifsavefile=0;
return;
}
/*---------- Cha-Xun------------------------------*/
void flightsearch() {
int choice;
while(1) {
printf("1.flightno\n");
printf("2.flightto\n");
printf("3.flightunion\n");
printf("0.Escape\n");
printf(" *DO(1/2/3/0):");
scanf("%d",&choice);
switch(choice) {
case 1:
flightno();
break;
case 2:
flightto();
break;
case 3:
flightunion();
break;
case 0:
return;
default:
printf("input error! input again...\n");
}
}
return;
}
void flightno() {
int choice;
printf("please input the flight.number=");
scanf("%d",&choice);
for (i=0;i<nb;i++) {
if(choice==flight[i].number) {
printf("\n:)\nSystem have found the flight!\n\n\n");
printf(" NO. Start End Time AllTK BookTK remainTK Price\n");
printf("%6d ",flight[i].number);
printf("%-10s",flight[i].start);
printf("%-10s",flight[i].end);
printf("%4d",flight[i].time.year);
printf("%2d",flight[i].time.month);
printf("%2d",flight[i].time.day);
printf(" %3d ",flight[i].alltickets);
printf("%3d",flight[i].booktickets);
printf("%3d ",flight[i].remaintickets);
printf("%5d",flight[i].price);
printf("\n");
l=301;
}
}
if(l !=301) {
printf("\n:(\nSorry,can't find this flight!\n\n");
}
printf("\n\n");
return;
}
void flightto() {
char choice[10];
printf("\nWhere do you want to fly to(place):");
scanf("%s",choice);
for (i=0;i<nb;i++) {
if(strcmp(flight[i].end,choice)==0) {
printf("\n:)\nSystem have found the flight!\n");
printf(" NO. Start End YYYYMMDD AllTK BookTK remainTK Price\n");
printf("%6d ",flight[i].number);
printf("%-10s",flight[i].start);
printf("%-10s",flight[i].end);
printf("%4d",flight[i].time.year);
printf("%2d",flight[i].time.month);
printf("%2d",flight[i].time.day);
printf(" %3d ",flight[i].alltickets);
printf("%3d",flight[i].booktickets);
printf("%3d ",flight[i].remaintickets);
printf("%5d",flight[i].price);
printf("\n");
l=501;
}
}
if(l !=501) {
printf("\n:(\nSorry,can't find the flight!\n\n");
}
return;
}
void flightunion() {
int y,m,d;
char choice[10];
printf("Use the time and end_place to union search the flight number!\n");
printf("When will you By air(YYYY/MM/DD):");
scanf("%d/%d/%d",&y,&m,&d);
printf("Where will you go(end_place):");
scanf("%s",choice);
for (i=0;i<nb;i++) {
if(strcmp(flight[i].end,choice)==0 && flight[i].time.year==y && flight[i].time.month==m && flight[i].time.day==d) {
printf("\n:)\nSystem have found the flight!\n");
printf(" NO. Start End YYYYMMDD AllTK BookTK remainTK Price\n");
printf("%6d ",flight[i].number);
printf("%-10s",flight[i].start);
printf("%-10s",flight[i].end);
printf("%4d",flight[i].time.year);
printf("%2d",flight[i].time.month);
printf("%2d",flight[i].time.day);
printf(" %3d ",flight[i].alltickets);
printf("%3d",flight[i].booktickets);
printf("%3d ",flight[i].remaintickets);
printf("%5d",flight[i].price);
printf("\n");
l=701;
}
}
if(l!=701) {
printf("\n:(\nSorry,can't find the flight!\n\n");
}
printf("\n\n");
return;
}
/*----------Pai-Xu ----------------------------------- */
void sortinformation() {
int ch;
printf("1.sort by time\n");
printf("2.sort by number\n");
printf(" *DO(1/2):");
scanf("%d",&ch);
switch(ch) {
case 1:
sorttime();
break;
case 2:
sortno();
break;
default:
printf("3 error!\n");
}
return;
}
void sorttime() {
for (j=0;j<nb-1;j++) {
for (i=0;i<nb-j-1;i++) {
if(flight[i].time.year>flight[i+1].time.year) {
TEMP=flight[i];
flight[i]=flight[i+1];
flight[i+1]=TEMP;
} else if(flight[i].time.year==flight[i+1].time.year && flight[i].time.month>flight[i+1].time.month) {
TEMP=flight[i];
flight[i]=flight[i+1];
flight[i+1]=TEMP;
} else if(flight[i].time.month==flight[i+1].time.month && flight[i].time.day>flight[i+1].time.day) {
TEMP=flight[i];
flight[i]=flight[i+1];
flight[i+1]=TEMP;
}
}
}
//savemanage();
return;
}
void sortno() {
for (j=0;j<nb-1;j++) {
for (i=0;i<nb-j-1;i++) {
if(flight[i].number>flight[i+1].number) {
TEMP=flight[i];
flight[i]=flight[i+1];
flight[i+1]=TEMP;
}
}
}
//savemanage();
return;
}
/*------- Ding-PiaoTui-PiaoTong-Ji ---------------- */
void flightbook() {
int choice1,choice2;
sortno();
printf("\n<<<<<<<<<<<<<<< The All flight Informations >>>>>>>>>>>>>>>>\n");
printf(" NO. Start End Time AllTK BookTK remainTK Price \n");
for (i=0;i<nb;i++) {
printf("%6d %-10s%-10s%4d%2d%2d %3d %3d%3d %5d\n",
flight[i].number,flight[i].start,flight[i].end,flight[i].time.year,
flight[i].time.month,flight[i].time.day,flight[i].alltickets,
flight[i].booktickets,flight[i].alltickets-flight[i].booktickets,
flight[i].price);
}
printf(" -------there are <<<<<<<%d>>>>>>> flight!------------------\n",nb);
printf("<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
printf("Which flight do you want to book(NO.):\n");
scanf("%d",&choice1);
printf("How many tickets will you book:\n");
scanf("%d",&choice2);
for (i=0;i<nb;i++) {
if(choice1==flight[i].number) {
flight[i].booktickets+=choice2;
}
}
printf("--------Book flight tickets successfull !!!\n\n\n\n");
savemanage();
return;
}
void flightdishonnor() {
int choice1,choice2;
printf("when did you book these flight tickets?\n");
printf("Which flight do you want to dishonnor(NO.):");
scanf("%d",&choice1);
printf("How many tickets will you dishonnor:");
scanf("%d",&choice2);
for (i=0;i<nb;i++) {
if(choice1==flight[i].number) {
flight[i].booktickets-=choice2;
}
}
printf("\n\n\n\ndishonnor flight tickets successfull !!!\n\n\n");
savemanage();
return;
}
void flightstatic() {
int y1,y2,m1,m2,d1,d2;
int soldtk=0;
int allmoney=0;
printf("please input the start_time/end_time for static informatiom\n");
printf("start_time(YYYY/MM/DD):");
scanf("%d/%d/%d",&y1,&m1,&d1);
printf("end_time(YYYY/MM/DD):");
scanf("%d/%d/%d",&y2,&m2,&d2);
for (i=0;i<nb;i++) {
if(flight[i].time.year>y1 && flight[i].time.year<y2) {
soldtk+=flight[i].booktickets;
allmoney+=flight[i].price*flight[i].booktickets;
} else if(flight[i].time.month>m1 && flight[i].time.month<m2) {
soldtk+=flight[i].booktickets;
allmoney+=flight[i].price*flight[i].booktickets;
} else if(flight[i].time.day>d1 && flight[i].time.day<d2) {
soldtk+=flight[i].booktickets;
allmoney+=flight[i].price*flight[i].booktickets;
}
}
printf("**********************************************************");
printf("\nhas been book ticket:%d\n",soldtk);
printf("all the money:%d\n\n",allmoney);
printf("**********************************************************");
printf("\n\nTo press anykey to continue...");
return;
}