|
发表于 2016-5-28 17:51:01
|
显示全部楼层
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*已订票者*/
typedef struct Orderman
{
char name[10];
int ordernum;/*订票量*/
int grade;
struct Orderman *next;
}LinkList;
/*等待订票者*/
typedef struct Waiter
{
char name[10];
int neednum;/*需求量*/
struct Waiter *next;
}LQNode,*Queueptr;/*结点和结点指针类型*/
typedef struct WQueue
{
Queueptr front;
Queueptr rear;
}LinkQueue;
/*航线信息*/
typedef struct Airline
{
char lname[10];/*终点站名*/
char airnum[10];/*航班号*/
char planenum[10];/*飞机号*/
char day[7];/*星期几*/
int passnum;/*乘员定额*/
int surnum;/*余票量*/
LinkList *order;
LinkQueue wait;
}LineInfo;
LineInfo *begin;/*全局变量*/
void Display(LineInfo *infomt)
{
printf("%s\t%3s\t%5s\t%5s\t%8d\t%d\n",infomt->lname,infomt->airnum,infomt->planenum,infomt->day,infomt->passnum,infomt->surnum);
}
void AirLineInfo()/*浏览全部航线信息*/
{
LineInfo *infomt;
int i=0;
infomt=begin;
printf("终点站\t航班号\t飞机号\t飞行周日 乘员定额 余票量\n");
for(;i<10;i++,infomt++)
Display(infomt);
printf("\n");
}
void SearchOneLine()/*查询某一航班信息*/
{
LineInfo *infomt;
int i=0,j=0;
char name[20];
infomt=begin;
printf("请输入终点站名:");
scanf("%s",name);
for(;i<10;i++,infomt++)
if(0==strcmp(name,infomt->lname))/*按终点站名查询航班*/
{
j++;
if(1==j) printf("终点站\t航班号\t飞机号\t飞行周日 乘员定额 余票量\n");
Display(infomt);
}
if(!j) printf("对不起,没有飞往%s的航班\n",name);
}
LineInfo *FindAirnum()/*根据输入的航班号查询并以指针形式返回*/
{ LineInfo *infomt;
int i=0;
char number[10];
infomt=begin;
printf("请输入航班号:");
scanf("%s",number);
for(;i<10;infomt++,i++)
if(!strcmp(number,infomt->airnum))
return infomt;
printf("对不起,没有该航班!\n");
return NULL;
}
void PutOrder() /*输出订票客户的名单信息*/
{ LinkList *p;
LineInfo *infomt;
infomt=FindAirnum();/*根据航班号得到*/
p=infomt->order;
if(p!=NULL)
{
printf("客户姓名 订票数额 舱位等级\n");
for(;p!=NULL;p=p->next)
printf("%s\t\t%d\t%d\n",p->name,p->ordernum,p->grade);
}
else printf("该航线暂时没有客户订票!\n");
}
void PutWaiter()/*输出等候订票客户的名单信息*/
{ Queueptr p;
LineInfo *infomt;
infomt=FindAirnum();
p=infomt->wait.front;
if(p!=NULL)
{
printf("客户姓名 预订票数\n");
for(;p!=NULL;p=p->next)
printf("%s\t\t%d\n",p->name,p->neednum);
}
else printf("该航线暂时没有客户等票!\n");
}
LinkList *InsertLink(LinkList *head,int amount,char name[],int grade)/*增加订票乘员名单域的客户信息*/
{ LinkList *p,*newbase;
p=head;
newbase=(LinkList*)malloc(sizeof(LinkList));
if(!newbase)
{
printf("\nNo enough memory\n");
return NULL;
}
strcpy(newbase->name,name);
newbase->ordernum=amount;
newbase->grade=grade;
newbase->next=NULL;
if(head==NULL)/*若原无订票客户信息*/
{
head=newbase;
newbase->next=NULL;
}
else
{
for(;p->next!=NULL;p=p->next);
p->next=newbase;
}
return head;
}
LinkQueue AddQueue(LinkQueue q,char name[],int amount)/*增加排队等候的客户名单域*/
{ Queueptr newbase;
newbase=(Queueptr)malloc(sizeof(Queueptr));
strcpy(newbase->name,name);
newbase->neednum=amount;
newbase->next=NULL;
if(q.front==NULL)/*若原排队等候客户名单域为空*/
q.front=newbase;
else
q.rear->next=newbase;
q.rear=newbase;/*队列尾指针位于新加入的结点*/
return q;
}
void Order()/*订票业务*/
{ LineInfo *infomt;
int amount,grade;
char name[10],select1,select2;
infomt=begin;
infomt=FindAirnum();
while(NULL==infomt)/*根据客户提供的航班号进行查询,若为空询问是否需要查询其他航班*/
{
printf("请问您是否需要查询别的航班?需要请按Y,不需要请按N:");
fflush(stdin);
scanf("%c",&select1);
if(select1=='Y'||select1=='y')
infomt=FindAirnum();
else return;
}
printf("请输入您订票的数量:");
scanf("%d",&amount);
if(amount>infomt->passnum)/*客户订票额超过乘员定额*/
{ char choose1,choose2;
printf("对不起,您的订票数量已经超过乘员定额!\n按Y:查询到达同一目的地的其他航班,按N:退出:");
fflush(stdin);
scanf("%c",&choose1);
if(choose1=='Y'||choose1=='y')
{
SearchOneLine();
printf("请问是否选择其他航班,按Y:是,按N:否");
fflush(stdin);
scanf("%c",&choose2);
if(choose2=='Y'||choose2=='y')
{
infomt=FindAirnum();
printf("请输入您订票的数量:");
scanf("%d",&amount);
}
else return;
}
else return;
}
if(amount<=infomt->surnum)/*客户订票额未超过余票量,订票成功并等记信息*/
{
int i;
printf("请输入您的姓名(订票客户):");
scanf("%s",name);
printf("请输入您购买的机票舱位等级(等级1,2或3):");
scanf("%d",&grade);
while(grade>3)/*舱位等级1,2,或3,输入错误需重新输入*/
{
printf("舱位等级只有1,2或3,请重新输入:");
scanf("%d",&grade);
}
infomt->order=InsertLink(infomt->order,amount,name,grade);/*在订票乘员名单域中添加客户信息*/
for(i=0;i<amount;i++)/*依次输出该订票客户的座位号*/
printf("%s的座位号是:%d\n",name,infomt->passnum-infomt->surnum+i+1);
infomt->surnum-=amount;/*该航线的余票量应减掉该客户的订票量*/
printf("订票成功,祝您旅途愉快!\n");
}
else /*若满员或余票不足,询问客户是否需要查询其他航班或排队等候*/
{
printf("余票不足,按Y:排队等候,按C:查询到达同一目的地的其他航班,按N:退出:");
fflush(stdin);
scanf("%c",&select2);
if(select2=='Y'||select2=='y')
{ printf("请输入您的姓名(等候订票客户):");
scanf("%s",name);
infomt->wait=AddQueue(infomt->wait,name,amount);/*在排队等候乘员名单域中添加客户信息*/
printf("等候登记成功!余票足够时会自动为您订票\n");
}
else if(select2=='C'||select2=='c')
{
char choose3;
SearchOneLine();
printf("请问是否选择其他航班,按Y:是,按N:否:");
fflush(stdin);
scanf("%c",&choose3);
if(choose3=='Y'||choose3=='y')
Order();/*d递归调用*/
else return;
}
else printf("欢迎您再次订购!\n");
}
}
void RefundTicket()/*退票业务*/
{
LineInfo *infomt;
LQNode *t,*q,*f,*r;
LinkList *p1,*p2,*head;
int grade,num;
char refundname[10];
if(!(infomt=FindAirnum())) return;/*调用查询函数,根据客户提供的航班号进行搜索*/
head=infomt->order;
p1=head;
printf("请输入您的姓名(退票客户):");
scanf("%s",refundname);
for(;p1!=NULL;p1=p1->next)/*根据客户提供的姓名到订票客户名单域进行查询*/
{
if(0==strcmp(refundname,p1->name))
break;
p2=p1;
}
if(!p1)
{
printf("对不起,您没有订过票!\n");/*若未订过票,退出订票业务*/
return;
}
else/*查询成功,删除订票客户名单中该订票者的信息*/
{
printf("您的订票量为:%d\n",p1->ordernum);
printf("请输入您的退票数量:");
scanf("%d",&num);
while(num>p1->ordernum)/*退票数量超出订票数量*/
{
printf("退票数量超出订票数量,请重新输入:");
scanf("%d",&num);
}
if(p1==head)
{ grade=p1->grade;
if(p1->ordernum==num)/*退所有的票*/
{
head=p1->next;
free(p1);
}
else
{
if(num<p1->ordernum)/*退部分票*/
head->ordernum-=num;
}
}
else
{
if(p1->ordernum==num)/*退所有的票*/
{
p2->next=p1->next;
free(p1);
}
else
{
if(num<p1->ordernum)
p1->ordernum-=num;
}
}
infomt->surnum+=num;/*余票增加*/
printf("退票成功!欢迎您再次订票\n");
}
infomt->order=head;/*重新将航线名单域指向订票单链表的头指针 */
if(infomt->wait.front==NULL)
return;
f=(infomt->wait).front;
r=(infomt->wait).rear;
t=f;/*t为满点条件的等候名单域*/
if(infomt->wait.front->neednum<=infomt->surnum)/*头结点的等候者满足订票条件*/
{
int i;
infomt->wait.front=t->next;/*头结点出队列*/
printf("%s订票成功!\n",t->name);
for(i=0;i<t->neednum;i++)/*输出座位号*/
printf("%s的座位号是:%d\n",t->name,(infomt->surnum)-i);
infomt->surnum-=t->neednum;
infomt->order=InsertLink(infomt->order,t->neednum,t->name,grade);/*插入到订票客户名单链表中*/
free(t);
return;
}
else printf("余票不足,%s",t->name);
for(q=t,t=t->next;t!=NULL;q=t,t=t->next)
{
if((infomt->surnum)>=(t->neednum)&&t!=NULL)/*若头结点的等候者不满足订票条件*/
{
int i;
q->next=t->next;/*t结点出队列*/
printf("\n\t%s订票成功!\n",t->name);
for(i=0;i<t->neednum;i++)
printf("%s的座位号是:%d\n",t->name,(infomt->surnum)-i);
infomt->surnum-=t->neednum;
infomt->order=InsertLink(infomt->order,t->neednum,t->name,grade);
free(t);
return;
}
else printf(",%s",t->name);
}
printf("都无法成功订票\n");
}
int Menu()/*主函数*/
{
char s[20];
int c;
printf("-------------------------------------------------------------------------------- "
"----------------------- 欢迎使用航空客运订票系统 ----------------------"
" * * * * * * * * * * \n"
" 1.浏览全部航线信息 \n"
" 2.查询航线信息 \n"
" 3.浏览订票客户信息 \n"
" 4.浏览等候订票客户信息 \n"
" 5.办理订票业务 \n"
" 6.办理退票业务 \n"
" 7.退出系统 \n"
" 计科6班 3114006065 郑湘平 \n"
"-------------------------------------------------------------------------------- ");
do{
printf(" 请选择并按回车键结束:");
scanf("%s",s);
c=atoi(s);
}while(c<0||c>7);
return c;
}
void main()
{
LineInfo air[10]={{"北京","1","B1530","日",5,5},{"上海","2","S1112","一",5,5},{"香港","3","H1026","二",5,5},
{"广州","4","G1011","三",5,5},{"深圳","5","S2140","四",5,5},{"北京","6","B3054","五",10,10},
{"上海","7","S4839","六",10,10},{"香港","8","H2964","日",10,10},
{"广州","9","G3945","日",10,10},{"深圳","10","S7421","一",10,10}};/*初始化航线信息*/
begin=air;
for(;;)
{
switch(Menu()){
case 1:AirLineInfo();break;
case 2:SearchOneLine();break;
case 3:PutOrder();break;
case 4:PutWaiter();break;
case 5:Order();break;
case 6:RefundTicket();break;
case 7:exit(0);
}
}
}
|
|