代码分享如下
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#define ERROR 0
#define OK 1
typedef struct aldbook{//已订票人结点
char albookname[20]; //订票人姓名
int albookno; //订票量
struct aldbook *next;
}aldbook,*albook;
typedef struct undbook{//等候订票人节点
char unbookname[20];//等候订票人姓名
int unbookno;//所需票量
struct undbook *next;
}undbook,*unbook;
typedef struct{ //队列
unbook front; //队头指针
unbook rear; //队尾指针
}linkqueueun;
typedef struct airline{ //航线结点
char terminusname[20];//终点站名
char flightno[10];//航班号
char planeno[10];//飞机号
char flytime[10];//起飞时间
int allnumber;//乘员定额
int unsellno;//余票量
struct airline *next;
albook albookP;
linkqueueun QY;
}airline,*air;
int initqueue(linkqueueun &Q)
{//建立以Q为头结点的链队列,成功返回OK,否则返回ERROR
Q.front=Q.rear=(unbook)malloc(sizeof(undbook)); //分配存储空间,使队头队尾指针指向同一结点
if(!Q.front) return ERROR; //分配空间失败
Q.front->next=NULL;
return OK;
}//initqueue()
int initlist1(air &P)
{ //建立存储航线信息的空链表
P=(air)malloc(sizeof(airline));
if(!P) return ERROR;
P->next=NULL;
return OK;
}//initlist1()
int initlist2(albook &Y)
{ //建立存储已定票人信息的空链表
Y=(albook)malloc(sizeof(aldbook));
if(!Y) return ERROR;
Y->next=NULL;
return OK;
}//initlist2()
int enqueue(linkqueueun &Q,char name[],int num);
int deletelist(albook &Y,char name[])//在存放已定票人信息的链表中删除用户名为name[]的结点
{ //并返回此用户的订票数量
int i;
albook p,q;
p=Y->next;
q=Y;
while(p&&strcmp(p->albookname,name)) {p=p->next;q=q->next;}
if(!p) {
printf("对不起!本航班无此用户信息!请确认是否输入正确!n");
return ERROR;
}
i=p->albookno;
q->next=p->next;
free(p);
return i;
}//deletelist()
int createlistcorrect(air &p)//建立存储航线信息的链表
{
air newbase,pa;
pa=p;
newbase=(airline *)malloc(sizeof(airline));
if(!newbase) return ERROR;
printf("请依此输入以下内容:n");
printf("请输入终点站名:");
scanf("%s",newbase->terminusname);
printf("请输入航班号:");
scanf("%s",newbase->flightno);
printf("请输入飞机号:");
scanf("%s",newbase->planeno);
printf("请输入飞行时间(输入格式为:飞行周日(用数字表示)-(具体时间)): ");
scanf("%s",newbase->flytime);
printf("请输入客机容量:");
scanf("%d",&newbase->allnumber);
printf("请输入客机总余票量:");
scanf("%d",&newbase->unsellno);
initlist2(newbase->albookP);
initqueue(newbase->QY);
newbase->next=pa->next;
pa->next=newbase;
return OK;
}//createlistcorrect()
int createlist(air &p)
{//建立以P为头结点的链表
air newbase;
newbase=(airline *)malloc(sizeof(airline));//分配新的存储空间
if(!newbase) return ERROR; //分配失败
strcpy(newbase->terminusname,"上海");//初始化
strcpy(newbase->flightno,"AZ001");
strcpy(newbase->planeno,"BY747");
strcpy(newbase->flytime,"3-09:00");
newbase->allnumber=200;
newbase->unsellno=200;
initlist2(newbase->albookP);//建立存储以订票人信息的头指针
initqueue(newbase->QY);//建立存储预定票人信息的队列
newbase->next=p->next;
p->next=newbase;
return OK;
} //CreateList()
int addflight(air &plane)//增加航班信息,对应按键6
{
char password[20]="gaoyunqiang";
char ps[20];
printf("相关操作需要密码,请输入以确定您的信息n");
scanf("%s",ps);
if(strcmp(ps,password)==0)
{
printf("密码正确n");
createlistcorrect(plane);
return OK;
}
else
{
printf("密码错误,系统将要退出n");
return ERROR;
}
}//addflight()
int deleteflight(air &plane) //删除航班信息,对应按键7
{
char password[20]="gaoyunqiang";
char ps[20];
printf("相关操作需要密码,请输入以确定您的信息n");
scanf("%s",ps);
if(strcmp(ps,password)==0)
{
printf("密码正确n");
char banci[10];
air frontp,delep;
printf("请输入要删除的航班号:");
scanf("%s",banci);
delep=plane->next;
frontp=plane;
for(;delep;)
{
if(strcmp(delep->flightno,banci)==0)
{
frontp->next=delep->next;free(delep);
printf("删除成功!n");
break;
}
else
{
frontp=delep; delep=delep->next;
}
}
if(!delep)
{
printf("无此航班信息!请再次输入确定n");
}
return OK;
}
else
{
printf("密码错误,系统将要退出n");
return ERROR;
}
}//deleteflight()
int searchname(albook p, char name[])//检查姓名,后面调用
{
albook YD;
YD=p->next;
while(YD)
{
if(strcmp(YD->albookname,name)==0)
return OK;
else YD=YD->next;
}
return ERROR;
}//searchname()
int searchaim(air plane)//通过目的地查询航线,对应按键1
{
char Aim[20];
air p;
p=plane->next;
printf("==>请输入您要查找的目的地:");
scanf("%s",Aim);
for(;p;p=p->next)
{
if(strcmp(p->terminusname,Aim)==0)
{
printf("航班号:%s 飞机号:%s 飞行时间:%s 余票量:%dn",
p->flightno,p->planeno,p->flytime,p->unsellno);
return OK;
}//if
}//while
printf("对不起!本航空公司还未开通此航线,敬请谅解n");
return ERROR;
}//searchaim()
int searchnum(air plane)//通过航班号查询航线,对应按键2
{
char Aim[10];
air p;
p=plane->next;
printf("请输入您要查找的航班号:");
scanf("%s",Aim);
for(;p;p=p->next)
{
if(strcmp(p->flightno,Aim)==0)
{
printf("降落城市:%s 飞机号:%s 飞行时间:%s 余票量:%dn",
p->terminusname,p->planeno,p->flytime,p->unsellno);
return OK;
}
}
printf("对不起!本航空公司还未开通此航线,敬请谅解n");
return ERROR;
}//searchnum()
int bookticket(air &plane)//实现订票业务,对应按键3
{
int i,a;
char f;
char banhao[10];
air p;
albook YI;
YI=(albook)malloc(sizeof(aldbook));
p=plane->next;
int num;
loop:printf("请输入您要乘坐的航班号:");
scanf("%s",banhao);
for(;p;p=p->next)
{
if(strcmp(p->flightno,banhao)==0)
{
printf("请输入您要购买的票数:");
getchar();
scanf("%d",&num);
if(p->unsellno>=num)
{
printf("请输入您的姓名:");
getchar();
scanf("%s",YI->albookname);
a=p->allnumber-p->unsellno;
printf("您的座位号为: ");
for(i=1;i<=num;i++)
{
printf("- ",a+i);
}
printf("n");
p->unsellno-=num;
YI->albookno=num;
YI->next=p->albookP->next;
p->albookP->next=YI;
printf("订票成功,为保护您的合法权益,请在主页面进行登记n");
return OK;
}
else
{
printf("==>对不起,本航班剩余座位已不足,请到主页面进行登记");
return ERROR;
}
}
}
printf("对不起,本航空公司未开通此航线,请确认输入是否正确n");
printf("是否重新输入(Y/N?):");
getchar();
scanf("%c",&f);
if(f=='Y'||f=='y') goto loop;
else return ERROR;
}//bookticket()
int tuipiao(air &plane)
{
char date[20];
char HB[10];
char name[20];
int num;
air p;
unbook Q;
Q=(unbook)malloc(sizeof(undbook));
albook Y;
Y=(albook)malloc(sizeof(aldbook));
printf("==>请输入您要退票的航班:");
getchar();
gets(HB);
for(p=plane->next;p;p->next)
{
if(strcmp(p->flightno,HB)==0)
{
RePutDate: printf("==>请输入乘坐日期:");
gets(date);
if(strcmp(p->flytime,date)==0)
{
printf("==>请输入用户名:");
gets(name);
if(searchname(p->albookP,name))
{
num=deletelist(p->albookP,name);
p->unsellno+=num;
Q=p->QY.front->next;
while(Q)
{
if(Q!=NULL&&Q->unbookno<=p->unsellno)
{
Y->albookno=Q->unbookno;
strcpy(Y->albookname,Q->unbookname);
p->unsellno=p->unsellno-Q->unbookno;
Y->next=p->albookP->next;
p->albookP->next=Y;
}
Q=Q->next;
}
return OK;
}
else
{
printf("==>无此顾客信息,请确认输入是否正确,系统退出n");
}
return ERROR;
}
else
{
printf("==>输入的日期错误,请重新输入:");
goto RePutDate;
}
}
else
{
printf("==>无本航班信息,请确认输入是否正确,系统退出n");
return ERROR;}
}
return OK ;
}//tuipiao()
int albookrecord(albook &p)//建立存储已订票人信息的链表,对应按键10
{
albook newbase,pa;
pa=p;
newbase=(aldbook *)malloc(sizeof(aldbook));
if(!newbase) return ERROR;
printf("请依此输入以下内容:n");
printf("请输入姓名:");
scanf("%s",newbase->albookname);
printf("请输入定票量:");
scanf("%d",&newbase->albookno);
newbase->next=pa->next;
pa->next=newbase;
return OK;
}//albookrecord()
void Printal(albook b)//实现显示全部订票人信息,对应按键8
{
char password[20]="gaoyunqiang";
char ps[20];
printf("相关操作需要密码,请输入以确定您的信息n");
scanf("%s",ps);
if(strcmp(ps,password)==0)
{
printf("密码正确n");
albook p;
p=b->next;
if(!p) printf("对不起,暂无已订票人信息n");
for(;p;p=p->next)
{
printf("订票人姓名:%s 定票量:%dn",
p->albookname,p->albookno);
}
}
else printf("密码错误,系统将要退出n");
}//Printal()
int enqueue(linkqueueun &Q,char name[],int num)//在队列Q中插入新的队尾元素,对应按键11
{
unbook Y;
Y=(unbook)malloc(sizeof(undbook));
if(!Y) return ERROR;
strcpy(Y->unbookname,name);
Y->unbookno=num;
Y->next=NULL;
Q.rear->next=Y;
Q.rear=Y;
return OK;
} // enqueue()
void Printun(linkqueueun &Q)//实现显示全部预订票人信息,对应按键9
{
char password[20]="gaoyunqiang";
char ps[20];
printf("相关操作需要密码,请输入以确定您的信息n");
scanf("%s",ps);
if(strcmp(ps,password)==0)
{
printf("密码正确n");
unbook p;
p=Q.front->next;
if(!p) printf("对不起,暂无等候排队订票人信息n");
for(;p;p=p->next)
{
printf("等候人姓名:%s 所需票量:%dn",
p->unbookname,p->unbookno);
}
}
else printf("密码错误,系统将要退出n");
}//Printun()
void Print(air plane)//实现显示全部航班信息,对应按键5
{
air p;
p=plane->next;
if(!p) printf("对不起,本航空公司无航线信息n");
for(;p;p=p->next)
{
printf("降落城市:%s 航班号:%s 飞机号:%s 飞行时间:%s 余票量:%dn",
p->terminusname,p->flightno,p->planeno,p->flytime,p->unsellno);
}
}//Print()
void main()//主函数
{
int a;
air plane;
albook alb;
linkqueueun Q;
initqueue(Q);
initlist1(plane);
initlist2(alb);
createlist(plane);
loop1:printf("----------------------欢迎使用本公司航空客运订票系统----------------------------n");
printf("-----------------本系统具有如下功能,请选择您要办理的业务-----------------------n");
printf("====== 1、通过目的地查询航线n");
printf("====== 2、通过航班号查询航线n");
printf("====== 3、客票订购n");
printf("====== 4、办理退票n");
printf("====== 5、查看全部航班信息n");
printf("====== 框内为管理员操作,非相关人员勿进=======================n");
printf("| |n");
printf("====== 6、添加航班信息 |n");
printf("====== 7、删除航班信息 |n");
printf("====== 8、查看已订票人信息 |n");
printf("====== 9、查看预订票人信息 |n");
printf("| |n");
printf("====== 框内为管理员操作,非相关人员勿进=======================n");
printf("====== 10、已订票人登记n");
printf("====== 11、等候订票人登记n");
printf("====== 12、退出系统n");
printf("-----------------本系统具有以上功能,请选择您要办理的业务-----------------------n");
printf("请输入您要办理的业务:");
loop2:scanf("%d",&a);
switch(a)
{
case 1:
system("pause");
system("cls");
printf("------------------------------欢迎使用查询航线业务------------------------------n");
searchaim(plane);
system("pause");
system("cls");
goto loop1;
break;
case 2:
system("pause");
system("cls");
printf("------------------------------欢迎使用查询航线业务------------------------------n");
searchnum(plane);
system("pause");
system("cls");
goto loop1;
break;
case 3:
system("pause");
system("cls");
printf("------------------------------欢迎使用客票预订业务------------------------------n");
bookticket(plane);
system("pause");
system("cls");
goto loop1;
break;
case 4:
system("pause");
system("cls");
printf("------------------------------欢迎使用办理退票业务------------------------------n");
if(tuipiao(plane))
printf("退票成功n");
else printf("退票失败n");
system("pause");
system("cls");
goto loop1;
break;
case 5:
system("pause");
system("cls");
printf("---------------------------欢迎使用全部航线查询业务---------------------------n");
Print(plane);
system("pause");
system("cls");
goto loop1;
break;
case 6:
system("pause");
system("cls");
printf("---------------------------警告:非相关人员勿擅自操作---------------------------n");
addflight(plane);
system("pause");
system("cls");
goto loop1;
break;
case 7:
system("pause");
system("cls");
printf("---------------------------警告:非相关人员勿擅自操作---------------------------n");
deleteflight(plane);
system("pause");
system("cls");
goto loop1;
break;
case 8:
system("pause");
system("cls");
printf("---------------------------警告:非相关人员勿擅自操作---------------------------n");
Printal(alb);
system("pause");
system("cls");
goto loop1;
break;
case 9:
system("pause");
system("cls");
printf("---------------------------警告:非相关人员勿擅自操作---------------------------n");
Printun(Q);
system("pause");
system("cls");
goto loop1;
break;
case 10:
system("pause");
system("cls");
printf("---------------------------欢迎使用已订票人登记业务---------------------------n");
albookrecord(alb);
system("pause");
system("cls");
goto loop1;
break;
case 11:
system("pause");
system("cls");
printf("---------------------------欢迎使用等候订票人登记业务---------------------------n");
char name[20];
int num;
printf("请输入您的姓名:");
scanf("%s",name);
printf("请输入您想要预定票量:");
scanf("%d",&num);
enqueue(Q,name,num);
system("pause");
system("cls");
goto loop1;
break;
case 12:
system("pause");
system("cls");
printf("n");
printf("n");
printf("n");
printf("n");
printf("tt感谢您对本航空公司的支持,欢迎下次光临!n");
break;
default :
getchar();
printf("对不起,您输入的数字无效,请重新输入:");
goto loop2;
break;
}
}