打印全部顾客函数,指针出现问题,没有传递正确。或订票函数中的addCList()函数指针传递错误,导致订票后,打印全部顾客函数出错。
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define m 20
using namespace std;
//乘客信息链表节点
typedef struct CNode {
char name[m];//乘客姓名
int num;//订票数
int rank;//机舱等级
CNode *next;
} CNode, *CList;
//候补乘客数据节点
typedef struct HNode {
char name[m];//候补乘客姓名
int num;//候补票数
HNode *next;
} HNode, *HList;
//链队节点
typedef struct Queue {
HList front;
HList rear;
} LinkQueue;
//航班节点
typedef struct hangban {
char destination[m];//目地的
char hnum[m];//航班号
char planeid[m];//飞机型号
char day[m];//飞行日期
int maxnum;//机载人数
int less;//总余票
int lessrank1;//一等余票
int lessrank2;//二等余票
int lessrank3;//三等余票
hangban *next;//下一个节点
CList cusLinkList;//乘客名单域,指向乘客名单链表的头指针
LinkQueue waitrank1;//候补一等队列
LinkQueue waitrank2;//候补二等队列
LinkQueue waitrank3;//候补三等队列
} hangbanNode, *Phangban;
Phangban allflight;//全局头节点变量
CList c;//全局头结点变量
hangban flight1[100] = {
{ "北京", "1", "A1111", "星期一", 5, 5, 2, 2, 1},
{ "上海", "2", "B2222", "星期三", 100, 0, 0, 0, 0},
{ "上海", "3", "C3333", "星期五", 100, 5, 2, 2, 1},
{ "上海", "4", "D4444", "星期六", 100, 5, 2, 2, 1},
{ "上海", "5", "E5555", "星期二", 100, 5, 2, 2, 1},
{ "上海", "6", "F6666", "星期四", 100, 5, 2, 2, 1},
{ "上海", "7", "G7777", "星期五", 100, 5, 2, 2, 1},
{ "上海", "8", "H8888", "星期三", 100, 5, 2, 2, 1},
{ "上海", "9", "I9999", "星期二", 100, 5, 2, 2, 1},
{ "上海", "10", "J1010", "星期一", 100, 5, 2, 2, 1},
{ "上海", "11", "K1212", "星期四", 100, 5, 2, 2, 1},
{ "上海", "12", "L1313", "星期日", 100, 5, 2, 2, 1},
{ "上海", "13", "M1414", "星期六", 100, 5, 2, 2, 1},
{ "上海", "14", "N1515", "星期二", 100, 5, 2, 2, 1},
};
//初始化乘客链表
void initCList(CList &cusLinkList) {
cusLinkList = (CNode *)malloc(sizeof(CNode));
if (cusLinkList == NULL) {
cout << "内存不足" << endl;
return;
}
cusLinkList->next = NULL;
c = cusLinkList;
}
//初始化队列
void initQueue(LinkQueue &waitrank) {
HList p = (HList)malloc(sizeof(HNode));
if (p == NULL) {
cout << "内存不足" << endl;
return;
}
p->next = NULL;
waitrank.front = waitrank.rear = p;
}
//初始化候补乘客
void houbu(HList &head, char name[], int need) {
HList q = (HNode *)malloc(sizeof(HNode));
strcpy(q->name, name);
q->num = need;
q->next = head->next;
head->next = q;
}
//进队
LinkQueue enqueue(LinkQueue &q, char name[], int need) {
HList new1 = (HNode *)malloc(sizeof(HNode));
strcpy(new1->name, name);
new1->num = need;
new1->next = NULL;
q.rear->next = new1;
q.rear = new1;
return q;
}
//添加正式乘客信息
void addCList(CList &head, int need, char name[], int grade) {
CList new1 = (CNode *)malloc(sizeof(CNode));
if (new1 == NULL) {
cout << "内存不足" << endl;
return;
}
strcpy(new1->name, name);
new1->num = need;
new1->rank = grade;
new1->next = NULL;
CList current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = new1;
}
//打印正式顾客
void printfCList(CList head) {
//CList head = c;
//CList current = (CNode *)malloc(sizeof(CNode));
CNode *current = c->next; // 从头节点的下一个节点开始遍历
//current=head->next;
printf("客户姓名 舱位等级\n");
if(current==NULL)
printf("没有乘客");
while (current != NULL) {
printf("%s %d\n", current->name, current->rank);
current = current->next;
}
printf("\n\n");
}
//快速查找航班
Phangban search() {
char number[m];
cout << "请输入航班编号: ";
cin >> number;
Phangban p = allflight->next;
while (p) {
if (!strcmp(number, p->hnum))
return p;
p = p->next;
}
return NULL;
}
//候补函数
void Houbu(Phangban &info, LinkQueue &waitrank, int les, int grade) {
HList p = waitrank.front;
p=p->next;
while (p!=NULL) {
if (p->num <= les) {
printf("%s候补成功", p->name);
CList head = info->cusLinkList;
addCList(head, p->num, p->name, grade);
printf("您的座位号是:%d\n", info->maxnum - info->less + 1);
les -= p->num;
info->less -= p->num;
}
p=p->next;
}
if (grade == 1)
info->lessrank1 = les;
else if (grade == 2)
info->lessrank2 = les;
else
info->lessrank3 = les;
}
//初始化航班
void initflight() {
allflight = (hangban *)malloc (sizeof(hangban));
if (allflight == NULL)//航班为空,退出程序
exit(0);
allflight->next = NULL;
Phangban p = allflight, q;//将p赋给allflight
//链表赋值
for (int i = 0; i < 14; i++) {
q = (hangban *)malloc(sizeof(hangban));
if (q == NULL)
return;
strcpy(q->destination, flight1[i].destination);
strcpy(q->hnum, flight1[i].hnum);
strcpy(q->planeid, flight1[i].planeid);
strcpy(q->day, flight1[i].day);
q->maxnum = flight1[i].maxnum;
q->less = flight1[i].less;
q->lessrank3 = flight1[i].lessrank3;
q->lessrank2 = flight1[i].lessrank2;
q->lessrank1 = flight1[i].lessrank1;
initCList(q->cusLinkList);
initQueue(q->waitrank1);
initQueue(q->waitrank2);
initQueue(q->waitrank3);
q->next = p->next;
p->next = q;
}
}
//打印全部航线
void printfall(Phangban allflight) {
Phangban p;
p = allflight->next;
printf("目的地 航班号 飞机号 飞行日期 机载量 总余票 一等余票 二等余票 三等余票\n");
while (p) {
printf("%4s %2s %5s %8s %8d %8d %8d %8d %5d\n", p->destination, p->hnum, p->planeid, p->day, p->maxnum,
p->less, p->lessrank1, p->lessrank2, p->lessrank3);
p = p->next;
}
printf("\n\n");
}
//查询单个航线
void chaxun() {
char destination[20];
int flag = 0;//判断航班是否有该航班
cout << endl;
printf("请输入终点站名:");
cin >> destination;
cout << endl;
Phangban p;
p = allflight->next;
printf("目的地 航班号 飞机号 飞行日期 机载量 总余票 一等余票 二等余票 三等余票\n");
while (p) {
if (strcmp(destination, p->destination) == 0) //匹配
{
flag = 1;//flag重新赋值
printf("%4s %4s %2s %5s %8d %5d %5d %5d %5d\n", p->destination, p->hnum, p->planeid, p->day,
p->maxnum, p->less, p->lessrank1, p->lessrank2, p->lessrank3);
};
p = p->next;
}
if (flag == 0)
cout << "该航班不存在!";
cout << endl << endl;
}
//办理订票
void dingpiao() {
char name[m];
char day[m];
int need, grade;
int num;
Phangban info;
info = search();
if (info == NULL) {
cout << "不存在该航班!" << endl;
return;
}
cout << "请输入您想要订的舱位等级(1/2/3):";
while (true) {
cin >> grade;
if (grade < 1 || grade > 3)
cout << "输入有误,请重新输入:";
else {
if (grade == 1)
num = info->lessrank1;
else if (grade == 2)
num = info->lessrank2;
else
num = info->lessrank3;
break;
}
}
cout << "请输入您需要订的票数:";
while (true) {
cin >> need;
if (need < 1 || need > info->maxnum)//需要的票超过机载人数
cout << "输入有误,请重新输入:";
else
break;
}
if (need <= num) { //余票够
for (int i = 1; i <= need; i++) {
cout << "请输入您的姓名:";
cin >> name;
CList head = info->cusLinkList;
addCList(head, 1, name, grade);
printf("%s 的座位号是:%d\n", name, info->maxnum - info->less + i);
}
info->less -= need;
if (grade == 1)
info->lessrank1 -= need;
else if (grade == 2)
info->lessrank2 -= need;
else
info->lessrank3 -= need;
cout << "订票成功" << endl;
} else {
char vis;
cout << "该舱位的余票不足,请问您是否需要候补(Y/N)" << endl;
while (true) {
cin >> vis;
if (vis != 'Y' && vis != 'N')
cout << "输入错误,请重新输入:";
else {
if (vis == 'Y') {
for (int i = 1; i <= need; i++) {
cout << "请输入您的姓名:";
cin >> name;
if (grade == 1) {
info->waitrank1 = enqueue(info->waitrank1, name, 1);
} else if (grade == 2) {
info->waitrank2 = enqueue(info->waitrank2, name, 1);
} else
info->waitrank3 = enqueue(info->waitrank3, name, 1);
cout << "排队成功!" << endl;
}
}
break;
}
}
}
}
//办理退票
void tuipiao() {
Phangban info;
int grade;
CList p1, p2, head;
char name[20];
info = search();
if (info == NULL) {
cout << "退票失败,该航班不存在!" << endl;
}
head = info->cusLinkList;
p1 = head->next;
cout << "请输入姓名:";
cin >> name;
p2 = head ;
while (p1) {
if (strcmp(name, p1->name) == 0)
break;
p2 = p1;
p1 = p1->next;
}
if (p1 == NULL) {
cout << "对不起,您没有订过票,或者输入信息有误!" << endl;
return;
} else {
grade = p1->rank;
p2->next = p1->next;
info->less += p1->num;
printf("%s 退票成功!\n", p1->name);
if (grade == 1) {
info->lessrank1 += p1->num;
Houbu(info, info->waitrank1, info->lessrank1, grade);
} else if (grade == 2) {
info->lessrank2 += p1->num;
Houbu(info, info->waitrank2, info->lessrank2, grade);
} else {
info->lessrank3 += p1->num;
Houbu(info, info->waitrank3, info->lessrank3, grade);
}
free(p1);
}
}
//主页
void menu() {
int res;
printf("\n\n");
printf(" 航空订票系统 \n");
printf("***********************************************************\n");
printf(" 1. 总航班 \n");
printf(" 2. 查询航线 \n");
printf(" 3. 订票 \n");
printf(" 4. 退票 \n");
printf(" 5. 打印客户名单 \n");
printf(" 6. 退出 \n");
printf("***********************************************************\n");
printf(" 输入需要办理的业务:");
scanf("%d", &res);
switch (res) {
case 1:
printfall(allflight);//打印全部航班信息
menu();
break;
case 2:
chaxun();//查询航班
menu();
break;
case 3:
dingpiao();//订票
menu();
break;
case 4:
tuipiao();//退票
menu();
break;
case 5:
printfCList(c);//打印全部正式客户
menu();
break;
case 6:
printf("欢迎再次使用!");//退出系统
exit(0);
default:
printf("输入有误!");
exit(0);
}
}
int main() {
initflight();//初始化航班链表
initCList(c);
menu();
return 0;
}