admin 管理员组

文章数量: 887021

C语言

#include<stdio.h>
#include<stdlib.h>
#include<string.h>


static int airnum=0;//静态变量,自动统计航班数
static int customer_num=0;//静态变量,自动统计客户数


/*航班信息*/
/*typedef */struct airplane{
   char airno[30];//航班号
   char qidianzhan[50];//起点站
   char departure_time[50];//起飞时间
   char landing_time[50];//降落时间
   char zhongdianzhan[50];//终点站
   int seat;//座位数
   int standby_ticket;//余票
   struct airplane *next1;
}/*airplane,*Linklist1*/;


/*客户信息*/
typedef struct customer{
   char name[20];//姓名
   char passport_no[20];//证件号
   int  booking_ticket;//订票数
   int  booking_airnum;//订票编号
   char airno[30];//航班号
   char departure_time[50];//到达时间
   char landing_time[50];//起飞时间
   char zhongdianzhan[50];//终点站
   struct customer *next2;
 //  struct customer *prior;
}customer,*Linklist2;


//打印航班信息
void print(struct airplane *pl)
{
struct airplane *t;
t=pl->next1;
printf("航班号\t起点站\t终点站\t起飞时间\t到达时间\t座位数\t余票\n");
while(t!=NULL)
{
printf("%-6s\t%-6s\t%-6s\t%-8s\t",t->airno,t->qidianzhan,t->zhongdianzhan,t->departure_time);
printf("%-8s\t%-6d\t%-4d\n",t->landing_time,t->seat,t->standby_ticket);
t=t->next1;
}
}


//添加航班
void insert_airplane(struct airplane *pl)
{
struct airplane *insert_node,*q,*t;
int i,j,k=0;
//printf("121324");
pl=pl->next1;
printf("请输入所要添加的航班数目:");
scanf("%d",&j);
getchar();
for(i=0;i<j;i++)
{
insert_node=(struct airplane *)malloc(sizeof(struct airplane));
if(insert_node==NULL)
{
printf("动态内存分配失败");
exit(0);
}
printf("请输入第%d个航班的信息\n",++k);
printf("航班号\n");
gets(insert_node->airno);
printf("起点站\n");
gets(insert_node->qidianzhan);
printf("终点站\n");
gets(insert_node->zhongdianzhan);
printf("起飞时间,格式如:20110101 00:00\n");
gets(insert_node->departure_time);
printf("到达时间,格式如:20110101 00:00\n");
gets(insert_node->landing_time);
printf("座位数\n");
scanf("%d",&insert_node->seat);
printf("余票数\n");
scanf("%d",&insert_node->standby_ticket);
getchar();
if(pl->next1==NULL)
{//如果链表是空的话,直接插入头结点之后
pl->next1=insert_node;
insert_node->next1=NULL;
}
else
{//如果链表不是空的
q=pl->next1;//此时q指向第一个数据结点
if(strcmp(insert_node->airno,q->airno)<0)
{//如果成立,则将insert_node指向的结点插入头结点之后
pl->next1=insert_node;
insert_node->next1=q;
}
else
{//否则,继续向后寻找插入的位置
while(strcmp(insert_node->airno,q->airno)>0&&(q->next1!=NULL))
{//如果insert_node指向的航班号大于q所指向的航班号并且q指向的不是表尾
//则t指向q所指的当前结点,q指向下一个结点
t=q;
q=q->next1;
}
if(strcmp(insert_node->airno,q->airno)<=0)
{//如果成立,则将insert_node所指向的结点插入到链表中间
t->next1=insert_node;
insert_node->next1=q;
}
else
{//否则,则将insert_node指向的结点插入到q指向的表尾之后
q->next1=insert_node;
insert_node->next1=NULL;
}
}
}
airnum++;
}
printf("已成功插入航班\n");
}


//修改信息
void change_airplane(struct airplane *pl/*,Linklist2 kehu*/)
{
int /*change,i,*/j,flag=0;
char /*passport_1[20],*/air_num[30];//证件号和航班号
// kehu=kehu->next2;
pl=pl->next1;
/* printf("\t\t欢迎使用修改系统\n");
printf("\t**************************\n");
printf("\t==>1.修改客户订票信息\n");
printf("\t==>2.修改航班信息\n");
printf("\t**************************\n");
printf("请选择:");
scanf("%d",&change);
getchar();
if(change==1)
{
printf("请输入客户证件号:");
gets(passport_1);
for(i=0;i<customer_num;i++)
{
if(strcmp(passport_1,kehu->passport_no)==0)
{
printf("客户信息如下,请修改\n");
printf("姓名\t证件号\t订票编号\t订票数\t航班号\t起飞时间\t到达时间\t终点站\n");
printf("%-4s\t%-6s\t%-8d\t%-6d\t",kehu->name,kehu->passport_no,kehu->booking_airnum,kehu->booking_ticket);
printf("%-6s\t%-8s\t%-8s\t%-6s\n",kehu->airno,kehu->departure_time,kehu->landing_time,kehu->zhongdianzhan);
printf("姓名\n");
gets(kehu->name);
// printf("%s",kehu->name);
printf("证件号\n");
gets(kehu->passport_no);
printf("订票编号\n");
scanf("%d",&kehu->booking_airnum);
getchar();
printf("信息修改成功\n");
flag=1;
}
kehu=kehu->next2;
}
}
if(change==2)
{
printf("请输入客户证件号:");
gets(passport_1);
for(i=0;i<customer_num;i++)//先查找客户
{
if(strcmp(passport_1,kehu->passport_no)==0)
{*/
printf("请输入所要修改航班信息的航班号:");
gets(air_num);
for(j=0;j<airnum;j++)
{
if(strcmp(air_num,pl->airno)==0)
{
/*strcpy(kehu->airno,pl->airno);//航班号复制
strcpy(kehu->departure_time,pl->departure_time);//起飞时间复制
strcpy(kehu->landing_time,pl->landing_time);//降落时间复制
strcpy(kehu->zhongdianzhan,pl->zhongdianzhan);//终点站复制
printf("信息修改成功\n");*/
printf("\n现在你可以修改航班信息了\n");
printf("新航班号\n");
gets(pl->airno);
printf("新起点站\n");
gets(pl->qidianzhan);
printf("新终点站\n");
gets(pl->zhongdianzhan);
printf("新起飞时间,格式如:20110101 00:00\n");
gets(pl->departure_time);
printf("新到达时间,格式如:20110101 00:00\n");
gets(pl->landing_time);
printf("新座位数\n");
scanf("%d",&pl->seat);
printf("新余票数\n");
scanf("%d",&pl->standby_ticket);
getchar();
printf("\n航班信息修改成功!\n");
flag=1;
break;
}
pl=pl->next1;
}
// }
//kehu=kehu->next2;
//}
// }
if(flag==0)
printf("系统中并不存在该航班的信息\n");
}


//退票系统
void cancel_airplane(struct airplane *pl,Linklist2 kehu)
{
Linklist2 p,q,t;
char passport[20];
int i,j,k;
t=kehu;
pl=pl->next1;
printf("请输入证件号:");
gets(passport);
for(i=0;i<customer_num;i++)
{
if(strcmp(passport,kehu->passport_no)==0)//寻找相同的证件号
{
for(k=0;k<airnum;k++)
{
if(strcmp(kehu->airno,pl->airno)==0)//寻找航班,为退票后余票数自加1做准备
{
kehu=t;//重新指向头结点
printf("\n\t\t是否退票\n");
printf("\t**************************\n");
printf("\t==>\t1.确定\n\t==>\t2.取消\n");
printf("\t**************************\n");
printf("请选择:");
scanf("%d",&j);
getchar();
if(j==1)
{
if(kehu->next2==NULL)
printf("该链表是一个空链表");
//如果链表不是空表
else
{
p=kehu->next2;
while(strcmp(p->passport_no,passport)!=0 && p->next2!=NULL)
{/*如果p->passport_no与passport不同,并且p指向的不是表尾,则
 q指向p指向的当前结点,p指向下一个结点*/
q=p;
p=p->next2;//这是寻找证件号的前一个结点
}
}
//如果成立,则p指向的结点为要删除的节点
if(strcmp(p->passport_no,passport)==0)
{
if(p==kehu->next2)
kehu->next2=p->next2;//删除第一个数据结点
else if(p->next2!=NULL)
q->next2=p->next2;//删除表中间的结点
else
q->next2=NULL;//删除表尾
printf("已成功退票!\n");
free(p);
pl->standby_ticket++;
}
else
printf("系统中并不存在该客户的订票信息");
}
else
printf("已取消操作!\n");
}
pl=pl->next1;
}
}
kehu=kehu->next2;
}
/* for(i=0;i<customer_num;i++)
{
if(strcmp(passport,kehu->passport_no)==0)
{
for(k=0;k<airnum;k++)
{
if(strcmp(kehu->airno,pl->airno)==0)
{
printf("订票信息:\n");
printf("姓名\t证件号\t订票编号\t订票数\t航班号\t起飞时间\t到达时间\t终点站\n");
printf("%-4s\t%-6s\t%-8d\t%-6d\t",kehu->name,kehu->passport_no,kehu->booking_airnum,kehu->booking_ticket);
printf("%-6s\t%-8s\t%-8s\t%-6s\n",kehu->airno,kehu->departure_time,kehu->landing_time,kehu->zhongdianzhan);
printf("\n\t\t是否退票\n");
printf("\t**************************\n");
printf("\t==>\t1.确定\n\t==>\t2.取消\n");
printf("\t**************************\n");
printf("请选择:");
scanf("%d",&j);
getchar();
if(j==1)
{
p=kehu;
q=p->next2;
p->next2=q->next2;
/* q=p;
p=p->next2;*/
/* free(q);
pl->standby_ticket++;
printf("已成功退票\n");
}
else if(j!=1)
printf("已取消操作\n");
}
pl=pl->next1;
}
}
kehu=kehu->next2;
}
pl=L->next1;*/
}


//客户信息浏览功能
void scan_info(Linklist2 kehu)
{
Linklist2 t;
t=kehu->next2;
printf("姓名\t证件号\t订票编号\t订票数\t航班号\t起飞时间\t到达时间\t终点站\n");
while(t!=NULL)
{
printf("%-4s\t%-6s\t%-8d\t%-6d\t",t->name,t->passport_no,t->booking_airnum,t->booking_ticket);
printf("%-6s\t%-8s\t%-8s\t%-6s\n",t->airno,t->departure_time,t->landing_time,t->zhongdianzhan);
t=t->next2;
}
printf("\n");


}


//客户信息采集
Linklist2 input_customer_info(struct airplane * pl,char flight_no[],int ticket)
{
Linklist2 h,q,p;
int i,j=0;
h=(Linklist2)malloc(sizeof(customer));
if(h==NULL)
{
printf("动态内存分配失败\n");
exit(0);
}
//输入头结点数据
h->name[0]='\0';
h->passport_no[0]='\0';
h->booking_ticket=0;
h->booking_airnum=0;
h->airno[0]='\0';
h->departure_time[0]='\0';
h->landing_time[0]='\0';
h->zhongdianzhan[0]='\0';
h->next2=NULL;
// h->prior=NULL;
q=h;
for(i=0;i<ticket;i++)
{ //p指向新创建的结点
pl->standby_ticket--;
p=(Linklist2)malloc(sizeof(customer));
if(p==NULL)
{
printf("动态内存分配失败");
exit(0);
}
//输入新结点数据
p->booking_ticket=1;
strcpy(p->airno,flight_no);
printf("请填写第%d位客户的信息:\n",++j);
printf("姓名:");
gets(p->name);
printf("证件号:");
gets(p->passport_no);
printf("订票编号:");
scanf("%d",&p->booking_airnum);
getchar();
strcpy(p->departure_time,pl->departure_time);//起飞时间
strcpy(p->landing_time,pl->landing_time);//降落时间
strcpy(p->zhongdianzhan,pl->zhongdianzhan);//终点站
p->next2=NULL;
q->next2=p;//将p指针指向的新结点链接到表尾
q=p;//q移到表尾
customer_num++;
}
return(h);//返回链表头指针
}


//机票预订系统
Linklist2 book_airplane(struct airplane *pl,Linklist2 kehu)
{
struct airplane *p;
char destination1[50],flight_no[30];//目的地,终点站
int i,plnum=0/*查询到的航班数目*/;
int choice/*是否重订选项*/,ticket;//订票数
int flag1=0,run=0;//确定航线有无的标志
p=pl;
printf("请输入终点站:\n");
gets(destination1);
for(i=0;i<airnum;i++)//把去往该目的地的所有航班列举出来
{
pl=pl->next1;
if(strcmp(destination1,pl->zhongdianzhan)==0)
{
printf("前往该目的地的航班信息:\n");
printf("航班号\t起点站\t终点站\t起飞时间\t到达时间\t座位数\t余票\n");
printf("%-6s\t%-6s\t%-6s\t%-8s\t",pl->airno,pl->qidianzhan,pl->zhongdianzhan,pl->departure_time);
printf("%-8s\t%-6d\t%-4d\n",pl->landing_time,pl->seat,pl->standby_ticket);
flag1=1;
plnum++;
}
}
pl=p->next1;//上面查询完航班后,pl的指向已经改变,所以要让他指向头结点
if(flag1==1)//有航班情况
{
printf("请输入航班号:");
gets(flight_no);
for(i=0;i<plnum;i++)
{


if(strcmp(flight_no,pl->airno)==0)
{
printf("输入订票数:");
scanf("%d",&ticket);
getchar();
if(ticket<=pl->standby_ticket)
{
kehu=input_customer_info(pl,flight_no,ticket);//录入客户信息
printf("\t\t订票成功\n");
printf("\t**************************\n");
printf("\t==>1.浏览订票信息\n");
printf("\t==>2.退出操作系统\n");
printf("\t*************************\n");
printf("请选择:");
scanf("%d",&choice);
if(choice==1)
scan_info(kehu);
pl=p->next1;
// printf("%s\n",pl->airno);
run=1;
}
else
{
printf("\t航班余票不足,请选择是否重订\n");
printf("\t**************************\n");
printf("\t==>\t1.重订\n\t==>\t2.退出\n");
printf("\t**************************\n");
printf("请选择:");
scanf("%d",&choice);
getchar();
if(choice==1)
{
pl=p;
kehu=book_airplane(pl,kehu);
}
pl=p;
}
}
pl=pl->next1;
} }
if(run!=1)
printf("对不起,暂时没有开通前往该地的航线!\n");
return kehu;
}


//查询航班信息
void search_airplane(struct airplane *pl)
{
int b;
// airplane a;
char destination[50];//目的地
char fly_num[30];//航班号
int i;
int flag=0;
struct airplane *p;
p=pl;
// printf("%s",pl->airno);
printf("\t\t查找航线信息\n");
printf("\t**************************\n");
printf("\t==>1.通过目的地查询\t==\n");
printf("\t==>2.通过航班号查询\t==\n");
printf("\t**************************\n");
printf("请选择:");
scanf("%d",&b);
getchar();
if(b==1)
{ printf("请输入所要查询航班的目的地:");
scanf("%s",destination);


// printf("%d",airnum);
for(i=0;i<airnum;i++)
{
pl=pl->next1;
if(strcmp(destination,pl->zhongdianzhan)==0)
{
printf("航班号\t起点站\t终点站\t起飞时间\t到达时间\t座位数\t余票\n");
printf("%-6s\t%-6s\t%-6s\t%-8s\t",pl->airno,pl->qidianzhan,pl->zhongdianzhan,pl->departure_time);
printf("%-8s\t%-6d\t%-4d\n",pl->landing_time,pl->seat,pl->standby_ticket);
flag=1;
}
//pl=pl->next1;
}
}
if(b==2)
{
printf("请输入所要查询航班的航班号:");
scanf("%s",fly_num);
//printf("航班信息:\n");
for(i=0;i<airnum;i++)
{
pl=pl->next1;
if(strcmp(fly_num,pl->airno)==0)
{
printf("航班号\t起点站\t终点站\t起飞时间\t到达时间\t座位数\t余票\n");
printf("%-6s\t%-6s\t%-6s\t%-8s\t",pl->airno,pl->qidianzhan,pl->zhongdianzhan,pl->departure_time);
printf("%-8s\t%-6d\t%-4d\n",pl->landing_time,pl->seat,pl->standby_ticket);
flag=1;
}
}
}
if(flag==0)
printf("对不起,此航线尚未开通!请重新选择:\n");
pl=p;
}


struct airplane * creat_airplane()//插入航班信息
{
struct airplane * h,* p,* q;
int b;
int i=0;
h=(struct airplane *)malloc(sizeof(struct airplane));
if(h==NULL)
{
printf("动态内存分配失败\n");
exit(0);
}
//输入头结点数据
h->airno[0]='\0';
h->qidianzhan[0]='\0';
h->departure_time[0]='\0';
h->landing_time[0]='\0';
h->zhongdianzhan[0]='\0';
h->seat=0;
h->standby_ticket=0;
h->next1=NULL;
q=h;//q指向链表链尾,此时为头结点


while(b<2)//循环创建结点,并链入链表中
{
p=(struct airplane *)malloc(sizeof(struct airplane));
if(p==NULL)
{
printf("动态内存分配失败\n");
exit(0);
}
/*输入新结点数据*/
printf("\t请输入下面几项内容:\n");
printf("航班号\n");
gets(p->airno);
printf("起点站\n");
gets(p->qidianzhan);
printf("终点站\n");
gets(p->zhongdianzhan);
printf("起飞时间,格式如:20110101 00:00\n");
gets(p->departure_time);
printf("到达时间,格式如:20110101 00:00\n");
gets(p->landing_time);
printf("座位数\n");
scanf("%d",&p->seat);
printf("余票数\n");
scanf("%d",&p->standby_ticket);
p->next1=NULL;
q->next1=p;
q=p;
printf("已输入%d个航班\n",++airnum);
printf("\t**************************\n");
printf("\t是否要输入下一条航线记录\n\t==>\t1.继续录入\n\t==>\t2.退出操作\n");
printf("\t**************************\n");
printf("请选择:");
scanf("%d",&b);
getchar();
}
return(h);//返回链表头指针
}


int main()
{
int a;
struct airplane *pl;
Linklist2 kehu=NULL;
while(1)
{
printf("\t 欢迎使用航空客运订票系统\n");
printf("\t**************************\n");
printf("\t=>1.\t录入功能\t==\n");
printf("\t=>2.\t查询功能\t==\n");
printf("\t=>3.\t订票功能\t==\n");
printf("\t=>4.\t退票功能\t==\n");
printf("\t=>5.\t修改功能\t==\n");
printf("\t=>6.\t添加航班\t==\n");
printf("\t=>7.\t查询订票信息\t==\n");
printf("\t=>8.\t打印航班信息\t==\n");
printf("\t=>9.\t退出系统\t==\n");
printf("\t**************************\n");
printf("请选择:");
scanf("%d",&a);
getchar();


if(a==1)
pl=creat_airplane();
if(a==2)
search_airplane(pl);
if(a==3)
kehu=book_airplane(pl,kehu);
if(a==4)
cancel_airplane(pl,kehu);
if(a==5)
change_airplane(pl/*,kehu*/);
if(a==6)
insert_airplane(pl);
if(a==7)
scan_info(kehu);
if(a==8)
print(pl);
if(a==9)
exit(0);
}
}

本文标签: C语言