当前位置:文档之家› 飞机订票系统实验报告

飞机订票系统实验报告

课程设计课程名称:数据结构实验项目:飞机订票系统姓名:专业:班级:学号:计算机科学与技术学院年月日实验项目名称:飞机订票系统一、实验目的:1. 了解并掌握数据结构与算法的设计方法,具备初步的独立分析和设计能力;2. 初步掌握软件开发过程的问题分析、系统设计、程序编码、测试等基本方法和技能;3. 提高综合运用所学的理论知识和方法独立分析和解决问题的能力;二、实验内容(1)本系统实现航班订票的功能。

要求具有录入、订票、退票等功能。

(2)航班信息存放在一个结构体中,分别有航班号、起飞时间、降落时间、票价等信息。

(3)程序完成航班信息的录入和乘客订票功能。

(4)客户能通过航班号或抵达城市查询航班信息,并且能订票。

当座位无剩余时提示客户并提示相关航线。

三.概要设计程序包含函数:主控模块包含子模块为:四.详细设计算法流程图a) 系统以菜单方式工作;b) 航班订票功能c) 航班退票功能d) 查询航线:(至少一种查询方式)--算法;e) 修改航线(1) 增加航班号(2) 删除航班号(3) 修改密码(4) 查询航班信息f) 按终点站查询。

程序截图菜单增加航线信息订票退票查询五.源代码#include <conio.h>#include<stdio.h>#include<string.h>#include<stdlib.h>#define OK 1#define TRUE 1#define FALSE 0#define ERROR 0#define OVERFLOW -2#define PR printftypedef int status;typedef struct airline{char line_num[8];//航班号char plane_num[8];//飞机号char end_place[20];//目的的int total;//座位总数int left;//剩余座位struct airline *next;//下一个结点}airline;typedef struct customer{char name[9];//顾客名char line_num[8];//航班号int seat_num;//座位号struct customer *next;//下一个结点}customer;airline *init_airline(){ //初始化链表airline *l;l=(airline*)malloc(sizeof(airline)); if(l==NULL){ exit(0);}l->next=NULL;return l;}customer * init_customer(){//初始化链表customer *l;l=(customer*)malloc(sizeof(customer)); if(l==NULL){exit(0);l->next=NULL;return l;}status insert_airline(airline **p,char *line_num,char *plane_num,char *end_place,int total,int left){//airline链表插入操作airline *q;q=(airline*)malloc(sizeof(airline));strcpy(q->line_num , line_num);strcpy(q->plane_num , plane_num);strcpy(q->end_place , end_place);q->total =total;q->left =left;q->next=NULL;(*p)->next=q;(*p)=(*p)->next;// PR("insert %d ,%dis succssed!\n",e,bl);return OK;}status insert_customer(customer **p,char *name,char *line_num,int seat){//customer链表插入操作customer *q;q=(customer*)malloc(sizeof(customer));/* { PR("内存分配失败\n");return OVERFLOW; }*/strcpy(q->name , name);strcpy(q->line_num , line_num);q->seat_num =seat;q->next=NULL;(*p)->next=q;(*p)=(*p)->next;// PR("insert %d ,%dis succssed!\n",e,bl);return OK;}airline *modefy_airline(airline *l,char *line_num)//修改airline链表中的数据{ airline *p;p=l->next ;for(;p!=NULL;p=p->next ){ if(strcmp(line_num,p->line_num )==0){ p->left ++;// PR("modefy %s\n",p->line_num );return l;}}PR("\n\t\t没有这个航班,无法完成修改任务!");status delete_airline(airline *h,char *line_num)//删除航班{ airline *p,*pr;pr=h;p=pr->next ;while(p!=NULL){ if(strcmp(line_num,p->line_num )==0){ pr->next =p->next ;PR("\n\t\t删除 %s 航班\n",p->line_num );return OK;}pr=pr->next ;p=pr->next ;}PR("\n\t\t无此航班,无法删除!\n");return ERROR;}status delete_customer(customer *h,char *line_num)//删除顾客{ customer *p,*pr;pr=h;p=pr->next ;while(p!=NULL){ if(strcmp(line_num,p->line_num )==0){ pr->next =p->next ;}pr=pr->next ;p=pr->next ;}// PR("无此航班,无法删除!\n");return OK;}status delete_cus(customer *h,airline *l,char *name)//顾客退票{ customer *p,*pr;char line_num[8];// qr=h;pr=h;p=pr->next ;// PR("开始删除\n");while(p!=NULL){ if(strcmp(name,p->name )==0){ strcpy(line_num,p->line_num );l=modefy_airline(l,line_num);pr->next =p->next ;PR("\n\t\t顾客 %s 退票成功!",p->name );pr=pr->next ;p=pr->next ;}PR("\n\t\t无此顾客,无法退票!\n");return ERROR;}status save_airline(airline *l)//保存airline.dat{ FILE *fp_airline;char ch='#';airline *p=l->next ;char filename[]="c:\\airline.dat";if((fp_airline=fopen(filename,"wb"))==NULL){ printf("can not open file to write:%s\n",filename);return ERROR;}for(;p!=NULL;p=p->next ){ // printf("%s,%s,%s,%d,%d\n",p->line_num ,p->plane_num ,p->end_place ,p->total ,p ->left );fprintf(fp_airline,"%s,%s,%s,%d,%d%c\n",p->line_num ,p->plane_num ,p->end_plac e ,p->total ,p->left ,ch);}fclose(fp_airline);return OK;}status save_customer(customer *l)//保存顾客信息 customer.dat{ FILE *fp_customer;char ch='#';customer *p=l->next ;char filename[]="c:\\customer.dat";if((fp_customer=fopen(filename,"wb"))==NULL){ printf("can not open file to write:%s\n",filename);return ERROR;}for(;p!=NULL;p=p->next ){ // PR("%s,%s,%d\n",p->name ,p->line_num ,p->seat_num );fprintf(fp_customer,"%s,%s,%d%c",p->name ,p->line_num ,p->seat_num ,ch);}fclose(fp_customer);return OK;}int changStrInt(char *ch)//把字符串转化为整型{ int a=1,b=0,c=0,i;{ if (ch[i]<58&&ch[i]>47){ b=a*(ch[i]-48);a=a*10;c=c+b;}else{ PR("\n\t\t%c 不合法,无法将此字符串转化为整形!",ch[i]);return 0;}// printf("the c is %d\n",c);}return c;}status insert_air(airline *l,char *line_num,char *plane_num,char *end_place,int total,int left){//airline链表插入操作airline *q;q=(airline*)malloc(sizeof(airline));strcpy(q->line_num , line_num);strcpy(q->plane_num , plane_num);strcpy(q->end_place , end_place);q->total =total;q->left =left;q->next=l->next ;l->next=q;// PR("insert %d ,%dis succssed!\n",e,bl);return OK;}status insert_cus(customer *l,char *name,char *line_num,int seat){//customer链表插入操作customer *q;q=(customer*)malloc(sizeof(customer));strcpy(q->name , name);strcpy(q->line_num , line_num);q->seat_num =seat;q->next=l->next ;l->next=q;return OK;}status load_airline(airline *l){ FILE *fp_airline;int flag=0,i=0;char ch;char line_num[8];//航班号char end_place[20];//目的的char total_str[5];char left_str[5];int total;//座位总数int left;//剩余座位// airline *p=l;char filename[]="c:\\airline.dat";if((fp_airline=fopen(filename,"rb"))==NULL){ printf("can not open file to load:%s\n",filename); return ERROR;}while(!feof(fp_airline)){ ch=fgetc(fp_airline);if(ch!='#'){ if(flag==0&&ch!=','){ line_num[i]=ch;i++;}else if(flag==1&&ch!=','){ plane_num[i]=ch;i++;}else if(flag==2&&ch!=','){ end_place[i]=ch;i++;}else if(flag==3&&ch!=','){ total_str[i]=ch;i++;}else if(flag==4&&ch!=','){ left_str[i]=ch;i++;}else if (ch==','){ flag++;i=0;}/* else{ PR("错误\n");return ERROR;}*/}elsei=0;total=changStrInt(total_str);left=changStrInt(left_str);PR("%8s%8s%8s%9d%9d\n",line_num ,plane_num ,end_place ,total ,left ); insert_air(l,line_num,plane_num,end_place,total,left);}}fclose(fp_airline);return OK;}status load_customer(customer *l){ FILE *fp_customer;int flag=0,i=0;char ch;char name[9];char line_num[8];//航班号char seat_num_str[5];int seat_num;//座位// customer *p=*l;char filename[50]="c:\\customer.dat";if((fp_customer=fopen(filename,"rb"))==NULL){ printf("can not open file to load:%s\n",filename);return ERROR;}while(!feof(fp_customer)){ ch=fgetc(fp_customer);printf("%c\n",ch);if(ch!='#'){ if(flag==0&&ch!=','){ name[i]=ch;i++;}else if(flag==1&&ch!=','){ line_num[i]=ch;i++;}else if(flag==2&&ch!=','){ seat_num_str[i]=ch;i++;}else if (ch==','){ flag++;i=0;}{ PR("错误\n");return ERROR;}}else{ flag=0;seat_num=changStrInt(seat_num_str);PR("%10s %10s %d\n",name ,line_num ,seat_num );insert_cus(l,name,line_num,seat_num);// p=p->next ;}}fclose(fp_customer);return OK;}status creat_airline(airline **l)//创建airline单链表{ airline *p=*l;int i=0;char *line_num[3]={"bjnc01","bjsh02","shgz03"};char *plane_num[3]={"plane1","plane2","plane3"};char *end_place[3]={"nc","sh","gz"};int total[3]={100,100,100};int left[3]={51,50,78};for (i=0;i<3;i++){insert_airline(&p,line_num[i],plane_num[i],end_place[i],total[i],left[i]);}return OK;}status creat_customer(customer **l)////创建customer单链表{ customer *p=*l;int i=0;char *name[3]={"ouyangj0","yhl","fs"};char *line_num[3]={"bjnc01","bjsh02","shgz03"};int seat_num[3]={1,5,10};for (i=0;i<3;i++){insert_customer(&p,name[i],line_num[i],seat_num[i]);}return OK;}status increase_air(airline *l,char *line_num,char *plane_num,char *end_place,int total)//增加航线{ airline *p=l->next ;for(;p->next !=NULL;p=p->next){}insert_airline(&p,line_num,plane_num,end_place,total,total);return OK;}status book(airline *l,char *line_num,customer *c,char *name)//订票函数{ airline *p=l;customer *q=c->next ;p=l->next ;for(;q->next !=NULL;q=q->next){}// PR("%s\n",q->name );for(;p!=NULL;p=p->next ){ if(strcmp(line_num,p->line_num )==0){ if(p->left >0){ PR("\n\t\t恭喜您!订票成功!");PR("\n\t\t你的座位号是: %d",(p->total -p->left +1));insert_customer(&q,name,line_num,p->total -p->left +1);p->left --;return OK;}else PR("\n\t\t对不起,座位已满!");return 0;}}PR("\n\t\t对不起,没有这个航班号!");return ERROR;}status print_airline(airline *l)//打印航线信息{ airline *p=l->next ;for(;p!=NULL;p=p->next ){ PR("%8s%8s%8s%9d%9d\n",p->line_num ,p->plane_num ,p->end_place ,p->total ,p->left );}return OK;}status print_customer(customer *l)//打印顾客信息{customer *p=l->next ;for(;p!=NULL;p=p->next ){ PR("%10s %10s %d\n",p->name ,p->line_num ,p->seat_num );}return OK;}status inputpassword(char cc[9])//隐藏密码为*号的函数 -------------------楼主,注意这里!{char c;for(i=0;c=getch();i++){if(c==13) break; /*13是回车的ascii码,用'\n'不行,不知怎么回事*/else{cc[i]=c;printf("*");}}cc[i]='\0';//printf("\n");//printf("%s",cc);//getchar();return strlen(cc);}int main(){ char choice,choice2,name[9],line_num[8],password[9],plane_num[8],end_place[9];char pass[9]="12345678",re_pass_1[9],re_pass_2[9];int t=1,tt=1,total;airline *air=init_airline();customer *cus=init_customer();creat_airline(&air);creat_customer(&cus);// save_airline(air);// save_customer(cus);while(t==1){PR("\n\t\t********************************");PR("\n\t\t 欢迎进入航班订票系统 ");PR("\n\t\t* 1----订票 *");PR("\n\t\t* 2----退票 *");PR("\n\t\t* 3----查询 *");PR("\n\t\t* 4----修改航线 *");PR("\n\t\t* 5----读入文件 *");PR("\n\t\t* 0----返回 *");PR("\n\t\t********************************");PR("\n\t\t*请输入菜单号(0-5):");choice = getch();PR("%c\n",choice);if(choice=='1'){ PR("\n\t\t请输入你要订的航班号: ");scanf( "%s",line_num);PR("\n\t\t请输入你的姓名: ");book(air,line_num,cus,name);save_airline(air);save_customer(cus);}else if(choice=='2'){ PR("\n\t\t请输入你的姓名: ");scanf( "%s",name);delete_cus(cus,air,name);save_airline(air);save_customer(cus);}else if(choice=='3'){ PR("\n 航班号飞机号目的地总票数余票数\n"); print_airline(air);PR(" 姓名航班号座位号\n");print_customer(cus);}else if(choice=='4'){ tt=1;PR("请输入密码: ");//scanf("%s",password);inputpassword(password);if(strcmp(password,pass)==0){ while (tt==1){PR("\n\t\t********************************"); PR("\n\t\t* 航线信息修改: *"); PR("\n\t\t* 增加航班号-----'0' *");PR("\n\t\t* 删除航班号-----'1' *");PR("\n\t\t* 修改密码-------'2' *");PR("\n\t\t* 查询航线信息---'3' *");PR("\n\t\t* 退出航线修改---'4' *");PR("\n\t\t********************************"); PR("\n\t\t请选择: ");choice2=getch();PR("%c\n",choice2);if(choice2=='0'){ PR("\n\t\t请输入你要增加的航班号: ");scanf("%s",line_num);PR("\n\t\t请输入飞机号: ");scanf("%s",plane_num);PR("\n\t\t请输入目的地: ");scanf("%s",end_place);PR("\n\t\t请输入座位总数: ");increase_air(air,line_num,plane_num,end_place,total); save_airline(air);save_customer(cus);}else if (choice2=='1'){ PR("\n\t\t请输入你要删除的航班号: ");scanf("%s",line_num);delete_airline(air,line_num);delete_customer(cus,line_num);save_airline(air);save_customer(cus);}else if(choice2=='2'){ PR("\n\t\t注意:密码不能超过8位!");PR("\n\t\t请输入新密码:");scanf("%s",re_pass_1);PR("\n\t\t请再输入一次: ");scanf("%s",re_pass_2);if(strcmp(re_pass_1,re_pass_2)==0){ strcpy(pass,re_pass_1);PR("\n\t\t密码修改成功!请记住.");}else { PR("\n\t\t你两次输入的密码不一致!");}}else if(choice2=='3'){ PR("\n\t\t 航班号飞机号目的地总票数余票数\n"); print_airline(air);}else if(choice2=='4'){ tt=0;}else {PR("\n\t\t你的输入有误");tt=0;}}//end while}//end ifelse {PR("\n\t\t对不起!你输入的密码不正确!");}}//end else if 修改else if(choice=='4'){ load_airline(air);}else if(choice=='0'){ PR("\n\t\t再见!");t=0;}else{ PR("\n\t\t你的输入有误"); }}getch();}。

相关主题