① 求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();
}
代碼全給你了,課設自己寫,呵呵