回复 10楼 vandychan
楼主我这个程序有问题 可以帮忙看一下吗?与题目要求好像还不完全符合
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define OK 1
#define ERROR 0
typedef struct flightnode{ /*定义航班信息结点*/
char air_num[10];
char start_time[15];
char end_time[15];
char end_place[30];
int total;
int left;
int price;
struct flightnode *next;
}flightnode;
typedef struct passengernode{ /*定义乘客资料结点*/
char name[20];
char ID_num[20];
char air_num[20];
int seat_num;
struct passengernode *next;
}passengernode;
flightnode *head_flight(); /*生成航班信息的头结点*/
passengernode *head_passenger(); /*生成乘客资料的头结点*/
int CreatFlight(flightnode **l); /*生成航班信息链表*/
int CreatPassenger(passengernode **l); /*生成乘客资料链表*/
int InsertFlight(flightnode **p,char *air_num,char *start_time,char *end_place,int total,int left,int price); /*插入航班信息的结点*/
int InsertPassenger(passengernode **p,char *name,char *ID_num,char *air_num,int seat_num); /*插入乘客资料的结点*/
flightnode *modefy_airline(flightnode *l,char *air_num); /*修改航班信息的函数*/
int BookTicket(flightnode*a,char *air_num,passengernode *c,char *name,char *ID_num); /*订票函数*/
int CancelTicket(passengernode *c,flightnode*l,char *name,char *ID_num); /*退票函数*/
int SearchFlight(flightnode*head); /*查询航班信息函数*/
int SearchPassenger(passengernode *head); /*查询乘客信息函数*/
void main() /*主函数*/
{
int p=2; /*p==1时进入主菜单,p==0时退出系统*/
passengernode *cus=head_passenger();
flightnode *air=head_flight();
char name[20],air_num[20],ID_num[30],mima[20],ch;
char t[20]="zhengjianze"; /*管理员密码*/
void clrscr();
CreatFlight(&air);
CreatPassenger(&cus);
while(p==2)
{
printf("\n welcome to the airline system!\n\n please choose the serves below and press the number:\n\n");
printf(" ================================================================\n\n");
printf(" 1 ->订票 \n");
printf(" 2 -> 退票 \n");
printf(" 3 ->查询 \n");
printf(" 4 ->退出系统 \n\n");
printf(" ================================================================\n");
switch(ch=getchar()){
case '1':{printf("\n *-------------Booking Ticket-------------*\n"); /*进入订票功能*/
printf("Please input a airline number and press enter for OK:");
scanf("%s",air_num);
printf("Please input your name and press enter for OK:");
scanf("%s",name);
printf("Please input your ID number and press enter for OK:");
scanf("%s",ID_num);
BookTicket(air,air_num,cus,name,ID_num);getchar();}
;break;
case '2':{printf(" *-----------------------Canceling Ticket--------------------*\n"); /*进入退票功能*/
printf("Please input the cancel name for booking ticket:");
scanf("%s",name);printf("Please input the ID for booking ticket:");scanf("%s",ID_num);
CancelTicket(cus,air,name,ID_num);
};break;
case '3':{printf("*---------------Search Information---------------------*\n"); /*进入信息查询功能*/
printf(" 1 -> flight information \n" );
printf(" 2 -> passenger information\n ");
printf(" other -> exit \n");
printf("*------------------------------------------------------*\n");
if((ch=getchar())=='1') /*航班信息查询*/
{printf("*------------------ flight information -----------------*\n");
SearchFlight(air);}
else if(ch=='2'){ /*乘客资料查询*/
printf("\n please input the key:");scanf("%s",mima); /*输入管理员密码*/
if(strcmp(mima,t)==0){printf("\n*---------------passenger information ---------------*\n\n");
SearchPassenger(cus);}
else printf("sorry,the key is wrong");getchar();}}break;
case '4':{getchar(); /*退出系统*/
p=0;}break;
}
}}
flightnode *head_flight()
{
flightnode *a;
a=(flightnode *)malloc(sizeof(flightnode));
if(a==NULL)
a->next=NULL;
return a;
}
passengernode *head_passenger()
{
passengernode *c;
c=(passengernode*)malloc(sizeof(passengernode));
if(c==NULL)
c->next=NULL;
return c;
}
int CreatFlight(flightnode **l)
{
flightnode *p=*l;
int i=0;
char *air_num[3]={"flight01","flight02","flight03"};
char *start_time[3]={" 08:00"," 12:00"," 16:30"};
char *end_place[3]={"Guangzhou","Shenzhen","Shantou"}; /*初始化航班信息*/
int total[3]={100,100,100};
int price[3]={300,200,250};
int left[3]={23,54,76};
for(i=0;i<3;i++)
InsertFlight(&p,air_num[i],start_time[i],end_place[i],total[i],left[i],price[i]); /*插入航班信息*/
return OK;
}
int CreatPassenger(passengernode **l)
{
passengernode *p=*l;
int i=0;
char *name[3]={"liming","Ximing","Chongliang"};
char *ID_num[3]={"3105006052","3105006054","3105006051"}; /*初始化乘客资料*/
char *air_num[3]={"flight01","flight02","flight03"};
int seat_num[3]={12,5,8};
for(i=0;i<3;i++)
InsertPassenger(&p,name[i],ID_num[i],air_num[i],seat_num[i]); /*插入乘客资料*/
return OK;
}
int InsertFlight(flightnode **p,char *air_num,char *start_time,char *end_place,int total,int left,int price)
{flightnode *q;
q=(flightnode*)malloc(sizeof(flightnode));
strcpy(q->air_num,air_num);
strcpy(q->start_time,start_time);
strcpy(q->end_place,end_place);q->total=total;
q->left=left;
q->price=price;
q->next=NULL;
(*p)->next=q;
(*p)=(*p)->next; /*航班信息链的尾插法*/
return OK;
}
int InsertPassenger(passengernode **p,char *name,char *ID_num,char *air_num,int seat_num)
{passengernode *q;
q=(passengernode*)malloc(sizeof(passengernode));
strcpy(q->name,name);
strcpy(q->ID_num,ID_num);
strcpy(q->air_num,air_num);
q->seat_num=seat_num;
q->next=NULL;
(*p)->next=q;
(*p)=(*p)->next; /*乘客资料链表的尾插法*/
return OK;
}
flightnode *modefy_airline(flightnode *l,char *air_num)
{
flightnode *p;
p=l->next;
for(;p!=NULL;p=p->next)
{
if(strcmp(air_num,p->air_num)==0)
{
p->left++;
return l;
}
printf("NO the airline!");
return 0;
}}
int BookTicket(flightnode *a,char *air_num,passengernode *c,char *name,char *ID_num)
{flightnode *p=a;
passengernode *q=c->next;
p=a->next;
for(;q->next!=NULL;q=q->next);
for(;p!=NULL;p=p->next)
{if(strcmp(p->air_num,air_num)==0){
if(p->left>0)
{printf(" \nSuccessful for Booking ticket.\n\n Your flight: %s\n Your seat number : %d ",p->air_num,(p->total-p->left+1));printf("\n *-----------------------------------------*");
InsertPassenger(&q,name,ID_num,air_num,p->total-p->left+1);
p->left--; return OK;}
else
{printf("seat is full");return 0;}
}/*if(strcmp)*/ }
printf("Sorry,there isn't the plane,please check the airline number!");printf("\n *-----------------------------------------*");
}
int CancelTicket(passengernode *c,flightnode *l,char *name,char *ID_num)
{
passengernode *p,*pr;
char air_num[20];
pr=c;
p=pr->next;
while(p!=NULL)
{
if(strcmp(p->name,name)==0&&strcmp(p->ID_num,ID_num)==0)
{
strcpy(air_num,p->air_num);
l=modefy_airline(l,air_num);
pr->next=p->next;
p=pr->next;
printf("successful for canceling ticket.");
printf("\n *-----------------------------------------------------------------*");
return OK;
}
pr=pr->next;
p=pr->next;
}
printf("Sorry,there isn't the customer!");
printf("\n *--------------------------------------------------------*");
return ERROR;
}
int SearchFlight(flightnode *head)
{flightnode *p=head->next;
printf(" Flight Start_time Arrival_place Price Total Left_seat\n\n");
for(;p!=NULL;p=p->next)
{
printf(" %s %-10s %-9s %-7d%-6d %-6d\n",p->air_num,p->start_time,p->end_place,p->price,p->total,p->left);printf("\n");
}printf(" *----------------------------------------------------------*\n\n");
return OK;
}
int SearchPassenger(passengernode *head)
{struct passengernode *q=head->next;
printf(" Name ID Flight Seat_number\n\n");
for(;q!=NULL;q=q->next)
{
printf("%-12s %-8s %-5s %d\n",q->name,q->ID_num,q->air_num,q->seat_num);printf("\n");
}
printf("\n *----------------------------------------------------------*");
return OK;}