① 求c语言编程(航班信息查询与检索)
#include "stdio.h"
#include "malloc.h"
#include "string.h"
#define MAXN 100
typedef struct{
char hour[MAXN];
char minute[MAXN];
} time;
typedef struct node
{
char number[MAXN]; //航班号
time start; //起飞时间
time end; //到达时间
char start_station[MAXN]; //起点站
char end_station[MAXN]; //终点站
char type[MAXN]; //飞机型号
char price[MAXN]; //票价
struct node*link;
}NODE;
NODE *create_link_list(int n)
{
int i;
NODE *head,*p,*q;
if(n==0)return(NULL);
head=(NODE*)malloc(sizeof(NODE));
for(i=0;i <MAXN;i++)
{
head->number[i]='\0';
head->start.hour[i]='\0';
head->start.minute[i]='\0';
head->end.hour[i]='\0';
head->end.minute[i]='\0';
head->start_station[i]='\0';
head->end_station[i]='\0';
head->type[i]='\0';
head->price[i]='\0';
}
p=head;
for(i=1;i <n;i++)
{
printf("请输入航班号:");
scanf("%s",&(p->number));
printf("请输入起飞时间(时 分):");
scanf("%s %s",&(p->start.hour),&(p->start.minute));
printf("请输入达到时间(时 分):");
scanf("%s %s",&(p->end.hour),&(p->end.minute));
printf("请输入起点站 终点站:");
scanf("%s %s",&(p->start_station),&(p->end_station));
printf("请输入飞机型号:");
scanf("%s",&(p->type));
printf("请输入票价:");
scanf("%s",&(p->price));
printf("\n");
q=(NODE*)malloc(sizeof(NODE));
p->link=q;
p=q;
}
printf("请输入航班号:");
scanf("%s",&(p->number));
printf("请输入起飞时间(时 分):");
scanf("%s %s",&(p->start.hour),&(p->start.minute));
printf("请输入达到时间(时 分):");
scanf("%s %s",&(p->end.hour),&(p->end.minute));
printf("请输入起点站 终点站:");
scanf("%s %s",&(p->start_station),&(p->end_station));
printf("请输入飞机型号:");
scanf("%s",&(p->type));
printf("请输入票价:");
scanf("%s",&(p->price));
printf("\n");
getchar();
p->link=NULL;
return(head);
}
void insert(NODE **p_head,NODE *q)
{
NODE *p;
if(*p_head==NULL)
*p_head=q;
else
{
p=*p_head;
while(p!=NULL&&p->link!=NULL)
p=p->link;
p->link=q;
}
}
unsigned int countit(NODE* n)//计算链表长度
{
unsigned int counti = 0;
while(n!=NULL)
counti++,n=n->link;
return counti;
}
NODE* getindex(NODE* head, int num);
NODE* getindex(NODE* head, int num)//取得index为num 的节点!
{
if(num<0 || num>countit(head))
return NULL;
NODE* rn = head;
while(--num>0)
rn = rn->link;
return rn;
}
int binSearch(NODE* n,char *strinput,int low, int high)// 二分查找
{
int i;
int middle = (high+low)/2;
if (high < low)
return 0;
if ((i=strcmp(strinput, n->number)) <0)
high= middle;
else if (i > 0)
low = middle;
else
{
i = middle;
return i;
}
binSearch(getindex(n,middle),strinput,low,high);
}
int bisect(char a[],int n,char s[MAXN])//二分查找
{
int i,j,m;
i=0;
j=n-1;
while(i <=j)
{
m=(i+j)/2;
}
return(-1);
}
NODE *search1(NODE *head,char v[MAXN])//起点站顺序查找
{
for(;head!=NULL&&strcmp(head->start_station,&v[0]);head=head->link);
return(head);
}
NODE *search2(NODE *head,char w[MAXN])//到达站顺序查找
{
for(;head!=NULL&&strcmp(head->end_station,&w[0]);head=head->link);
return(head);
}
NODE *search3(NODE *head,char x[MAXN],char y[MAXN])//起飞时间顺序查找
{
for(;head!=NULL&&(strcmp(head->start.hour,&x[0]) || strcmp(head->start.minute,&y[0]));head=head->link);
return(head);
}
NODE *search4(NODE *head,char t[MAXN],char u[MAXN])//到达时间顺序查找
{
for(;head!=NULL&&(strcmp(head->end.hour,&t[0]) || strcmp(head->end.minute,&u[0]));head=head->link);
return(head);
}
void output(NODE *p)
{
while(p!=NULL)
{
printf("航班信息:\n");
printf("航班号:%s\n",p->number);
printf("起飞时间:%s点%s分,",p->start.hour,p->start.minute);
printf("到达时间:%s点%s分\n",p->end.hour,p->end.minute);
printf("起点站:%s,",p->start_station);
printf("到达站:%s\n",p->end_station);
printf("飞机型号:%s ",p->type);
printf("票价:%s元\n\n",p->price);
p=p->link;
}
}
NODE *rank( NODE *head)
{
NODE *q=0,*p=0,*t,*h1;
h1=head->link;
head->link=NULL;
while(h1!=NULL)
{
t=h1;
h1=h1->link;
p=head;
q=head;
while( p!=NULL && strcmp(t->number, p->number)>0 )
{
q=p;
p=p->link;
}
if(q == p)
{
t->link=p;
head=t;
}
else
{
t->link=p;
q->link=t;
}
}
return head;
}
int main(int argc, char* argv[])
{
NODE *p,*q,*r;
int a,b,i,j,n;
int count=0;
char o[MAXN];
char s[MAXN];
char v[MAXN];
char w[MAXN];
char x[MAXN];
char y[MAXN];
char t[MAXN];
char u[MAXN];
for(i=0;i <MAXN;i++)
{
o[i]='\0';
s[i]='\0';
v[i]='\0';
w[i]='\0';
x[i]='\0';
y[i]='\0';
t[i]='\0';
u[i]='\0';
}
while(true)
{
printf("【航班信息的查询与检索】\n");
printf("★*******************************★\n");
printf(" 1.建立航班信息\n");
printf(" 2.插入航班信息\n");
printf(" 3.按航班号进行排序 \n");
printf(" 4.航班信息查询\n");
printf(" 5.显示航班信息\n");
printf(" 6.退出本系统\n");
printf("★*******************************★\n");
scanf("%d",&a);
getchar();
switch(a)
{
case 1:
printf("请输入你所要建立的航班信息个数:");
scanf("%d",&n);
p=create_link_list(n);
break;
case 2:
q=create_link_list(1);
insert(&p,q);
break;
case 3:
p = rank(p);
break;
case 4:
printf("\n1、按照航班号查询.\n");
printf("2、按照起点站查询.\n");
printf("3、按照到达站查询.\n");
printf("4、按照起飞时间查询.\n");
printf("5、按照到达时间查询.\n");
scanf("%d",&b);
getchar();
switch(b)
{
case 1:
p=rank(p);
printf("请输入您所要找的航班号:");
scanf("%s",s);
if( binSearch(p,s,1, countit(p)) )
printf("scuess!\n");
break;
case 2:
printf("请输入起点站");
scanf("%s",&v[MAXN]);
if(search1(p,&v[MAXN])!=NULL)
{
printf("查询成功!\n");
r=search1(p,&v[MAXN]);
output(r);
}
else
printf("查询失败,该信息录中没有该起点站!\n");
break;
case 3:
printf("请输入到达站");
scanf("%s",&w[MAXN]);
if(search2(p,&w[MAXN])!=NULL)
{
printf("查询成功!\n");
r=search2(p,&w[MAXN]);
output(r);
}
else
printf("查询失败,该信息录中没有该到达站!\n");
break;
case 4:
printf("请输入起飞时间(时 分)");
scanf("%s %s",&x[MAXN],&y[MAXN]);
if(search3(p,&x[MAXN],&y[MAXN])!=NULL)
{
printf("查询成功!\n");
r=search3(p,&x[MAXN],&y[MAXN]);
output(r);
}
else
printf("查询失败,该信息录中没有该到达站!\n");
break;
case 5:
printf("请输入到达时间");
scanf("%s %s",&t[MAXN],&u[MAXN]);
if(search4(p,&t[MAXN],&u[MAXN])!=NULL)
{
printf("查询成功!\n");
r=search4(p,&t[MAXN],&u[MAXN]);
output(r);
}
else
printf("查询失败,该信息录中没有该到达站!\n");
break;
}
break;
case 5:
output(p);
printf("\n");
break;
case 6:
return(0);
}
}
return(0);
}
② 请用c语言用结构体或者二维数组完成航班信息管理系统
需要分析:
A.车寻航线:
1.根据旅客提出的起点站,终点站名输出下列信息:航班号,票价,折扣,最多载客量,是否满载,起飞时间,降落时间和飞行时间;
2.根据订票乘客的姓名可以查询所订航班的航班号,座位号,飞行日期等信息;
3.根据航班号查询航班的起点站,中转站,终点站名,票价,折扣,最多载客量,是否满载,起飞时间,降落时间和飞行时间;
B.承办客户提出的要求(航班号、订票数额)查询该航班票额情况,若有余票,则为客户办理订票手续,输出座位号,需付款项信息;若已满员或余票额少于盯票额,则需重新询问客户要求。若需要,可登记排队候补;
C.根据客户提供的情况(日期、航班),为客户办理退票手续。(然后查询该航班是否有人排队候补,首先询问排第一的客户,若所退票额所能满足他的要求,则为他办理订票手续,否则依次询问其他排队候补客户);
E.内部人员对航班情况的控制:
可以录入航班信息,删除航班信息,修改航班信息,查看基本航班信息。
概要设计:
因为每个客户名单或查询名单都包括多个数据域,这样就需要有一个能存储多个数据域的数据类型来存储,因此采用单链表类型。由于航线的信息是固定的,可选用结构体数组,又因为订票与预约人数无法预计,可选用链表存储信息。
线性表的单链表存储结构:typedef struct LNode{
ElemType;
Struct Lnode*next;}LNode,*LinkList;
a.抽象数据类型顺序表的定义如下:
ADT SqList{
数据对象:D={ai|ai∈数据类型,i=1,2,3...,n}
数据关系:R1={<ai-1,ai>|ai-1,ai∈D,i=1,2,3...,n}
基本操作:
InitList_Sq(&L)
操作结果:创建空的顺序表。
CreatList_Sq(&L)
操作结果:建立顺序表。
}ADT SqList
b.抽象数据类型单链表的定义如下:
ADT LinkList{
数据对象:D={ai|ai∈结构类型,i=1,2,3...,n,n>0}
数据关系:R1={<ai-1,ai>|ai-1,ai∈D,i=1,2,3...,n}
基本操作:
InitList_L(&L)
操作结果:创建空的顺序表。
}ADT LinkList
在main()里调用各个函数
2.主程序
void main(){
初始化;
do{
接受命令;
处理命令;
}while(“命令”!=“退出”);
}
3.程序的模块调用:
三.详细设计:
1.所有数据类型:
struct plan /*航班数据*/
{
③ C语言编写一个简单的航空管理系统
需要分析:
A.车寻航线:
1.根据旅客提出的起点站,终点站名输出下列信息:航班号,票价,折扣,最多载客量,是否满载,起飞时间,降落时间和飞行时间;
2.根据订票乘客的姓名可以查询所订航班的航班号,座位号,飞行日期等信息;
3.根据航班号查询航班的起点站,中转站,终点站名,票价,折扣,最多载客量,是否满载,起飞时间,降落时间和飞行时间;
B.承办客户提出的要求(航班号、订票数额)查询该航班票额情况,若有余票,则为客户办理订票手续,输出座位号,需付款项信息;若已满员或余票额少于盯票额,则需重新询问客户要求。若需要,可登记排队候补;
C.根据客户提供的情况(日期、航班),为客户办理退票手续。(然后查询该航班是否有人排队候补,首先询问排第一的客户,若所退票额所能满足他的要求,则为他办理订票手续,否则依次询问其他排队候补客户);
E.内部人员对航班情况的控制:
可以录入航班信息,删除航班信息,修改航班信息,查看基本航班信息。
概要设计:
因为每个客户名单或查询名单都包括多个数据域,这样就需要有一个能存储多个数据域的数据类型来存储,因此采用单链表类型。由于航线的信息是固定的,可选用结构体数组,又因为订票与预约人数无法预计,可选用链表存储信息。
线性表的单链表存储结构:typedef struct LNode{
ElemType;
Struct Lnode*next;}LNode,*LinkList;
a.抽象数据类型顺序表的定义如下:
ADT SqList{
数据对象:D={ai|ai∈数据类型,i=1,2,3...,n}
数据关系:R1={<ai-1,ai>|ai-1,ai∈D,i=1,2,3...,n}
基本操作:
InitList_Sq(&L)
操作结果:创建空的顺序表。
CreatList_Sq(&L)
操作结果:建立顺序表。
}ADT SqList
b.抽象数据类型单链表的定义如下:
ADT LinkList{
数据对象:D={ai|ai∈结构类型,i=1,2,3...,n,n>0}
数据关系:R1={<ai-1,ai>|ai-1,ai∈D,i=1,2,3...,n}
基本操作:
InitList_L(&L)
操作结果:创建空的顺序表。
}ADT LinkList
在main()里调用各个函数
2.主程序
void main(){
初始化;
do{
接受命令;
处理命令;
}while(“命令”!=“退出”);
}
3.程序的模块调用:
三.详细设计:
1.所有数据类型:
struct plan /*航班数据*/
{
char num[5];/*航班号码*/
char city[10];/*到达城市*/
char up[8];/*航班起飞时间*/
char down[8];/*航班到达时间*/
int pric ;/*航班价格*/
int rshu ;/*人数*/
int zheg[4];/*价格折扣*/
}
;
struct man/*定票人数据*/
{
char num[10];/*身份证号码*/
char nam[10];/*姓名*/
int demand ;/*定票数量*/
}
;
typedef struct node/*航班数据结点*/
{
struct plan data ;
struct node*next ;
}
Node,*Link ;
typedef struct people/*乘客数据结点*/
{
struct man data ;
struct people*next ;
}
peo,*LIN ;
2.程序所用函数:
void print()/*界面输出*/
{
printf("============================System of book ticket===============================\n");
printf("\n");
printf("\t***********************************************************\n");
printf("\t*\t1---Bookticket \t2---Dishonorbill *\n");
printf("\t*\t3---Adding flight\t4---Adding flight *\n");
printf("\t*\t5---Modify \t6---Advice *\n");
printf("\t*\t7---Save \t8---exit *\n");
printf("\t##########################################################\n");
}
添加航班模块
void add(Link l)/*添加航班数据*/
{
Node*p,*r,*s ;
char num[10];
r=l ;
s=l->next ;
while(r->next!=NULL)
r=r->next ;
while(1)
{
printf("please input the number of the plan(0-return)");/*输入0就返回*/
scanf("%s",num);
if(strcmp(num,"0")==0)
break ;
while(s)
{
if(strcmp(s->data.num,num)==0)
{
printf("=====tip:the number'%s'has been born!\n",num);
return ;
}
s=s->next ;
}
p=(Node*)malloc(sizeof(Node));/*航班数据输入*/
strcpy(p->data.num,num);
printf("Input the city where the plan will reach:");/*飞机到达地城市*/
scanf("%s",p->data.city);
getchar();
printf("Input the time which the plan take off:");/*起飞时间*/
scanf("%s",p->data.up);
getchar();
printf("Input the time which the plan reach:");/*降落时间*/
scanf("%s",&p->data.down);
getchar();
printf("Input the price of ticket:$");/*机票价格*/
scanf("%d",&p->data.pric);
getchar();
printf("Input the number of people who have booked ticket:");/*定票数量*/
scanf("%d",&p->data.rshu);
getchar();
printf("Input the agio of the ticket:");
scanf("%s",&p->data.zheg);
getchar();
p->next=NULL ;
r->next=p ;
r=p ;
shoudsave=1 ;
}
}
输出模块
void pri(Node*p)/*输出函数*/
{
printf("\n\t\t\tThe following is the record you want:\n");
printf("\nnumber of plan: %s",p->data.num);
printf("\ncity the plan will reach: %s",p->data.city);
printf("\nthe time the plan take off: %s\nthe time the plan reach: %s",p->data.up,p->data.down);
printf("\nthe price of the ticket: %d",p->data.pric);
printf("\nthe number of people who have booked ticket: %d",p->data.rshu);
printf("\nthe agio of the ticket:%s",p->data.zheg);
}
退出函数模块
Node*Locate1(Link l,char findmess[],char numorcity[])
{
Node*r ;
if(strcmp(numorcity,"num")==0)
{
r=l->next ;
while(r)
{
if(strcmp(r->data.num,findmess)==0)
return r ;
r=r->next ;
}
}
else if(strcmp(numorcity,"city")==0)
{
r=l->next ;
while(r)
{
if(strcmp(r->data.city,findmess)==0)
return r ;
r=r->next ;
}
}
return 0 ;
}
航班信息模块
void qur(Link l)/*航班信息查询*/
{
Node*p ;
int sel ;
char str1[5],str2[10];
if(!l->next)
{
printf("TIP:there are not any record to be inquired for you!");
return ;
}
printf("Choose the way:(1->according to the number of plan;2->according to the city):");/*选择航班号查询和终点城市查询*/
scanf("%d",&sel);
if(sel==1)
{
printf("Input the the number of plan:");
scanf("%s",str1);
p=Locate1(l,str1,"num");
if(p)
{
printf("the following is what you want:\n");
pri(p);
}
else
{
mark1=1 ;
printf("\nthe file can't be found!");
}
}
else if(sel==2)
{
printf("Input the city:");
scanf("%s",str2);
p=Locate1(l,str2,"city");
if(p)
{
printf("the following is what you want:\n");
pri(p);
}
else
{
mark1=1 ;
printf("\nthe file can't be found!");
}
}
}
定票模块
void buy(Link l,LIN k)/*定票函数*/
{
Node*r[10],*p ;
int ch,dem ;
peo*v,*h ;
int i=0,t=0 ;
char str[10],str1[10],str2[10];
v=k ;
while(v->next!=NULL)
v=v->next ;
printf("Input the city you want to go: ");/*航班终点站城市*/
scanf("%s",&str);
p=l->next ;
while(p!=NULL)
{
if(strcmp(p->data.city,str)==0)
{
r[i]=p ;
i++;
}
p=p->next ;
}
printf("\n\nthe number of record have %d\n",i);
for(t=0;t<i;t++)
pri(r[t]);
if(i==0)
printf("\n\tSorry!Can't find the plan for you!\n");
else
{
printf("\ndo you want to book it?<1/0>\n");
printf("please choose: ");
scanf("%d",&ch);
if(ch==1)
{
h=(peo*)malloc(sizeof(peo));/*重新分配空间*/
printf("Input your name: ");
scanf("%s",&str1);
strcpy(h->data.nam,str1);
printf("Input your id: ");
scanf("%s",&str2);
strcpy(h->data.num,str2);
printf("Input your demand: ");
scanf("%d",&dem);
h->data.demand=dem ;
h->next=NULL ;
v->next=h ;
v=h ;
printf("\n\tLucky!Success in booking ticket!");
getch();
shoudsave=1 ;
}
}
}
peo*Locate2(LIN k,char findmess[])
{
peo*r ;
r=k->next ;
while(r)
{
if(strcmp(r->data.num,findmess)==0)
{
mark=1 ;
return r ;
}
r=r->next ;
}
return 0 ;
}
退票模块
void tui(LIN k)/*退票函数*/
{
char str[10];
peo*p,*r ;
int ch2=0 ;
printf("Input your id: ");/*输入身份证号*/
scanf("%s",&str);
p=Locate2(k,str);
if(mark!=1)
printf("can't find the people!");
else if(mark==1)
{
mark=0 ;
printf("\t\t\tthe following is the record you want:\n");
printf("your id:%s\n",p->data.num);
printf("name:%s\n",p->data.nam);
printf("your denmand:%d",p->data.demand);
printf("\ndo you want to refund the ticket?<1/0>");
scanf("%d",&ch2);
if(ch2==1)
{
if(p)
{
r=k ;
while(r->next!=p)
r=r->next ;
r->next=p->next ;
free(p);
}
count2--;
printf("\nyou have sucessed in refunding ticket!");
shoudsave=1 ;
}
}
}
void Modify(Link l)/*修改航班信息*/
{
Node*p ;
char findmess[20],ch ;
if(!l->next)
{
printf("\n=====tip:there isn't record for you to modify!\n");
return ;
}
else
{
qur(l);
if(mark1==0)
{
printf("\nDo you want to modify it?\n");
getchar();
scanf("%c",&ch);
if(ch=='y');
{
printf("\nInput the number of the plan:");
scanf("%s",findmess);
p=Locate1(l,findmess,"num");
if(p)
{
printf("Input another number of plan:");
scanf("%s",&p->data.num);
getchar();
printf("Input another city the plan will reach:");
scanf("%s",&p->data.city);
getchar();
printf("Input another time the plan take off");
scanf("%s",&p->data.up);
printf("Input another time the plan reach:");
scanf("%s",&p->data.down);
printf("Input another price of the ticket::");
scanf("%d",&p->data.pric);
printf("Input another number of people who have booked ticket:");
scanf("%d",&p->data.rshu);
printf("Input another agio of the ticket:");
scanf("%s",&p->data.zheg);
printf("\n=====>tip:modifying record is sucessful!\n");
shoudsave=1 ;
}
else
printf("\tcan't find the flight!");
}
}
else
mark1=0 ;
}
}
void advice(Link l)/*终点站航班查询*/
{
Node*r ;
char str[10];
int mar=0 ;
r=l->next ;
printf("Iuput the city you want to go: ");/*输入终点站城市*/
scanf("%s",str);
while(r)
{
if(strcmp(r->data.city,str)==0&&r->data.rshu<200)
{
mar=1 ;
printf("\nyou can select the following plan!\n");
printf("\n\nplease select the fourth operation to book the ticket!\n");
pri(r);
}
r=r->next ;
}
if(mar==0)
printf("\n\t\t\tyou can't book any ticket now!\n");
}
void save1(Link l)/*保存数据*/
{
FILE*fp ;
Node*p ;
int count=0,flag=1 ;
fp=fopen("g:\\data1","wb");
if(fp==NULL)
{
printf("the file can't be opened!");
return ;
}
p=l->next ;
while(p)
{
if(fwrite(p,sizeof(Node),1,fp)==1)
{
p=p->next ;
count++;
}
else
{
flag=0 ;
break ;
}
}
if(flag)
{
printf("the number of the record which have been saved is %d\n",count);
shoudsave=0 ;
}
fclose(fp);
}
void save2(LIN k) /*保存数据*/
{
FILE*fp ;
peo*p ;
int count=0,flag=1 ;
fp=fopen("g:\\data2","wb");/*文件连接*/
if(fp==NULL)
{
printf("the file can't be opened!");
return ;
}
p=k->next ;
while(p)
{
if(fwrite(p,sizeof(peo),1,fp)==1)
{
p=p->next ;
count++;
}
else
{
flag=0 ;
break ;
}
}
if(flag)
{
printf("the number of the record which have been saved is %d\n",count);
shoudsave=0 ;
}
fclose(fp);
}
四.主函数模块:
main()
{
FILE*fp1,*fp2 ;
Node*p,*r ;
char ch1,ch2 ;
Link l ;
LIN k ;
peo*t,*h ;
int sel ;
l=(Node*)malloc(sizeof(Node));
l->next=NULL ;
r=l ;
k=(peo*)malloc(sizeof(peo));
k->next=NULL ;
h=k ;
fp1=fopen("g:\\data1","ab+");
if((fp1==NULL))
{
printf("can't open the file!");
return 0 ;
}
while(!feof(fp1))
{
p=(Node*)malloc(sizeof(Node));
if(fread(p,sizeof(Node),1,fp1)==1)
{
p->next=NULL ;
r->next=p ;
r=p ;
count1++;
}
}
fclose(fp1);
fp2=fopen("g:\\data2","ab+");
if((fp2==NULL))
{
printf("can't open the file!");
return 0 ;
}
while(!feof(fp2))
{
t=(peo*)malloc(sizeof(peo));
if(fread(t,sizeof(peo),1,fp2)==1)
{
t->next=NULL ;
h->next=t ;
h=t ;
count2++;
}
}
fclose(fp2);
while(1)
{
getch();
clrscr();
print();
printf("please choose the operation(1-8): ");
scanf("%d",&sel);
if(sel==8)
{
if(shoudsave==1)
{
getchar();
printf("\n=====tip:the file have been changed!do you want to save it(y/n)?\n");
scanf("%c",&ch1);
if(ch1=='y'||ch1=='Y')
{
save2(k);
save1(l);
}
}
printf("\n\tyou have exited! Happy serve for you");
break ;
}
switch(sel)
{
case 1 :
buy(l,k);
break ;
case 2 :
tui(k);
break ;
case 3 :
qur(l);
break ;
case 4 :
add(l);
break ;
case 5 :
Modify(l);
break ;
case 6 :
advice(l);
break ;
case 7 :
{
save1(l);
save2(k);
break ;
}
case 8 :
exit(0);
}
}
getch();
}
④ C语言程序设计 编写程序计算飞机票款.输入舱位代码和购票数量,输出总票款
#include<stdio.h>
intmain(void)
{
intn;
floatf,c,y,i;
charg;
printf("请输入F舱、C舱和Y舱的公布价; ");
scanf("%f%f%f",&f,&c,&y);
getchar();
printf("请输入仓位代码和购票数量; ");
scanf("%c%d",&g,&n);
switch(g)
{
case'B':i=y*0.9*n;break;
case'H':i=y*0.85*n;break;
case'K':i=y*0.8*n;break;
case'L':i=y*0.75*n;break;
case'M':i=y*0.7*n;break;
case'N':i=y*0.65*n;break;
case'Q':i=y*0.6*n;break;
case'T':i=y*0.55*n;break;
case'X':i=y*0.5*n;break;
}
printf("%.2f ",i);
}
⑤ 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->destination,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 ,ch);
}
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);
return 1;
}
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);
return 1;
}
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->destination ,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();
}
代码全给你了,课设自己写,呵呵