#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <cstring>
using namespace std;
class userLink;
struct Node
{
int i;
Node* next;
};
class LQueue
{
private:
Node* front, * rear;
public:
LQueue();
~LQueue();
void EnQueue(int x);
int DeQueue();
bool Emply();
};
struct Plane
{
char plane_number[10];
char strat_time[10];
char end_time[10];
char strat_city[20];
char end_city[20];
int full;
int empty_seats;
int all_seats;
LQueue backseats;
float price;
float discount;
Plane* next;
};
struct user
{
char name[10];
char ID[20];
char plane_number[10];
int order_number;
user* next;
};
class planeLink//飞机链表
{
private:
Plane* head;
Plane* end;
public:
planeLink();
~planeLink();
void delete_plane(char* number, userLink& u);
void add_plane(Plane& p);
void imp_plane();
void out_plane();
void back_seat(int x, char* number);
int out_seat(Plane* p);
Plane* seek_plane(char* number, int n = 0);
};
class userLink
{
private:
user* head;
user* end;
public:
userLink();
~userLink();
void out_user();
void imp_user(planeLink& p);
user* seek_user(char* name, int n = 0);
void delete_user(char* name, planeLink& p);
void delete_alluser(char* number);
void add_user(user& u, planeLink& p);
};
///////////////////////////////////////////////////////////////////////////队
LQueue::LQueue()
{
front = new Node;
rear = front;
front->next = NULL;
}
LQueue::~LQueue()
{
Node* s1, * s2;
for (s1 = front; s1 = NULL; s1 = s2)
{
s2 = s1->next;
delete s1;
}
}
void LQueue::EnQueue(int x)//入队
{
rear->next = new Node;
rear = rear->next;
rear->i = x;
rear->next = NULL;
}
int LQueue::DeQueue()//出队
{
Node* s;
int x;
s = front->next;
x = s->i;
front->next = s->next;
if (s->next == NULL)
rear = front;
delete s;
return x;
}
bool LQueue::Emply()//检查队是否为空
{
if (front == rear)
return 1;
else
return 0;
}
////////////////////////////////////////////////////////////////////////////用户
userLink::userLink()
{
head = new user;
end = head;
head->next = NULL;
}
userLink::~userLink()
{
user* u1, * u2;
for (u1 = head; u1 != NULL; u1 = u2)
{
u2 = u1->next;
delete u1;
}
}
void userLink::imp_user(planeLink& p)
{
user u;
cout << "姓名:";
cin >> u.name;
cout << endl;
cout << "身份证:";
cin >> u.ID;
cout << endl;
cout << "航班号:";
cin >> u.plane_number;
cout << endl;
add_user(u, p);
}
void userLink::out_user()//输出用户信息
{
if (head->next == NULL)
cout << "没有用户!";
user* u1;
for (u1 = head->next; u1 != NULL; u1 = u1->next)
{
cout << "姓名:" << u1->name << endl;
cout << "身份证:" << u1->ID << endl;
cout << "航班:" << u1->plane_number << endl;
cout << "座位号:" << u1->order_number << endl;
cout << endl;
}
}
user* userLink::seek_user(char* name, int n)//查找用户
{
if (head->next == NULL)
{
cout << "没有用户!";
return NULL;
}
user* u1, * u2;
u2 = head;
for (u1 = head->next; u1 != NULL; u1 = u1->next)
{
if (strcmp(u1->name, name) == 0)
break;
u2 = u1;
}
if (n == 0)
{
u1 = u2->next;
cout << "姓名:" << u1->name << endl;
cout << "身份证:" << u1->ID << endl;
cout << "航班:" << u1->plane_number << endl;
cout << "座位号:" << u1->order_number << endl;
cout << endl;
}
else
{
if (u1 != NULL)
return u2;
else
{
cout << "未找此用户!";
return NULL;
}
}
}
void userLink::delete_alluser(char* number)
{
user* u1, * u2;
u2 = head;
u1 = head->next;
while (u1 != NULL)
{
if (strcmp(u1->plane_number, number) == 0)
{
u2->next = u1->next;
delete u1;
u1 = u2->next;
}
else
u1 = u1->next;
}
}
void userLink::delete_user(char* name, planeLink& p)//删除用户
{
if (head->next == NULL)
cout << "没有用户!";
user* u1, * u2;
u1 = seek_user(name, 1);
u2 = u1->next;
p.back_seat(u2->order_number, u2->plane_number);
if (u1 != NULL)
{
u1->next = u2->next;
delete u2;
cout << "删除成功!" << endl;
}
else
cout << "无法删除." << endl;
}
void userLink::add_user(user& u, planeLink& p)//添加用户
{
end->next = new user;
end = end->next;
strcpy_s(end->name, u.name);
strcpy_s(end->ID, u.ID);
strcpy_s(end->plane_number, u.plane_number);
end->order_number = p.out_seat(p.seek_plane(u.plane_number, 1)->next);
end->next = NULL;
}
////////////////////////////////////////////////////////////////////////////飞机
planeLink::planeLink()//建立一个代表头的链表
{
head = new Plane;
end = head;
head->next = NULL;
}
planeLink::~planeLink()//析构
{
Plane* p1, * p2;
for (p1 = head; p1 != NULL; p1 = p2)
{
p2 = p1->next;
delete p1;
}
}
Plane* planeLink::seek_plane(char* number, int n)//查找航班上一个地址
{
if (head->next == NULL)
{
cout << "没有航班!";
return NULL;
}
Plane* p1, * p2;
p2 = head;
for (p1 = head->next; p1 != NULL; p1 = p1->next)
{
if (strcmp(p1->plane_number, number) == 0)
break;
p2 = p1;
}
if (n == 0)
{
p1 = p2->next;
cout << "航班:" << p1->plane_number << endl;
cout << "起飞时间:" << p1->strat_time << endl;
cout << "降落时间:" << p1->end_time << endl;
cout << "起飞城市:" << p1->strat_city << endl;
cout << "降落城市:" << p1->end_city << endl;
cout << "剩余座位:" << p1->empty_seats << endl;
cout << "价格:" << p1->price << endl;
cout << "折扣:" << p1->discount << endl;
cout << endl;
}
else
{
if (p1 != NULL)
return p2;
else
{
cout << "未找到此航班!";
return NULL;
}
}
}
void planeLink::delete_plane(char* number, userLink& u)//删除航班
{
if (head->next == NULL)
cout << "没有航班!";
u.delete_alluser(number);
Plane* p1, * p2;
p1 = seek_plane(number, 1);
p2 = p1->next;
if (p1 != NULL)
{
p1->next = p2->next;
delete p2;
cout << "删除成功!" << endl;
}
else
cout << "无法删除." << endl;
}
void planeLink::imp_plane()//接受飞机信息
{
Plane n;
//system("cls");
cout << "输入飞机信息!" << endl;
cout << " 输入飞机航班:";
cin >> n.plane_number;
cout << endl;
cout << "输入起飞时间:";
cin >> n.strat_time;
cout << endl;
cout << "输入降落时间:";
cin >> n.end_time;
cout << endl;
cout << "输入起城市:";
cin >> n.strat_city;
cout << endl;
cout << "输入降落城市:";
cin >> n.end_city;
cout << endl;
cout << "输入是否满仓:";
cin >> n.full;
cout << endl;
cout << "输入座位:";
cin >> n.empty_seats;
cout << endl;
cout << "输入价格:";
cin >> n.price;
cout << endl;
cout << "输入折扣:";
cin >> n.discount;
cout << endl;
add_plane(n);
}
void planeLink::out_plane()//输出航班信息
{
if (head->next == NULL)
cout << "没有航班!";
Plane* p1;
for (p1 = head->next; p1 != NULL; p1 = p1->next)
{
cout << "航班:" << p1->plane_number << endl;
cout << "起飞时间:" << p1->strat_time << endl;
cout << "降落时间:" << p1->end_time << endl;
cout << "起飞城市:" << p1->strat_city << endl;
cout << "降落城市:" << p1->end_city << endl;
cout << "剩余座位:" << p1->empty_seats << endl;
cout << "价格:" << p1->price << endl;
cout << "折扣:" << p1->discount << endl;
cout << endl;
}
}
void planeLink::back_seat(int x, char* number)//录入退订座位号
{
Plane* p;
p = seek_plane(number, 1)->next;
p->empty_seats++;
p->backseats.EnQueue(x);
p->full = 0;
}
int planeLink::out_seat(Plane* p)//返回座位号
{
p->empty_seats--;
if (p->empty_seats == 0)
{
p->full = 1;
cout << "满仓!";
}
if (p->backseats.Emply())
return p->all_seats - p->empty_seats;
else
return p->backseats.DeQueue();
}
void planeLink::add_plane(Plane& p)//添加航班
{
end->next = new Plane;
end = end->next;
strcpy_s(end->plane_number, p.plane_number);
strcpy_s(end->strat_time, p.strat_time);
strcpy_s(end->end_time, p.end_t