運行效果如下:
QQ截圖20171116214717.png (26.22 KB, 下載次數: 110)
下載附件
2017-11-16 21:48 上傳
單片機源程序如下:
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- //#include <conio.h>
- #define MAXSIZE 10
- typedef struct wat_ros
- {
- char name[10];
- int req_amt;
- struct wat_ros *next;
- }qnode,*qptr;
- typedef struct pqueue
- {
- qptr front;
- qptr rear;
- }linkqueue;
- typedef struct ord_ros
- {
- char name[10];
- int ord_amt;
- int grade;
- struct ord_ros *next;
- }linklist;
- struct airline
- {
- char ter_name[10]; /* 站點名 */
- char air_num[10]; /* 航班號 */
- char plane_num[10]; /* 飛機號 */
- char date[7]; /* 飛行日期 */
- int tkt_amt; /* 乘員定額 */
- int tkt_sur; /* 余票量 */
- linklist *order;
- linkqueue wait;
- }lineinfo;
- struct airline *start;
- struct airline air[MAXSIZE]={
- {"北京", "1", "S1001", "星期一", 130, 10},
- {"上海","2","S1002","星期二",120,5},
- {"廈門","3","S1003","星期三",157,15},
- {"廣州","4","S1004","星期四",146,5},
- {"海口","5","S1005","星期四",173,15},
- {"深圳","6","S1006","星期五",120,35},
- {"長沙","7","S1007","星期四",145,65},
- {"成都","8","S1008","星期六",125,53},
- {"杭州","9","S1009","星期四",146,24},
- {"重慶","10","S1010","星期五",100,30}
- };;
- void display(struct airline *info)
- {
- printf("%8s\t%3s\t%s\t%4s\t\%d\t\%d\t\n",info->ter_name,info->air_num,info->plane_num,info->date,info->tkt_amt,info->tkt_sur);
- }
- /* 瀏覽航線信息函數 */
- void list()
- {
- struct airline *info;
- int i = 0;
- info = start;
- printf("終點站名\t航班號\t飛機號\t飛行日期" "乘員定額" "余票量\n");
- while(i < MAXSIZE){
- display(info);
- info++;
- i++;
- }
- printf("\n\n");
- }
- void search()
- {
- struct airline *info,*find();
- char name[10];
- int i=0;
- info=start;
- printf("請輸入終點站名:");
- scanf("%s",name);
- while(i<MAXSIZE)
- {
- if(!strcmp(name,info->ter_name)) break;
- info++;
- i++;
- }
- if(i>=MAXSIZE)
- printf("對不起,該航線未找到!\n");
- else
- {
- printf("終點站名\t航班號\t飛機號\t飛行日期\t乘員定額\t余票量\n");
- display(info);
- }
- }
- struct airline *find()
- {
- struct airline *info;
- char number[10];
- int i=0;
- info=start;
- printf("請輸入航班號:");
- scanf("%s",number);
- while(i<MAXSIZE)
- {
- if(!strcmp(number,info->air_num)) return info;
- info++;
- i++;
- }
- printf("對不起,該航線末找到!\n");
- return NULL;
- }
- /* 瀏覽已訂票客戶信息 */
- void prtlink()
- {
- linklist *p;
- struct airline *info;
- info=find();
- p=info->order;
- if(p!=NULL){
- printf("客戶姓名 訂票數額 艙位等級\n");
- while(p){
- printf("%s\t\t%d\t%d\n",p->name,p->ord_amt,p->grade);
- p=p->next;
- }
- }
- else
- printf("該航線沒有客戶信息!!\n");
- }
- linklist *insertlink(linklist *head,int amount,char name[],int grade)
- { linklist *p1,*new1;
- p1=head;
- new1=(linklist *)malloc(sizeof(linklist));
- if(!new1) {printf("\nOut of memory!!\n");return NULL;}
- strcpy(new1->name,name);
- new1->ord_amt=amount;
- new1->grade=grade;
- new1->next=NULL;
- if(head==NULL)
- {head=new1;new1->next=NULL;}
- else
- head=new1;
- new1->next=p1;
- return head;
- }
- linkqueue appendqueue(linkqueue q,char name[],int amount)
- { qptr new1;
- new1=(qptr)malloc(sizeof(qnode));
- strcpy(new1->name,name);
- new1->req_amt=amount;
- new1->next=NULL;
- if(q.front==NULL)
- q.front=new1;
- else
- q.rear->next=new1;
- q.rear=new1;
- return q;
- }
- void order()
- { struct airline *info;
- int amount,grade;
- char name[10];
- info=start;
- if(!(info=find())) return;
- printf("請輸入訂飛機票的數量:");
- scanf("%d",&amount);
- if(amount>info->tkt_amt)
- { printf("\n對不起,您輸入的數量已經超額!");
- return;
- }
- if(amount<=info->tkt_sur)
- {
- int i;
- printf("請輸入您的姓名(訂票客戶):");
- scanf("%s",name);
- printf("請輸入%s票的艙位等級:",name);
- scanf("%d",&grade);
- info->order=insertlink(info->order,amount,name,grade);
- for(i=0;i<amount;i++)
- printf("%s的座位號是:%d\n",name,info->tkt_amt-info->tkt_sur+i+1);
- info->tkt_sur-=amount;
- printf("\n祝您旅途愉快!\n");
- }
- else
- { char r;
- printf("\n已經沒有更多的票,您需要登記嗎?(Y/N)");
- //r=getch();
- //printf("%c",r);
- scanf ( "回車繼續等待%c", &r );
- if(r=='Y'||r=='y')
- { printf("\n請輸入您的姓名(排隊訂票客戶):");
- scanf("%s",name);
- info->wait=appendqueue(info->wait,name,amount);
- printf("\n注冊成功!\n");
- }
- else printf("\n歡迎您再次乘坐!\n");
- }
- }
- void return_tkt()
- { struct airline *info;
- qnode *t,*back,*f,*r;
- int grade;
- linklist *p1,*p2,*head;
- char cusname[10];
- if(!(info=find())) return;
- head=info->order;
- p1=head;
- printf("請輸入你的姓名(退票客戶):");
- scanf("%s",cusname);
- while(p1!=NULL) {
- if(!strcmp(cusname,p1->name)) break;
- p2=p1;p1=p1->next;
- }
- if(p1==NULL){ printf("對不起,您沒有訂過票!\n");return;}
- else
- {
- if(p1==head) head=p1->next;
- else p2->next=p1->next;
- info->tkt_sur+=p1->ord_amt;
- grade=p1->grade;
- printf("%s退票成功!\n",p1->name);
- free(p1);
- }
- info->order=head;
- f=(info->wait).front;
- r=(info->wait).rear;
- t=f;
- while(t)
- {
- if((info->tkt_sur = info->wait.front->req_amt))
- {
- int i;
- info->wait.front=t->next;
- printf("%s訂票成功!\n",t->name);
- for(i=0;i<t->req_amt;i++)
- printf("%s的座位號是:%d\n",t->name,(info->tkt_sur)-i);
- info->tkt_sur-=t->req_amt;
- info->order=insertlink(info->order,t->req_amt,t->name,grade);
- free(t);
- break;
- }
- back=t;t=t->next;
- if((info->tkt_sur)>=(t->req_amt)&&t!=NULL)
- { int i;
- back->next=t->next;
- printf("%s訂票成功!\n",t->name);
- for(i=0;i<t->req_amt;i++)
- printf("<%s>'s seat number is:%d\n",t->name,(info->tkt_sur)-i);
- info->tkt_sur-=t->req_amt;
- info->order=insertlink(info->order,t->req_amt,t->name,grade);
- free(t);break;
- }
- if(f==r) break;
- }
- }
- void sort_tkt()
- {
- int j;
- struct airline t,*info,*p,*q;
- p=info=air;
- for(p=info;(p+1)->tkt_sur;p++)
- for(q=p+1;q->tkt_sur;q++)
- {
- if(p->tkt_sur<q->tkt_sur)
- {
- t=*p;
- *p=*q;
- *q=t;
- }
- }
- printf("終點站名\t航班號\t飛機號\t飛行日期\t乘員定額\t余票量\n");
- for(j=0;j<MAXSIZE;j++)
- {
- display(info);
- info++;
- }
- }
- int menu_select()
- {
- int c;
- char s[20];
- printf("\n\t\t 飛訂票系統(信息來自 12306官網)\n");
- printf(" ******************************************\n");
- printf(" ** 1.瀏覽航線信息 **\n");
- printf(" ** 2.查詢航班 **\n");
- printf(" ** 3.瀏覽已訂票客戶信息 **\n");
- printf(" ** 4.辦理訂票業務 **\n");
- printf(" ** 5.辦理退票業務 **\n");
- printf(" ** 6.查看剩余票數 **\n");
- printf(" ** 0.退出系統 **\n");
- printf(" *******************************************\n");
- do
- {
- printf("請選擇:");
- scanf("%s",s);
- c = atoi(s); /* atoi將字符串轉換為整型 */
- }while(c < 0||c > 7);
- return c;
- }
- int main()
- {
- start = air;
- for(;;)
- {
- switch(menu_select())
- {
- case 1:list();break;
- case 2:prtlink();break;
- case 3:search();break;
- case 4:order();break;
- case 5:return_tkt();break;
- case 6:sort_tkt();break;
- case 0:printf("\n歡迎使用12306訂票系統,再會!\n");exit(0);
- }
- printf("\nPress any key to continue!\n");
- getchar();
- }
- return 0;
- }
復制代碼
|