飞机订票系统设计

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

假定民航机场共有n个航班,每壹航班有一行班号,确定地航线(起始站,终点站),确定的飞行时间(星期几)和一定的成员订额,试设计一民航订票系统,使之能提供一下服务:1:航班信息录入功能(航班信息用文件保存)2:航班信息浏览功能3:查询航线(至少一种查询方式)(1):按航班号查询(2):按起始站查询(3):按终点站查询(4):按飞行时间查询此外,要提供键盘式选择菜单以实现功能选择
c语言程序设计地飞机订票系统设计
//#include<stdafx.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef int status;
typedef struct airline
{
char flight_num[8]; /*航班号*/
char plane_num[8]; /*飞机号*/
char destination[20]; /*目的地*/
int total; /*座位总数*/
int left; /*剩余座位*/
struct airline *next; /*下一个结点*/
}airline;
typedef struct customer
{
char name[9]; /*顾客名*/
char flight_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 *flight_num,char *plane_num,char
*destination,int total,int left)
{ /*airline链表插入操作*/
airline *q;
q=(airline*)malloc(sizeof(airline)); /*初始化*/
strcpy(q->flight_num , flight_num); /*拷贝信息*/
strcpy(q->plane_num , plane_num);
strcpy(q->destination , destination);
q->total =total;
q->left =left;
q->next=NULL;
(*p)->next=q;
(*p)=(*p)->next;
return 1;
}
status insert_customer(customer **p,char *name,char *flight_num,int seat) { /*customer信息插入操作*/
customer *q;
q=(customer*)malloc(sizeof(customer));
strcpy(q->name , name); /*顾客信息拷贝*/
strcpy(q->flight_num , flight_num);
q->seat_num =seat;
q->next=NULL;
(*p)->next=q;
(*p)=(*p)->next;
return 1;
}
airline *modefy_airline(airline *l,char *flight_num) /*修改airline中的数据*/ { airline *p;
p=l->next ;
for(;p!=NULL;p=p->next )
{ if(strcmp(flight_num,p->flight_num )==0) /*查找*/
{ p->left ++;
return l;
}
}
printf("No this flight,can't make correction!\n"); /*查找失败*/
return 0;
status delete_airline(airline *h,char *flight_num) /*删除航班*/
{ airline *p,*pr;
pr=h;
p=pr->next ;
while(p!=NULL)
{ if(strcmp(flight_num,p->flight_num )==0) /*信息匹配*/
{ pr->next =p->next ;
printf("Delete %s flight\n",p->flight_num );
return 1;
}
pr=pr->next ;
p=pr->next ;
}
printf("No this flight,can't delete!\n"); /*无该信息*/
return 0;
}
status delete_customer(customer *h,char *flight_num) /*顾客信息删除*/ { customer *p,*pr;
pr=h;
p=pr->next ;
while(p!=NULL)
{ if(strcmp(flight_num,p->flight_num )==0) /*信息匹配*/
{ pr->next =p->next ; }
pr=pr->next ;
p=pr->next ;
}
return 1;
}
status delete_cus(customer *h,airline *l,char *name) /*顾客退票*/
{ customer *p,*pr;
char flight_num[8];
pr=h;
p=pr->next ;
while(p!=NULL)
{ if(strcmp(name,p->name )==0) /*找顾客姓名*/
{ strcpy(flight_num,p->flight_num ); /*找航班号*/
l=modefy_airline(l,flight_num); /*修改该航班信息*/
pr->next =p->next ;
printf("Customer %s return tickets successed!\n",p->name );
return 1;
}
pr=pr->next ;
p=pr->next ;
printf("No this customer,can't return!\n");
return 0;
}
status save_airline(airline *l) /*保存airline.dat*/
{ FILE *fp_airline;
char ch='#';
airline *p=l->next ;
char filename[]="c:\\airline.dat"; /*寻找C盘中的航班信息文件*/
if((fp_airline=fopen(filename,"wb"))==NULL)
{ printf("can not open file to write:%s\n",filename);
return 0;
}
for(;p!=NULL;p=p->next )
{ fprintf(fp_airline,"%s,%s,%s,%d,%d,%c",p->flight_num,p->plane_num,p->de stination,p->total,p->left,ch);
fflush(stdin);
}
fclose(fp_airline);
return 1;
}
status save_customer(customer *l) /*保存顾客信息customer.dat*/ { FILE *fp_customer;
char ch='#';
customer *p=l->next ;
char filename[]="c:\\customer.dat"; /*寻找C盘中的顾客信息文件*/ if((fp_customer=fopen(filename,"wb"))==NULL)
{ printf("can not open file to write:%s\n",filename);
return 0;
}
for(;p!=NULL;p=p->next )
{ fprintf(fp_customer,"%s,%s,%d%c",p->name ,p->flight_num ,p->seat_num ,c h);
}
fclose(fp_customer);
return 1;
}
int changStrInt(char *ch) //把字符串转化为整型
{ int a=1,b=0,c=0,i;
for (i=strlen(ch)-1;i>=0;i--)
{ if (ch[i]<='9'&&ch[i]>='0')
{ b=a*(ch[i]-'0');
a=a*10;
c=c+b;
else
{ printf("%c不合法,无法将此字符转化为整形!\n",ch[i]);
return 0;
}
}
return c;
}
status insert_air(airline *l,char *flight_num,char *plane_num,char *destination,int total,int left)
{ /*airline链表插入操作*/
airline *q;
q=(airline*)malloc(sizeof(airline));
strcpy(q->flight_num , flight_num);
strcpy(q->plane_num , plane_num);
strcpy(q->destination , destination);
q->total =total;
q->left =left;
q->next=l->next ;
l->next=q;
return 1;
}
status insert_cus(customer *l,char *name,char *flight_num,int seat) { /*customer链表插入操作*/
customer *q;
q=(customer*)malloc(sizeof(customer));
strcpy(q->name , name);
strcpy(q->flight_num , flight_num);
q->seat_num =seat;
q->next=l->next ;
l->next=q;
return 1;
}
status load_airline(airline *l) /*读入文件中航班信息*/
{ FILE *fp_airline;
int flag=0,i=0;
char ch;
char flight_num[8]="\0"; /*航班号*/
char plane_num[8]="\0"; /*飞机号*/
char destination[20]="\0"; /*目的地*/
char total_str[5]="\0";
char left_str[5]="\0";
int total; /*座位总数*/
int left; /*剩余座位*/
char filename[]="c:\\airline.dat";
if((fp_airline=fopen(filename,"r"))==NULL)
{ printf("can not open file to load:%s\n",filename);
return 0;
}
while(!feof(fp_airline))
{ ch=fgetc(fp_airline);
if(ch!='#')
{ if(flag==0&&ch!=',')
{ flight_num[i]=ch; i++; }
else if(flag==1&&ch!=',')
{ plane_num[i]=ch; i++; }
else if(flag==2&&ch!=',')
{ destination[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==',')
{
if(flag==0)
flight_num[i]='\0';
else if(flag==1)
plane_num[i]='\0';
else if(flag==2)
destination[i]='\0';
else if(flag==3)
total_str[i]='\0';
else
left_str[i]='\0';
flag++; i=0;
}
}
else
{ flag=0; i=0;
total=changStrInt(total_str);
left=changStrInt(left_str);
printf("%8s%8s%8s%9d%9d\n",flight_num ,plane_num ,destination ,total ,left );
fflush(stdin);
////insert_air(l,flight_num,plane_num,destination,total,left);
}
}
fclose(fp_airline);
}
status load_customer(customer *l) /*从文件读入顾客信息*/ { FILE *fp_customer;
int flag=0,i=0;
char ch;
char name[9]="\0";
char flight_num[8]="\0"; /*航班号*/
char seat_num_str[5]="\0";
int seat_num; /*座位*/
char filename[50]="c:\\customer.dat";
if((fp_customer=fopen(filename,"r"))==NULL)
{ printf("can not open file to load:%s\n",filename);
return 0;
}
while(!feof(fp_customer))
{ ch=fgetc(fp_customer);
if(ch!='#')
{ if(flag==0&&ch!=',')
{ name[i]=ch; i++; }
else if(flag==1&&ch!=',')
{ flight_num[i]=ch; i++; }
else if(flag==2&&ch!=',')
{ seat_num_str[i]=ch; i++; }
else if (ch==',')
{ if(flag==0)
name[i]='\0';
else if(flag==1)
flight_num[i]='\0';
else seat_num_str[i]='\0';
flag++; i=0;
}
else
{ printf("ERROR\n"); return 0; }
}
else
{ flag=0; i=0;
seat_num=changStrInt(seat_num_str);
printf("%15s %15s %10d\n",name ,flight_num ,seat_num ); fflush(stdin);
////insert_cus(l,name,flight_num,seat_num);
}
}
fclose(fp_customer);
}
status creat_airline(airline **l) /*创建airline单链表*/
{ airline *p=*l;
int i=0;
char *flight_num[3]={"bjnc01","bjsh02","shgz03"};
char *plane_num[3]={"plane1","plane2","plane3"};
char *destination[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,flight_num[i],plane_num[i],destination[i],total[i],left[i]);
}
return 1;
}
status creat_customer(customer **l) /*创建customer单链表*/ { customer *p=*l;
int i=0;
char *name[3]={"yuyang","lucy","fanhong"};
char *flight_num[3]={"bjnc01","bjsh02","shgz03"};
int seat_num[3]={19,15,10};
for (i=0;i<3;i++){
insert_customer(&p,name[i],flight_num[i],seat_num[i]);
}
return 1;
}
status increase_air(airline *l,char *flight_num,char *plane_num,char
*destination,int total) /*增加航线*/
{ airline *p=l->next ;
for(;p->next !=NULL;p=p->next){}
insert_airline(&p,flight_num,plane_num,destination,total,total);
printf("Adding flight %s successed!\n",flight_num);
return 1;
}
status book(airline *l,char *flight_num,customer *c,char *name) /*订票函数*/
{ airline *p=l;
customer *q=c->next ;
p=l->next ;
for(;q->next !=NULL;q=q->next){}
for(;p!=NULL;p=p->next )
{ if(strcmp(flight_num,p->flight_num )==0)
{ if(p->left >0)
{ printf("Congratulations!Tickets booked!\n");
printf("Your seat_number is: %d\n",(p->total -p->left +1));
insert_customer(&q,name,flight_num,p->total -p->left +1);
p->left --;
return 1;
}
else printf("Sorry!Seats full!\n");
return 0;
}
}
printf("Sorry!No this flight!\n");
return 0;
}
status print_airline(airline *l) /*打印航线信息*/
{ airline *p=l->next ;
for(;p!=NULL;p=p->next )
{ printf("%8s%8s%8s%9d%9d\n",p->flight_num ,p->plane_num ,p->destinatio n ,p->total ,p->left );
}
return 1;
}
status print_customer(customer *l) /*打印顾客信息*/
{
customer *p=l->next ;
for(;p!=NULL;p=p->next )
{ printf("%10s %10s %d\n",p->name ,p->flight_num ,p->seat_num );
}
return 1;
}
void main()
{ char choice,name[9],flight_num[8];
int t=1,k=1;
airline *air=init_airline();
customer *cus=init_customer();
printf("Airline Tickets Book System\n");
printf(" \n");
creat_airline(&air);
creat_customer(&cus);
while(t==1)
{ printf("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n");
printf("**Welcome To Airline Tickets Book System**\n");
printf("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n");
printf("########################################\n");
printf("----++++++++-------主菜单-----++++++++++-------\n");
printf("# 订票-------0 #\n");
printf("# 退票-------1 #\n");
printf("#查询-------2 #\n");
printf("#信息载入---3 #\n");
printf("#退出------4 #\n");
printf("###################################\n");
if(k) { printf("请选择相应操作: \n"); k=0; }
else printf("请选择相应操作:\n");
choice = getch();
printf("%c\n",choice);
if(choice=='0')
{ printf("Please input your flight number: ");
scanf( "%s",flight_num);
printf("Please input your name: ");
scanf( "%s",name);
book(air,flight_num,cus,name);
save_airline(air);
save_customer(cus);
}
else if(choice=='1')
{ printf("\nPlease input your name: ");
scanf( "%s",name);
delete_cus(cus,air,name);
save_airline(air);
save_customer(cus);
}
else if(choice=='2')
{ printf("\n flight_number plane_number destination total tickets_num left tickets_num\n");
print_airline(air);
printf(" name flight_number seat_number\n");
print_customer(cus);
}
else if(choice=='3')
{ printf("flight_num plane_num destination total left\n" );
load_airline(air);
printf("\t name \t\tflight_num\tseat_num \n");
load_customer(cus);
}
else if(choice=='4')
{ printf("Good bye!Please enjoy your travel!");
t=0;
}
else
{ printf("Input error!\n");
}
}
getch(); }。

相关文档
最新文档