航空订票 求大神帮忙看下错误
下面是我的代码。可以正常输入,但是到了按目的地查询航班(输入的目的地是正确的)就有问题了,一直显示输入错误;还有查看航班(查看所有航班)会有随机值出现,求大佬帮忙改下,感激不尽!
如果有时间的话,顺带帮忙看下还有没有其他需要改正的地方,谢谢了
#include "stdafx.h"
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <stdio.h>
#define MAX 60
typedef struct Customer
{//乘客信息
char Name[8];
int Amount; //订票数
char Rank;
int Information;
struct Customer *Next;
}Customer;
typedef struct Flight
{//航班信息
char Starting[10]; //出发地
char Destination[10];
char Flight_num[6]; //航班号
char Departure[6]; //起飞时间
char Fall[6]; //降落时间
int Customer_amount; //总票数
int Free_amount; //余票数
int Discount; //折扣率
int Price[3];
Customer *CustName; //已定乘客名单
Customer *ReplName; //候补乘客名单
struct Flight *Next; //指示下一航班结点
}Flight, *PFlight;
//全局变量
int Customer_Count = 0; //所有航班订票乘客总数
Flight *Head = NULL; //航班头指针
Flight *p2; //航班结点指针
Customer *Custp1[MAX]; //航班乘客结点指针
Customer *Replp1[MAX]; //航班候补乘客结点指针
int IsEmpty = 1; //是否有已订票乘客
int IsReplace = 1; //是否有候补乘客
Customer *prior; //满足要求乘客的前结点,以便做删除操作
int shouldsave = 0;
//1.询问是否继续函数
char Continue()
{
char answer;
while (1)
{
printf("\n\t 是否继续?(Y/N)");
scanf("%c", &answer);
fflush(stdin);
system("cls");
if (answer == 'y' || answer == 'Y')
return 'y';
else if (answer == 'n' || answer == 'N')
return 'n';
else
printf("\n\t输入错误,请重新输入!");
}
}
//2.操作出错函数
void Error()
{
printf("\n\t对不起,您的操作有误!");
getchar();
}
//3-1 核对航班函数
int Check_Line1(PFlight L, char *key)
{
int flag = 0; //标志位0表示未找到相关信息,反之即找到
Flight *p1;
p1 = L; //赋航班首地址
if (p1 == p2)
return flag;
while (p1 == p2 && p1 != NULL)
{ //本航班不纳入比较范围,否则会一直提示航班不唯一
if (strcmp(p1->Flight_num, key) == 0)
{
flag = 1;
break;
}
p1 = p1->Next;
}
return flag;
}
//3-2 按航班号查询函数
int Find_Line2(PFlight L, char *key, PFlight *pp, int *Flight_num)
{
int flag = 0;
Flight *p1;
p1 = L;
while (p1 != NULL)
{
if (strcmp(p1->Flight_num, key) == 0)
{
flag = 1;
*pp = p1;
break;
}
p1 = p1->Next;
if (p1 != NULL)
Flight_num++;
}
return flag;
}
//4.按目的地查询航班函数
void Line_search()
{
char Desname[50];
Flight *p1 = Head;
if (Head == NULL)
{
printf("\n\t对不起,没有您查询的航班");
getchar();
return;
}
printf("\n\t请输入目的地;");
scanf("%s", Desname);
printf("\n\t您查询的航班如下;\n");
printf("\n__________________________________________________________________________________________________________\n");
while (p1 != NULL)
{
if (strcmp(p1->Destination, Desname) == 0)
{
printf("出发地 目的地 航班号 起飞时间 落地时间 头等舱/元 普通舱/元 经济舱/元 折扣率(%%) 总票数 余票数\n");
printf("\n%-9s%-9s%-8s%-7s%-7s%-10d%-12d%-8d%-7d%-7d%-7d", p1->Starting,p1->Destination, p1->Flight_num, p1->Departure,p1->Fall,
p1->Price[0], p1->Price[1], p1->Price[2],p1->Discount,p1->Customer_amount,p1->Free_amount);
}
p1 = p1->Next;
}
printf("\n__________________________________________________________________________________________________________\n");
Continue();
}
//5.添加新航班函数
void Line_Add()
{
PFlight *p1, p; ////建立临时航班结点
p1 = &Head; ////传航班链表头指针的地址
while (1)
{
if (Head == NULL) //航班为空*/
{
*p1 = (PFlight)malloc(sizeof(Flight));
(*p1)->Next = NULL;
p2 = Head; ////建立首个航班*/
}
else
{
p1 = &p; ////p1指向新的地址空间,以保持头指针 Head 的值不变!!!
*p1 = (PFlight)malloc(sizeof(Flight)); ////建立新航班结点
p2->Next = *p1; ////前一航班结点指向当前航班结点*/
p2 = *p1; ////保留当前航班结点地址
}
printf("\n\t添加新航班\n");
printf("\n\t请输入出发地:");
scanf("%s", &p2->Starting);
printf("\n\t请输入目的地:");
scanf("%s", &p2->Destination);
while (1)
{
printf("\n\t请输入航班号:");
scanf("%s", &p2->Flight_num);
if (Check_Line1(Head, p2->Flight_num))
printf("\n\t该航班已存在!\n");
else
break;
}
printf("\n\t请输入起飞时间:");
scanf("%s", &p2->Departure);
printf("\n\t请输入到达时间:");
scanf("%s", &p2->Fall);
printf("\n\t请输入头等舱价格:");
scanf("%d", &p2->Price[0]);
printf("\n\t请输入普通舱价格:");
scanf("%d", &p2->Price[1]);
printf("\n\t请输入经济舱价格:");
scanf("%d", &p2->Price[3]);
printf("\n\t请输入折扣率(%%):");
scanf("%d", &p2->Discount);
printf("\n\t请输入座位数量:");
scanf("%d", &p2->Customer_amount);
printf("\n\t请输入余票数量:");
scanf("%d", &p2->Free_amount);
fflush (stdin);
p2->Free_amount = p2->Customer_amount; //余票都在
p2->CustName = NULL; //订票乘客头指针为空
p2->ReplName = NULL; //候补名单为空
shouldsave = 1;
if (Continue() == 'n')
{
p2->Next = NULL;
return;
}
}
}
//6.判断航班是否存在函数
int Empty_Flight()
{
if (Head == NULL)
{
system("cls");
printf("\n\t对不起,该航班不存在,按任意键返回");
getchar();
return 1;
}
else
return 0;
}
//7.航班查看函数
int Line_see()
{
Flight *p1;
system("cls");
p1 = Head;
if (Empty_Flight())
return 0;
printf("\n\t您查询的航班信息如下\n");
printf("\n_______________________________________________________________________________________________\n");
printf("出发地 目的地 航班号 起飞时间 到达时间 头等舱/元 普通舱/元 经济舱/元 折扣率(%) 总票数 余票数\n");
while (p1 != NULL)
{
printf("\n%-9s%-9s%-8s%-7s%-7s%-10d%-12d%-8d%-7d%-7d%-7d", p1->Starting, p1->Destination, p1->Flight_num, p1->Departure, p1->Fall,
p1->Price[0], p1->Price[1], p1->Price[2], p1->Discount, p1->Customer_amount, p1->Free_amount);
p1 = p1->Next;
}
printf("\n\t_____________________________________________________________________________________________\n");
printf("\n\t按任意键返回\n");
getchar();
}
//8.航班管理菜单
void ManageMenu()
{
char c;
system("cls");
while (1)
{
printf("\n\t\t航班主菜单");
printf("\n___________________________________________\n\n");
printf("\t 1. 添加新航班\n");
printf("\t 2. 查询航班\n");
printf("\t 3. 查看航班\n");
printf("\t 4. 返回上级菜单\n");
printf("\n____________________________________________\n");
printf("\t 请输入您所需服务序号:");
scanf("%c", &c);
fflush (stdin);
switch (c)
{
case '1':Line_Add();
break;
case '2':Line_search();
break;
case '3':Line_see();
break;
case'4':
return;
}
}
}
//9.订票管理函数
void Booking()
{
int Ticket_count, Information, i, flag = 0;
int Flight_num = 0;
Flight *p1;
Customer *c1; //订票乘客结点
Customer *c2; //候补乘客结点
char answer[7]; //输入的航班数据
char temp, c;
int tag = 0; //候补乘客标志位
int IsRepl = 0; //是否执行候补操作
if (Empty_Flight())
return; //航班为空
while (1)
{
printf("\n\t现在您可以订票");
flag = 0;
Flight_num = 0;
tag = 0;
printf("\n\t请输入航班号");
scanf("%c", &answer);
if (Find_Line2(Head, answer, &p1, &Flight_num))
{
while (1)
{
printf("\n\t请输入购票数量");
scanf("%d", &Ticket_count);
if (Ticket_count == 0)
{
printf("\n\t请再次输入航班号");
getch();
}
else
break;
}
if (p1->Free_amount >= Ticket_count)
{
Customer_Count++;
flag = 1; //已订票
IsRepl = 1; //订票量满足,无须进入候补操作
if (p1->Customer_amount == NULL)
{
Custp1[Flight_num] = c1 = (Customer*)malloc(sizeof(Customer));
p1->CustName = Custp1[Flight_num];
}
else
{
c1 = (Customer*)malloc(sizeof(Customer));
Custp1[Flight_num]->Next = c1;
Custp1[Flight_num] = c1;
}
IsEmpty = 0; //订票乘员不为空
Custp1[Flight_num]->Amount = Ticket_count; //订票数
Information = p1->Customer_amount - p1->Free_amount; //算出座位号
Custp1[Flight_num]->Information = Information; //赋座位号
p1->Free_amount -= Ticket_count; //剩余票数减去已订票数,即现在剩余票数
printf("\n\t请输入您的姓名");
scanf("%s", &Custp1[Flight_num]->Name);
while (1)
{ //数据正确检测
printf("\n\t请输入舱位等级");
scanf("%s", &Custp1[Flight_num]->Rank);
if (Custp1[Flight_num]->Rank<'1' || Custp1[Flight_num]->Rank>'3')
{
printf("\n\t输入错误,请重新输入");
getch();
}
else
break;
}
printf("\n\t请输入您的身份信息");
scanf("%d", &Custp1[Flight_num]->Information);
if (Ticket_count < 10)
printf("\n\t");
else //为显示规整,作相应处理
printf("\n\t");
printf("\n\t恭喜您订票成功\n");
for (i = 1; i < Ticket_count; i++);
{
printf("\n\t您预订的座位号为%d", Information++);
if (i % 10 == 0)
printf("\n\t");
}
printf("\n");
} //满足订票数
else if (p1->Free_amount == 0)
{
printf("\n\t对不起,票已售完!");
IsRepl = 0;
}
else
{
printf("\n\t对不起,没有多余的票");
IsRepl = 0;
}//同上
if (!IsRepl)
{
printf("\n\t是否想排队等票?");
scanf("%s", &temp);
if (temp == 'y' || temp == 'Y')
{
if (p1->ReplName == NULL)
{
c2 = (Customer*)malloc(sizeof(Customer));
Replp1[Flight_num] = c2;
p1->ReplName = Replp1[Flight_num];
}
else
{
c2 = (Customer*)malloc(sizeof(Customer));
Replp1[Flight_num]->Next = c2;
Replp1[Flight_num] = c2;
}
IsReplace = 0;
tag = 1;
Replp1[Flight_num]->Amount = Ticket_count;
printf("\n\t请输入您的姓名:");
scanf("%s", &Replp1[Flight_num]->Name);
Replp1[Flight_num]->Information = Information;
Replp1[Flight_num]->Amount = Ticket_count;
while (1)
{
printf("\n\t请输入舱位等级:");
scanf("%s", &Replp1[Flight_num]->Rank);
printf("\n\t 请输入您的身份信息:");
scanf("%d", &Replp1[Flight_num]->Information);
if (!(Replp1[Flight_num]->Rank >= '1'&&Replp1[Flight_num]->Rank <= '3'))
{
printf("\n\t 输入错误!请重新输入!");
getch();
}
else
break;
}
printf("\n\t 没有剩余空位!\n");
shouldsave = 1;
}
}
}
else
printf("\n\t 对不起,航班不存在!\n");
if (flag)
Custp1[Flight_num]->Next = NULL;
if (tag)
{
Replp1[Flight_num]->Next = NULL;
printf("\n\t 您已成功排入候补订票队列中!\n");
}
printf("\n\t 是否退出菜单?:(y/n)");
scanf("%c", &c);
if (c == 'y')
return;
}
}
//订票乘客信息
void Display_Reserve() {
Flight *p1;
Customer *c1;
system("cls");
p1 = Head;
if (Empty_Flight())
return;
printf("\n\t 订票乘客信息");
if (IsEmpty)
{
printf("\n\t 对不起,没有订票乘客信息!\n");
getch();
return;
}
printf("\n________________________________________\n");
printf("姓名 航班号 订票总数 目的地 舱位等级 身份信息\n");
while (p1 != NULL)
{
if (p1->CustName != NULL)
{
c1 = p1->CustName;
while (c1 != NULL)
{
printf("\n%-8s%-10s%-11d%-9d%-9d%-9d", c1->Name, p1->Flight_num, c1->Amount, p1->Destination, c1->Rank, c1->Information);
if (p1->Free_amount >= 1)
printf("\n\n\t 还有多余的票!\n");
else
printf("\n\n\t 票已售完!\n");
c1 = c1->Next;
}
}
p1 = p1->Next;
printf("\n\n________________________________________\n");
}
printf("\n\t 按任意键返回!\n");
getch();
return;
}
//候补乘客信息
void Display_Replace()
{
Flight *p1;
Customer *c1;
system("cls");
p1 = Head;
if (Empty_Flight())
return;
printf("\n\t 候补乘客信息!");
if (IsReplace)
{
printf("\n\t 对不起,没有候补乘客!\n"); getch();
return;
}
printf(" 姓名 航班号 订票数 目的地 舱位等级 顾客号 \n");
while (p1 != NULL)
{
if (p1->ReplName)
{
printf("\n%-8s%-10s%-9s%-11d%-9d%-9d%-9d", c1->Name, p1->Flight_num, c1->Amount, p1->Destination, c1->Rank, c1->Information);
if (p1->Free_amount >= 1)
printf("\n\t 还有多余的票!\n");
else
printf("\n\t 票已售完!\n");
c1 = c1->Next;
}
}
p1 = p1->Next;
printf("\n\n____________________________________________\n");
printf("\n\t 按任意键返回!");
getch();
return;
}
//退票办理函数
void RefundticketMenu()
{
int Flight_num, flag = 0;
Flight *p1;
Customer *c2, *c4;
Customer *c3, *c5;
char answer[7], name[7]; //用户输入的航班数据
int tag = 0;
int Information;
if (Empty_Flight())
return;
printf("\n\t 现在开始进行退票手续!");
if (IsEmpty)
{
printf("\n\t 对不起,乘客不存在!");
getch();
return;
}
while (1)
{
flag = 0; tag = 0;
Flight_num = 0;
printf("\n\t 请输入航班:");
scanf("%c", &answer);
if (Find_Line2(Head, answer, &p1, &Flight_num))
{
c2 = p1->CustName;
printf("\n\t 请输入您的姓名:");
scanf("%s", &name);
if (c2 == NULL)
{
printf("\n\t 对不起,乘客不存在!");
if (Continue() == 'n')
return;
}
else
while (c2 != NULL)
{
if (strcmp(c2->Name, name) == 0)
{
if (c2 = p1->CustName)
{
prior = p1->CustName;
Information = c2->Next->Information;
flag = 1;
break;
}
}
c2 = c2->Next;
shouldsave = 1;
}
if (!flag)
printf("\n\t 对不起,乘客不存在\n");
}
else printf("\n\t 对不起,航班不存在!\n");
if (flag)
{
if (prior == p1->CustName && !tag)
{
if (prior->Next == NULL)
{
p1->Free_amount += prior->Amount;
p1->CustName = NULL;
}
else
{
p1->Free_amount += prior->Amount;
p1->CustName = prior->Next;
}
}
else
{
p1->Free_amount += prior->Next->Amount;
prior->Next = prior->Next->Next;
}
Customer_Count--;
if (Customer_Count == 0) IsEmpty = 1;
shouldsave = 1;
}
if (flag)
{ //存在退票操作
c3 = p1->ReplName;
while (c3 != NULL)
{
if (c3->Amount <= p1->Free_amount)
{ //候补乘客的订票数小于或等于剩余票数
printf("\n\t 候补乘客已经存在!\n");
c4 = (Customer*)malloc(sizeof(Customer)); //分配新节点
Custp1[Flight_num]->Next = c4;
c4->Next = NULL;
IsEmpty = 0;
if (p1->CustName == NULL) p1->CustName = c4;
strcpy(c4->Name, c3->Name);
c4->Rank = c3->Rank;
c4->Amount = c3->Amount;
c4->Information = Information;
p1->Free_amount -= c3->Amount; //减去相应的票数
Customer_Count++;
if (c3->Next == NULL) IsReplace = 1; //无候补乘客
if (p1->ReplName == c3)
{
if (p1->ReplName->Next == NULL)
p1->ReplName = NULL; //删除
else p1->ReplName = c3->Next;
}
else c5->Next = c3->Next->Next;
break;
}
if (c3->Next != NULL)
if (c3->Next->Amount <= p1->Free_amount)
c5 = c3;
c3 = c3->Next;
shouldsave = 1;
}
printf("\n\t 退票成功!");
getch();
return;
}
shouldsave = 1;
if (Continue() == 'n') return;
}
}
//乘客管理子菜单函数
void CustomermagMenu()
{
char c;
system("cls");
while (1)
{
printf("\n\t 乘客管理菜单\n");
printf("\n___________________________\n");
printf("\t 1.乘客信息 \n");
printf("\t 2.候补乘客信息 \n");
printf("\t 3.返回主菜单 \n");
printf("\n___________________________\n");
printf("\t 请选择您想要的服务:");
scanf("%c", &c);
fflush(stdin);
switch (c)
{
case '1': Display_Reserve(); break;
case '2': Display_Replace(); break;
case '3': return;
default: Error();
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
char c;
Flight *p1;
system("color 0c");
system("mode con: cols=78 ;lines=25");
p1 = Head;
do {
system("cls");
printf("\n\t\t航空客运订票系统主菜单\n ");
printf("\n ************************************\n");
printf("\t 1.航班管理菜单 \n");
printf("\t 2.订票办理菜单 \n");
printf("\t 3.退票办理菜单\n");
printf("\t 4.乘客管理菜单\n");
printf("\t 5.退出系统 \n");
printf("\t 谢谢使用航空客运订票系统 \n");
printf("请选择您想要的服务:");
scanf("%c", &c);
fflush (stdin);
switch (c)
{
case '1':ManageMenu(); break;
case '2':Booking(); break;
case '3':RefundticketMenu(); break;
case '4':CustomermagMenu(); break;
case '5':exit(0);
default: break;
}
}
while (c != '5');
return 0;
}