📄 aviation.cpp
字号:
#include "iostream"
#include "string"
using namespace std;
/*******************************定航空基本内容,用全局变量是为开发软件的方便!**********************************************************/
const string endPoint[10] = {"北京","上海","杭州","南京","南宁","武汉","南昌","夏门","安徽","长沙"}; //终点站
const string flightNum[10] = {"CZ01","CZ02","CZ03","CZ04","CZ05","CZ06","CZ07","CZ08","CZ09","CZ10"}; //航班号
const string planeNum[10] = {"SCUT01","SCUT02","SCUT03","SCUT04","SCUT05","SCUT06","SCUT07","SCUT08","SCUT09","SCUT10"}; //飞机号
const string flightDate[10] = {"2008-6-25","2008-6-25","2008-6-26","2008-6-25","2008-6-25","2008-6-25","2008-6-25","2008-6-25","2008-6-25","2008-6-25"};
const int ration = 300; //乘员定额
//定义航空链表
class Link
{
public:
string Name; //记录用户名字
int seatnum; //记录用户的座位号
Link *next;
Link(const string nam, int snum,Link* nextval=NULL) //用于舱位链表的操作
{
Name = nam;
seatnum = snum;
next = nextval;
}
Link(Link *nextval=NULL){ next = nextval; }
};
/***********************************定义侯票链表***********************************/
class waitLink
{
public:
string wName; //记录侯票客户名字
string wphoneNum; //记录侯票客户电话,主要是用于客户的侯票业务
string wEndP; //记录侯票客户的终点站
int wTicketNum; //用于记录客户侯票时的所需的座位数
int wBerth; //记录侯票客户的舱位
waitLink *wnext; //指向下一节点
waitLink *wprev; //指向前一节点
waitLink(const string name, string phonen, string endp,int ber, int ticket, waitLink *prevp = NULL, waitLink* nextval=NULL) //用于侯票链表的操作
{
wName = name;
wphoneNum = phonen;
wEndP = endp;
wBerth = ber;
wTicketNum = ticket;
wnext = nextval;
wprev = prevp;
}
waitLink(waitLink *prevp = NULL,waitLink *nextval=NULL)
{
wprev = prevp;
wnext = nextval;
}
};
/*********************************************定义航线类,是对不同航线下对应的不同舱位里面的座位链表********************************************/
/********************************** **********************************/
class airlineNode
{
public:
int bookseat[10][3]; //定义公有变量,记录每个舱位已定座位数
airlineNode()
{
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 3; j++)
{
fence[i][j] = tail[i][j] = head[i][j] = new Link;
bookseat[i][j] = 0;
fence[i][j]->seatnum = 0;
tail[i][j]->next = NULL;
}
}
}
~airlineNode()
{
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 3; j++)
{
while(head[i][j] != NULL)
{
fence[i][j] = head[i][j];
head[i][j] = head[i][j]->next;
delete fence[i][j];
}
}
}
}
//回到结点起点
void setStart()
{
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 3; j++)
{
fence[i][j] = head[i][j];
}
}
}
void setEnd()
{
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 3; j++)
{
fence[i][j] = tail[i][j];
}
}
}
void next()
{
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 3; j++)
{
if(fence[i][j] != tail[i][j])
{
fence[i][j] = fence[i][j]->next;
}
}
}
}
bool book(int endpnum, int t, int b); //订购机票的节点操作,主要进行的是插入和增加的操作
int remove(string nam, string endP); //删除节点
bool scanBerth( int n, int i); //遍历节点,因为定义的是每个舱位一条链表,所以根据舱位遍历
private:
Link *head[10][3]; //定义二维的指针数组,建立座位链表
Link *tail[10][3]; //表示10个航班,每个航班有3的舱位
Link *fence[10][3];
};
//查看舱位节点的定位信息
bool airlineNode :: scanBerth(int n, int i)
{
if (head[i][n]->next == NULL)
{
cout<<"该舱位暂无定位信息。"<<endl;
}
else
{
for (fence[i][n] = head[i][n]->next; fence[i][n] != NULL; fence[i][n] = fence[i][n]->next)
{
cout<<"座位"<<fence[i][n]->seatnum<<"\t"<<fence[i][n]->Name<<endl;
}
}
cout<<endl;
return true;
}
//订购机票
bool airlineNode :: book(int endpnum,int t, int b)
{
int stnums; //用于记录座位号的初值
int stnume[100]; //用于记录每个添加的座位号,方便输出
string nam;
int tnum = t; //记录所定机票的总数
cout<<"输入你的姓名:";
cin>>nam;
cout<<endl;
fence[endpnum][b] = head[endpnum][b];
if (tail[endpnum][b]->seatnum != bookseat[endpnum][b]) //当该舱位链表的尾节点记录的座位号和已定座位号不相等时,
{ //说明该链表前面有人退订了票,删除了节点,后面订票的人需插入
for (int j = 1 ; j < 100 && t != 0 ; j++) //从链表头开始查找,当查找到的节点记录的座位号与j(即累加的座位号)
{ //不相等时,则在该节点之前插入
if (fence[endpnum][b]->next != NULL && fence[endpnum][b]->next->seatnum != j)
{
stnume[tnum-t] = j;
Link *p = new Link(NULL); //新建个节点,记录所要插入节点的信息
p->seatnum = stnume[tnum-t];
p->Name = nam;
p->next = fence[endpnum][b]->next;
fence[endpnum][b]->next = p;
t--;
p = NULL;
bookseat[endpnum][b]++;
}
if (fence[endpnum][b]->next != NULL) //当查找到链表尾节点时,break,继续后面的增加节点的操作
{
fence[endpnum][b] = fence[endpnum][b]->next;
}
else break;
}
tail[endpnum][b] = fence[endpnum][b];
}
if(tail[endpnum][b]->seatnum == bookseat[endpnum][b]) //在链表尾节点增加节点
{
for (stnums = tail[endpnum][b]->seatnum;t > 0; t--)
{
tail[endpnum][b] = tail[endpnum][b]->next = new Link(nam,++stnums,NULL);
bookseat[endpnum][b]++;
stnume[tnum - t] = stnums;
}
}
/*进行输出订票信息的操作*/
cout<<"订位成功!你预订的飞机为:"<<endl;
cout<<"终点站:"<<endPoint[endpnum]<<" "<<"航班号:"<<flightNum[endpnum]<<" "<<"飞机号:"<<planeNum[endpnum]<<"日期:"<<flightDate[endpnum]<<endl;
cout<<"姓名:"<<nam<<endl;
cout<<"座位号:";
for(int i = 0; i < tnum; i++)
{
cout<<stnume[i]<<" ";
}
cout<<endl;
return true;
}
//删除座位节点
int airlineNode :: remove(string nam, string endP)
{
string n;
int berthn; //记录舱位号,用于侯票客户检查
int check1 = 0; //检查是否查找到该终点的航班
int check2 = 0; //检查是否查找到该用户的信息
int check3 = 0; //检查是否三个舱位都没有信息
berthn = 7; //随机初始化一个不在(0~3)范围的数字
for (int i = 0; i < 10; i++)
{
if (endP == endPoint[i])
{
check1 += 1;
for(int j = 0; j < 3; j++)
{
if(head[i][j]->next == NULL) check3++;
else
{
fence[i][j] = head[i][j];
while(fence[i][j] != tail[i][j])
{
Link *temp2 = fence[i][j]->next;
if (temp2->Name == nam)
{
berthn = j;
check2 += 1;
fence[i][j]->next = temp2->next;
if(tail[i][j] == temp2) tail[i][j] = fence[i][j];
cout<<"已退订去往"<<endPoint[i]<<"的"<<flightNum[i]<<"号航班"<<j+1<<"号舱的"<<temp2->seatnum<<"号座位"<<endl;
delete temp2;
bookseat[i][j]--;
}
else
{
fence[i][j] = fence[i][j]->next;
}
}
}
}
}
}
if (check1 == 0) {cout<<"没有到达该地点的航班!"<<endl; return berthn;}
else if (check3 == 3) {cout<<"该航班暂无订票信息!"<<endl<<endl; return berthn;}
else if(check2 == 0) {cout<<"该航班没有该客户的信息!请重新确认"<<endl; return berthn;}
else return berthn;
}
/******************************************************定义航班类************************************************/
class flight : public airlineNode
{
public:
flight()
{
maxsize = 100;
waitnum = 0;
waitf = waith = waitt = new waitLink;
for (int i = 0; i < 10; i++)
{
balance[i] = 300;
}
}
~flight()
{
while(waith != NULL)
{
waitf = waith;
waith = waith->wnext;
delete waitf;
}
};
void checkAll();
void checkLane();
bool bookcheck( string ep);
bool cancelTicket();
bool waitT(int endp,int t,int ber);
bool checkwait(string endp, int checkr);
void scanwait();
private:
int balance[10]; //记录每个舱位最大的容量
waitLink *waitf; //侯票链表栅节点
waitLink *waith; //侯票链表头节点
waitLink *waitt; //侯票链表尾节点
int maxsize; //制定最大座位数
int waitnum; //侯票人数
};
//查看所有航班
void flight::checkAll()
{
cout<<"终点站 航班号 飞机号 飞行日期 乘员定额 余票量"<<endl;
for (int i = 0; i < 10; i++)
{
cout<<endPoint[i]<<" "<<flightNum[i]<<" "<<planeNum[i]<<" "<<flightDate[i]<<" "<<ration<<" "
<<balance[i]-(bookseat[i][0] + bookseat[i][1] + bookseat[i][2])<<endl;
}
cout<<"■〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓■"<<endl<<endl;
}
//查询单条航线
void flight::checkLane()
{
string endP; //输入终点查询
char ch;
bool check1 = false;
bool check2;
cout<<"\n■◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆航班信息查询◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆■\n"
<<"■ ■\n"
<<"■〓〓〓〓〓〓〓〓〓〓〓〓暂提供达到以下终点的航班〓〓〓〓〓〓〓〓〓〓〓〓■\n"
<<endl;
for (int j = 0; j < 10; j++)
{
cout<<endPoint[j]<<"\t";
}
cout<<endl;
cout<<"\n■〓〓〓〓〓〓〓〓〓〓〓〓输入所需查找航班的终点站〓〓〓〓〓〓〓〓〓〓〓〓■\n"
<<endl;
cin>>endP;
cout<<endl;
for (int i = 0; i < 10; i++)
{
if (endP == endPoint[i])
{
cout<<"终点站 航班号 飞机号 飞行日期 余票量\n"
<<endPoint[i]<<" "<<flightNum[i]<<" "<<planeNum[i]<<" "<<flightDate[i]<<" "
<<balance[i]-(bookseat[i][0] + bookseat[i][1] + bookseat[i][2])<<endl;
cout<<"各舱订票信息:"<<endl<<endl;
cout<<"头等舱:"<<endl; scanBerth(0,i);
cout<<"经济舱:"<<endl; scanBerth(1,i);
cout<<"平价舱:"<<endl; scanBerth(2,i);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -