航空客票系统,退票有点问题,请求大家帮助。
程序代码:
#include<stdio.h> #include<stdlib.h> #include<string.h> #define OK 1 #define ERROR 0 typedef struct customer{ //客户结构(链表); char name[9]; int n; struct customer * next; } customer, * cus; typedef struct WaitType{ char name[9]; int need; //所需票量; struct WaitType * next; } WaitType, * wt; typedef struct wait{ //等候的客户队列; wt front; wt rear; }wait; typedef struct airline{ //航班(某架飞机)结构; char air_num[8]; //飞机航线名长度为8; char plane_num[8]; char end_place[20]; //飞机抵达城市 ,长度限制为20; int total; int left; //余票数; int day; //飞行周日; customer * cusptr; //已定票客户链表; wait w; //等待客户链表; struct airline * next; }airline, * air; airline * creat_air(){ //创建航班链表,返回航班机构类型指针; airline * a; //航班头结点; a = (airline *)malloc(sizeof(airline)); if(a==NULL) printf("空间不足"); return a; } customer * creat_cus(){ //创建客户链表; customer * c; //乘客头结点; c = (customer *)malloc(sizeof(customer)); c->next = NULL; if(c==NULL) printf("空间不足"); return c; } wait creat_wait(){ //创建等待链表; wait w; w.front = w.rear = (wt)malloc(sizeof(WaitType)); if(w.front==NULL) printf("空间不足"); return w; } int modefy_airline(air &p, int n){ //修改航班的空余座位信息(用于退票); p->left += n; return OK; } int insert_air(air &p, char * air_num, char * plane_num, char * end_place, int total, int left, int day, cus c, wait w){ //增加航班信息; airline * q; q = (airline *)malloc(sizeof(airline)); strcpy(q->air_num,air_num); //字符串复制; strcpy(q->plane_num, plane_num); strcpy(q->end_place, end_place); q->total= total; q->left = left; q->day = day; //q->cusptr = (customer *)malloc(sizeof(customer)); //q->w.front = q->w.rear = (wt)malloc(sizeof(WaitType)); q->next = NULL; q->cusptr = c->next; q->w = w; p->next = q; p = p->next; //指向尾节点; return OK; } int insert_cus(air &p, char * name,int n){ //增加某航班的客户信息; customer * q , *c; q = (customer *)malloc(sizeof(customer)); strcpy(q->name,name); //字符串复制; q->n = n; q->next = NULL; c = p->cusptr; for(;c->next!=NULL;c=c->next){} c->next = q; return OK; } int book(air &p, char * name, int n){ //订票; int a = (p->total - p->left + 1); printf("您已成功订购%d张票,座位号分别是:", n); printf("%d", a); for(int i=1; i <n; i++){ printf(" %d",a+i); } insert_cus(p, name, n); p->left -= n; return OK; } int reg(air &p, char * name, int n){ wt w = (wt)malloc(sizeof(WaitType)); strcpy(w->name,name); w->need = n; w->next = NULL; p->w.front->next = w; p->w.front =p->w.front->next; printf("已登记!"); return OK; } int order(air &a, char * air_num, int day, char * name, int n){ airline * p = a->next; char YorN; for(;p!=NULL;p=p->next){ if(strcmp(air_num,p->air_num)==0 && p->day==day){ if(p->left >= n){ book(p, name, n); return OK; } else{ printf("余票不足!请问您是否要登记候补?登记请输入:Y 不登记输入:N \n"); scanf(" %c",&YorN); if(YorN == 'Y') reg(p, name, n); return OK; } } } printf("输入航班号错误,请重新输入。\n"); return 0; } int del(air &p, char * name){ customer * pre; for(pre=p->cusptr; pre!=NULL; pre=pre->next){ if(strcmp(pre->next->name,name)==0){ customer * k = pre->next; pre->next = k->next; free(k); return OK; } } return OK; } int del_cus(air &a, char * air_num, int day, char * name){ //退票; air p = a; for(;p->next!=NULL;p=p->next){ if(strcmp(air_num,p->air_num)==0 && day == p->day ){ printf("找到要退票的航班!"); for(cus c = p->cusptr; c != NULL; c=c->next){ if(strcmp(c->name, name)==0){ //查找客户姓名; printf("找到要退票的乘客!"); modefy_airline(p, c->n); //空余座位加; del(p, name); printf("退票成功!"); return OK; } } } } printf("未知错误!\n"); return ERROR; } int search_air(airline * head){ airline * p = head->next; printf(" 航班号 飞机号 目的地 总票数 余票数 飞行周日\n"); for(; p!=NULL; p=p->next){ printf(" %-8s %-10s %-8s %-8d %-8d 星期%-8d\n",p->air_num, p->plane_num, p->end_place, p->total, p->left,p->day); } return OK; } int search_place(airline * head, char * place){ airline * p = head->next; for(; p!=NULL; p=p->next){ if(strcmp(place,p->end_place)==0){ printf("航班号:%-8s 飞机号:%-10s 余票数:%-8d 飞行周日:星期%-8d\n",p->air_num, p->plane_num, p->left,p->day); } } //printf("抱歉!没有该目的地,请重新选择。\n"); return 0; } void search_all(air a){ airline * p = a->next; cus c ; wait wt; for(; p!=NULL; p=p->next){ printf("航班号:%-8s飞机号:%-10s目的地:%-s 余票数:%-d 飞行周日:星期%-8d\n",p->air_num, p->plane_num, p->end_place, p->left,p->day); for(c=p->cusptr; c!=NULL; c=c->next){ printf("乘客:姓名:%-8s 订票数:%-d \n",c->name,c->n); } } } int default_cus(cus &c, char * name, int n){ cus k = (customer *)malloc(sizeof(customer)); strcpy(k->name,name); k->n = n; k->next = NULL; c->next = k; return OK; } int default_air(air &a){ //预设航班信息; airline * p = a; char * air_num[6] = {"f007","f008","f009","f009","f008","f007"}; char * plane_num[6] = {"plane01","plane02","plane03","plane03","plane02","plane01"}; char * end_place[6] = {"北京","上海","杭州","福州","广州","杭州"}; int total[6] = {100,100,100,100,100,100}; int left[6] = {52,54,76,44,0,15}; int day[6] = {1,3,5,2,4,6}; char * name[6] = {"Zhangsan","Lisi","Wangwu","Zhaohu","Liqiang","Linna"}; int n[6] = {2,1,1,3,2,1}; for(int i=0; i<6; i++){ customer * c = creat_cus(); wait w = creat_wait(); default_cus(c, name[i], n[i]); insert_air(p, air_num[i], plane_num[i], end_place[i], total[i], left[i], day[i], c, w); } return OK; } int main(){ int t = 1, n, day; airline * air = creat_air(); //创建链表; char name[8], air_num[8], place[20], ch; default_air(air); //初始化; while(t==1){ printf("\n"); printf("********************************\n"); printf("* 欢迎使用和谐航空订票系统! *\n"); printf("* 查询-------------1 *\n"); printf("* 订票-------------2 *\n"); printf("* 退票-------------3 *\n"); printf("* 退出-------------4 *\n"); printf("********************************\n"); scanf(" %c",&ch); //在控制字符前面加空格可以把输入缓冲区中遗留下来的最后一个回车"吃掉"; if(ch=='5'){ //查询全部(隐藏); search_all(air); printf("\n"); } if(ch=='1'){ printf(" 热门城市: \n"); printf(" 1.北京 2.上海 3.杭州\n"); printf(" 4.福州 5.广州 6.天津(停运)\n"); printf("请输入您要查询的目的地:"); scanf(" %s",&place); search_place(air, place); } else if(ch=='2'){ //订票; printf("请输入航班号:"); scanf("%s",&air_num); printf("请输入飞行周日(1~7):"); scanf(" %d",&day); printf("请输入名字:"); scanf("%s",&name); printf("请输入您要订购的票数:"); scanf(" %d",&n); order(air, air_num, day, name, n); } else if(ch=='3'){ //退票; printf("请输入您的姓名:"); scanf(" %s",&name); printf("请输入退票航班号:"); scanf(" %s",&air_num); printf("请输入要退票航班的飞行周日(数字1~7):"); scanf(" %d",&day); del_cus(air, air_num, day, name); } else if(ch=='4'){ //退出; printf("正在推出航空客运订票系统,欢迎再次使用!\n"); t = 0; } } return 0; }