问题描述
为了便于客户快速查询各航班的详细信息,设计并实现一个系统,可按航班号、起点站、终点站、起飞时间以及到达时间等关键码进行查询。
基本要求
1)每个航班记录至少包括如下几个数据项:航班号、起点站、终点站、起飞时间、到达时间、机型及票价,以.txt格式的文件方式存储;
2)采用高效的排序方法对航班号进行排序;
3)采用高效的查找方法对各航班信息实现快速查询。
问题分析
本项目是以航班信息相关为背景,主要在于可以记录和查询航班信息的数据,包括航班号、起点站、终点站、起飞时间、到达时间、机型及票价这些信息。同时存储数据要求要能够存储在txt文本文件中,并且进行通过C++程序运行时能够进行相关数据的更新。且按照关键字进行查找要考虑时间复杂度情况,因此多数的查询可以按照折半查找的查找方式来进行查找,但是通过折半查找的方式就必须要确保存储的数据是按照以需要查找的信息为关键字进行升序排序,否则折半查找会查找失败,这就要求在进行折半查找之前需要对数据进行排序,排序结束之后再进行查找操作。
设计思路
对于航班信息中的数据航班号、起点站、终点站、机型可以使用string类,起飞时间、到达时间、及票价则使用int类型。
存储和读取数据数据可以通过C++中ofstream方法和ifstream方法来进行,读取数据时需要将txt文本中的数据读取到指针数组中,这里使用二级指针用来存放一级指针,以一级指针来存储上述航班信息中的航班号、起点站、终点站、起飞时间、到达时间、机型及票价,通过对一级指针开辟一定的空间即可将txt文件中所读取的数据存储到其中,再为二级指针开辟空间来存储一级指针,这样就解决了将txt文件数据存储到C++程序中,并且后续方便进行操作。
除了对数据进行读取与储存以外,还有数据的修改,增删,则对当前C++程序中的指针进行操作即可,操作完成之后再通过ofstram方法更新txt文件中得数据即可。除此之外,还需要再类中设置菜单栏函数,以方便调用,减少代码得重复书写。
数据的排序就通过选择排序,快速排序算法对指针进行操作即可。这里可以对航班编号、票价、起飞时间、到达时间进行选择排序,对票价进行快速排序。对于机型、起点站、终点站这里不进行排序,是因为对机型、起点站、终点站进行查找操作时是通过遍历的操作进行查找。
数据查询可以通过折半查找的方法,可以减少程序运行过程中的时间,减少时间复杂度,不过考虑到飞机机型的类型比较少,出现数据重复的情况比较多,且没有规律,所以进行遍历查找的操作,起点站、终点站也使用遍历查找。而对于航班编号、票价、起飞时间、到达时间的查找则选择使用折半查找的方式进行查找,对于航班编号每一趟航班一定是唯一的因此若是折半查找找到所在位置则直接输出即可,不需要判断是否会有重复的数据出现。但是在对其他的数据信息进行查询时不能单单只要有一个输出就可以,航班编号以外的数据不一定是唯一的,因此还需要考虑是否会有重复的情况,将所有需要查询的数据输出。
tips
部分代码有重复,可以额外写函数调用,减少重复,下面的代码没有做此项工作。
代码
#include<iostream>
using namespace std;
#include<cstring>
#include<istream>
#include<ostream>
#include<fstream>
#define FILENAME "Aviation.txt"
class Plane {
public:
Plane(){}//默认构造函数
Plane(string planenumber, string startplace, string endplace, int starttime, int endtime,string planesize,int ticket)//带参构造函数
{
this->EndPlace = endplace;
this->EndTime = endtime;
this->PlaneNumber = planenumber;
this->StartPlace = startplace;
this->StartTime = starttime;
this->PlaneSize = planesize;
this->Ticket = ticket;
}
~Plane(){}//析构函数
string GetPlaneNumber() { return this->PlaneNumber; }//获取PlaneNumber得值
string GetStartPlace() { return this->StartPlace; }//获取StartPlace得值
string GetEndPlace() { return this->EndPlace; }//获取EndPlace得值
int GetStartTime() { return this->StartTime; }//获取StartTime得值
int GetEndTime() { return this->EndTime; }//获取EndTime得值
string GetPlaneSize() { return this->PlaneSize; }//获取PlaneSize得值
int GetTicket() { return this->Ticket; }//获取Ticket得值
void SetPlaneNumber(string pn) { this->PlaneNumber = pn; }//修改PlaneNumber得值
void SetStartPlace(string sp) { this->StartPlace = sp; }//修改StartPlace得值
void SetEndPlace(string ep) { this->EndPlace = ep; }//修改Endplace得值
void SetStartTime(int st) { this->StartTime = st; }//修改StartTime得值
void SetENdTime(int et) { this->EndTime = et; }//修改EndTime得值
void SetPlaneSize(string ps) { this->PlaneSize = ps; };//修改PlaneSize的值
void SetTicket(int t) { this->Ticket = t; };//修改Ticket的值
void ShowInformation()//显示信息
{
cout << "航空编号为:" << this->PlaneNumber<<"\t";
cout << "飞机起飞地点为:" << this->StartPlace<<"\t";
cout << "航空终点为:" << this->EndPlace<<"\t";
cout << "飞机起飞时间为:" << this->StartTime<<"\t";
cout << "飞机降落时间为:" << this->EndTime << "\t";
cout << "飞机机型为:" << this->PlaneSize << "\t";
cout << "飞机票价" << this->Ticket << "\n";
}
private:
string PlaneNumber;//飞机班号
string StartPlace;//起始地点
string EndPlace;//终点
string PlaneSize;//机型
int StartTime;//起飞时间
int EndTime;//落地时间
int Ticket;//票价
};
class Aviation
{
public:
Aviation();//构造函数
~Aviation();//析构函数
int Get_ArryNum();//获取一共有几条记录
void Inti_Arry();//将文件中的数据存到数组中
void NumberSame(string pn);//防止编号相同
void Show_Menu();//菜单栏显示
void Exit_System();//退出系统
void AddInformation();//添加信息
void SaveInformationToFile();//将数组中的信息存到文件中
void ShowInformation();//显示信息
void DeleteInformation();//删除信息
int Is_Exist(string pn);//按照编号查找,返回其位置
void ALterInformation();//修改航空信息
void Arry_Find();//查找
void CleanFIle();//清空文件
void SortArry();//排序
int Is_ExistLine(string pn);//航班编号查找,折半查找
void QuickSort(int first, int last);//快速排序
int Partition(int first, int last);//一次划分
void SortStartTime();//选择排序排出发时间
void SortEndTime();//选择排序排落地时间
int LineSearchTicket(int t);//折半查找票价
int LineSearcchStartTime(int st);//折半查找出发
int LineSearchEndTime(int et);//折半查找终点
private:
bool m_FileIsEmpty;//判断文件是否为空,或者不存在
Plane** Arry;//存放记录的数组,二级指针,存放Plane的这样一级指针,存数据
int AllSum;//总共有几条记录
};
int main()
{
Aviation a;
int chose = 0;
while(1)
{
a.Show_Menu();
cin >> chose;
switch (chose)
{
case 0:
{
a.Exit_System();
break;
}
case 1:
{
a.AddInformation();
break;
}
case 2:
{
std::system("pause");
std::system("cls");
a.ShowInformation();
break;
}
case 3:
{
a.DeleteInformation();
break;
}
case 4:
{
a.ALterInformation();
break;
}
case 5:
{
a.Arry_Find();
break;
}
case 6:
{
a.SortArry();
break;
}
case 7:
{
a.CleanFIle();
break;
}
default:
{
cout << "输入错误!" << endl;
break;
}
}
std::system("pause");
std::system("cls");
}
return 0;
}
Aviation::Aviation()//构造函数
{
ifstream ifs;
ifs.open(FILENAME, ios::in);//读文件
if (!ifs.is_open())
{
cout << "文件不存在" << endl;
//初始化属性,初始化记录人数
this->AllSum = 0;
//初始化数组指针
this->Arry = nullptr;
//初始化文件是否为空
this->m_FileIsEmpty = true;
ifs.close();
return;
}
char ch;
ifs >> ch;
if (ifs.eof())
{
//文件为空,初始化属性,初始化记录人数
this->AllSum = 0;
//初始化数组指针
this->Arry = nullptr;
//初始化文件是否为空
this->m_FileIsEmpty = true;
ifs.close();
return;
}
//文件存在,并且有记录
int num = this->Get_ArryNum();
cout << "文件内共有" << num << "条记录" << endl;
this->AllSum = num;
//开辟空间
this->Arry = new Plane*[this->AllSum];
//将文件中的数据存入数组中
this->Inti_Arry();
}
Aviation::~Aviation()//析构函数
{
if (this->Arry)
{
delete[]this->Arry;
this->Arry = nullptr;
}
}
int Aviation::Get_ArryNum()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);//打开文件,读
if (!ifs.is_open())
return 0;
string planenumber, startplace, endplace, planesize;
int starttime, endtime, ticket;
int sum = 0;
while (ifs >> planenumber && ifs >> startplace && ifs >> endplace && ifs >> starttime && ifs >> endtime && ifs >> planesize && ifs >> ticket)
sum++;
return sum;
}
void Aviation::Inti_Arry()//初始化航空信息到数组中去
{
ifstream ifs;
ifs.open(FILENAME,ios::in);
string planenumber, startplace, endplace,planesize;
int starttime, endtime,ticket;
int index = 0;
while (ifs >> planenumber && ifs >> startplace && ifs >> endplace && ifs >> starttime && ifs >> endtime && ifs >> planesize && ifs >> ticket)
{
Plane* p = new Plane(planenumber, startplace, endplace, starttime, endtime, planesize, ticket);
this->Arry[index] = p;
index++;
//注意在构造函数中AllSum已经初始化过了,在这里不用再搞了,而且这个函数是被构造函数调用的
}
ifs.close();
}
void Aviation::NumberSame(string pn)//防止编号相同
{
if (this->AllSum > 0)
{
for (int i = 0; i < this->AllSum; i++)
{
if (this->Arry[i]->GetPlaneNumber() == pn)
{
bool x = true;
cout << "此编号已经存在了,您是否搞错了,请重新输入一个编号,或者您可以选择结束输入!" << endl;
cout << "结束输入请按:0\t继续输入请按:1" << endl;
cin >> x;
if (x)
{
cin >> pn;
this->NumberSame(pn);//再次查找是否有相同的编号
}
}
}
}
}
void Aviation::Show_Menu()//菜单栏显示
{
cout << "***************************************************" << endl;
cout << "******************欢迎来到航空信息管理系统*********" << endl;
cout << "******************0.退出航空信息管理系统***********" << endl;
cout << "******************1.添加航空信息*******************" << endl;
cout << "******************2.显示航空信息*******************" << endl;
cout << "******************3.删除航空信息*******************" << endl;
cout << "******************4.修改航空信息*******************" << endl;
cout << "******************5.查找航空信息*******************" << endl;
cout << "******************6.按航空票价排序*****************" << endl;
cout << "******************7.清空航空信息*******************" << endl;
cout << "***************************************************" << endl;
}
void Aviation::Exit_System()//退出系统
{
std::system("cls");
cout << "欢迎下次实验航空信息系统!" << endl;
std::system("pause");
exit(0);//用于推出系统的函数
}
void Aviation::AddInformation()//添加信息
{
std::system("pause");
std::system("cls");
cout << "请输入您要添加的航空信息的个数:" << endl;
int addnum = 0;
cin >> addnum;
if (addnum > 0)
{
//计算新添加的新的空间的大小
//新空间的大小=原来记录的数据条数+新增添的航空信息条数
int newsize = this->AllSum + addnum;
//开辟新空间
Plane** newspace = new Plane * [newsize];
//将原来的数据拷贝到新的空间下
if (this->Arry)
for (int i = 0; i < this->AllSum; i++)
newspace[i] = this->Arry[i];
//开始添加数据
for (int i = 0; i < addnum; i++)
{
string planenumber, startplace, endplace, planesize;
int starttime, endtime, ticket;
cout << "请输入添加的第" << i + 1 << "个航空信息的航空编号" << endl;
cin >> planenumber;
this->NumberSame(planenumber);//防止重复
cout << "请输入添加的第" << i + 1 << "个航空信息的起始地点" << endl;
cin >> startplace;
cout << "请输入添加的第" << i + 1 << "个航空信息的航空终点" << endl;
cin >> endplace;
cout << "请输入添加的第" << i + 1 << "个航空信息的起飞时间" << endl;
cin >> starttime;
cout << "请输入添加的第" << i + 1 << "个航空信息的落地时间" << endl;
cin >> endtime;
cout << "请输入添加的第" << i + 1 << "个航空信息的飞机机型" << endl;
cin >> planesize;
cout << "请输入添加的第" << i + 1 << "个航空信息的飞机票价" << endl;
cin >> ticket;
//创建数据,并存入新空间内
Plane* p = new Plane(planenumber, startplace, endplace, starttime, endtime, planesize, ticket);
newspace[this->AllSum + i] = p;
}
//释放原来的空间
delete[]this->Arry;
//更新空间指向
this->Arry = newspace;
//更新信息的个数
this->AllSum = newsize;
//成功添加后保存到文件中去
this->SaveInformationToFile();
//更新信息不为空的标志
this->m_FileIsEmpty = false;
//提示信息添加成功
cout << "成功添加" << addnum << "条航空信息!" << endl;
}
else
cout << "输入的添加个数小于0,信息有误!" << endl;
std::system("pause");
std::system("cls");
}
void Aviation::SaveInformationToFile()
{
ofstream ofs;
ofs.open(FILENAME, ios::out);//写文件
//将每个航空信息写入文件中
for (int i = 0; i < this->AllSum; i++)
{
//开始写入
ofs << this->Arry[i]->GetPlaneNumber() << "\t" << this->Arry[i]->GetStartPlace() << "\t"
<< this->Arry[i]->GetEndPlace() << "\t" << this->Arry[i]->GetStartTime() << "\t"
<< this->Arry[i]->GetEndTime() << "\t" << this->Arry[i]->GetPlaneSize() << "\t"
<< this->Arry[i]->GetTicket() << endl;
}
ofs.close();//需要关闭文件,保存
}
void Aviation::ShowInformation()//显示信息
{
if (this->m_FileIsEmpty)
cout << "文件不存在,或者为空!" << endl;
else
for (int i = 0; i < this->AllSum; i++)
this->Arry[i]->ShowInformation();
}
void Aviation::DeleteInformation()//删除信息,覆盖即可
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在,或者为空!" << endl;
std::system("pause");
std::system("cls");
return;
}
cout << "请输入您想要删除的航空信息的航空编号:" << endl;
string pn;
cin >> pn;
int pos = this->Is_Exist(pn);
if (pos >= 0)
{
for (int i = pos; i < this->AllSum; i++)//覆盖即可,即数据前移
this->Arry[i] = this->Arry[i + 1];
this->AllSum--;//更新信息的条数
delete this->Arry[this->AllSum];
this->SaveInformationToFile();
cout << "删除成功!" << endl;
}
else
cout << "此航空编号在文件中不存在!" << endl;
std::system("pause");
std::system("cls");
}
int Aviation::Is_Exist(string pn)
{
for (int i = 0; i < this->AllSum; i++)
if (this->Arry[i]->GetPlaneNumber() == pn)
return i;
return -1;
}
void Aviation::ALterInformation()//修改航空信息
{
if (this->m_FileIsEmpty)
cout << "文件不存在,或者为空!" << endl;
else
{
cout << "请输入要修改的航空信息的航空编号:" << endl;
string pn;
int pos = this->Is_Exist(pn);//找到位置
if (pos >= 0)
{
delete this->Arry[pos];
cout << "查找到了编号为" << pn << "的航空信息" << endl;
string newpn, newsp, newep, newps;
int newst, newet, newt;
cout << "请输入修改后航空信息的航空编号" << endl;
cin >> newpn;
this->NumberSame(newpn);//防止重复
cout << "请输入修改后航空信息的起始地点" << endl;
cin >> newsp;
cout << "请输入修改后航空信息的航空终点" << endl;
cin >> newep;
cout << "请输入修改后航空信息的起飞时间" << endl;
cin >> newst;
cout << "请输入修改后航空信息的落地时间" << endl;
cin >> newet;
cout << "请输入修改后航空信息的飞机机型" << endl;
cin >> newps;
cout << "请输入修改后航空信息的飞机票价" << endl;
cin >> newt;
Plane* p = new Plane(newpn, newsp, newep, newst, newet, newps, newt);
this->Arry[pos] = p;//更新数据
cout << "修改成功" << endl;
this->SaveInformationToFile();
}
else
cout << "文件中没有此信息!" << endl;
}
std::system("pause");
std::system("cls");
}
void Aviation::Arry_Find()//查找
{
if (this->m_FileIsEmpty)
cout << "文件为空,或者不存在!" << endl;
else
{
bool x = true;
//给出选择,两种方式
while (x)
{
cout << "请输入查找的方式:" << endl;
cout << "1.按航空编号查找" << endl;
cout << "2.按飞机机型查找" << endl;
cout << "3.按飞机票价查找" << endl;
cout << "4.按起点站查找" << endl;
cout << "5.按终点站查找" << endl;
cout << "6.按飞机起飞时间查找" << endl;
cout << "7.按飞机落地时间查找" << endl;
int chose = 0;
cin >> chose;
switch (chose)
{
case 1:
{
string pn;
cout << "请输入要查找的航空编号:" << endl;
cin >> pn;
//这里使用选择排序的方法
for (int i = 0; i < this->AllSum; i++)
{
int pos = i;
for (int j = i + 1; j < this->AllSum; j++)
{
//升序排序
if (this->Arry[pos]->GetPlaneNumber() > this->Arry[j]->GetPlaneNumber())
pos = j;
}
if (i != pos)//交换
{
Plane* temp = this->Arry[i];
this->Arry[i] = this->Arry[pos];
this->Arry[pos] = temp;
}
}
int pos = this->Is_ExistLine(pn);//开始查找
if (pos >= 0)
{
cout << "在文件中查到此航空编号的信息!其信息如下:" << endl;
this->Arry[pos]->ShowInformation();//显示信息
}
else
cout << "查找失败,文件中并无此航空编号的信息!" << endl;
x = false;
break;
}
case 2:
{
string ps;
cout << "请输入您要查找的飞机型号:" << endl;
cin >> ps;
bool flag = false;//查找标志
bool say = false;
for(int i=0;i<this->AllSum;i++)
if (this->Arry[i]->GetPlaneSize() == ps)
{
if(!say)//使下面中文只输出一次
{
cout << "查找成功,已查找到此机型的信息!信息如下:" << endl;
say = true;
}
this->Arry[i]->ShowInformation();
flag = true;
}
if (!flag)
cout << "查无信息!" << endl;
x = false;
break;
}
case 3:
{
this->QuickSort(0, this->AllSum - 1);//快速排序
int ticket = 0;
cout << "请输入要查找的飞机票价:" << endl;
cin >> ticket;
int pos = this->LineSearchTicket(ticket);//折半查找
if (pos >= 0)
{
cout << "查找成功,信息如下:" << endl;
this->Arry[pos]->ShowInformation();
if (pos - 1 >= 0)//防止有重复的票价
{
int temp = pos-1;
bool flag = true;
while (flag&&temp>=0)
{
if (this->Arry[temp]->GetTicket() == ticket)
this->Arry[temp]->ShowInformation();
else
flag = false;
temp--;
}
}
if (pos + 1 <= this->AllSum-1)//防止有重复的票价
{
int temp = pos + 1;
bool flag = true;
while (flag && temp < this->AllSum)
{
if (this->Arry[temp]->GetTicket() == ticket)
this->Arry[temp]->ShowInformation();
else
flag = false;
temp++;
}
}
}
else
cout << "查无信息!" << endl;
x = false;
break;
}
case 4:
{
string sp;
cout << "请输入要查找的飞机的起点:" << endl;
cin >> sp;
bool flag = false;//查找标志
bool say = false;
for(int i=0;i<this->AllSum;i++)
if (this->Arry[i]->GetStartPlace() == sp)
{
if (!say)//这里时为了防止有相同重复输出下面查找成功语句
{
cout << "查找成功,已查找到此信息!信息如下:" << endl;
say = true;
}
this->Arry[i]->ShowInformation();
flag = true;
}
if (!flag)
cout << "查无信息!" << endl;
x = false;
break;
}
case 5:
{
string ep;
cout << "请输入要查找的飞机的终点:" << endl;
cin >> ep;
bool flag = false;//查找标志
bool say = false;
for (int i = 0; i < this->AllSum; i++)
if (this->Arry[i]->GetEndPlace() == ep)
{
if (!say)//这里时为了防止有相同重复输出下面查找成功语句
{
cout << "查找成功,已查找到此终点的信息!信息如下:" << endl;
say = true;
}
this->Arry[i]->ShowInformation();
flag = true;
}
if (!flag)
cout << "查无信息!" << endl;
x = false;
break;
}
case 6:
{
int st = 0;
cout << "请输入要查找的飞机的起飞的时间:" << endl;
cin >> st;
this->SortStartTime();
int pos = this->LineSearcchStartTime(st);
if (pos >= 0)
{
cout << "查找成功,信息如下:" << endl;
this->Arry[pos]->ShowInformation();
if (pos - 1 >= 0)//防止有重复的票价
{
int temp = pos - 1;
bool flag = true;
while (flag && temp >= 0)
{
if (this->Arry[temp]->GetStartTime() == st)
this->Arry[temp]->ShowInformation();
else
flag = false;
temp--;
}
}
if (pos + 1 <= this->AllSum - 1)//防止有重复的票价
{
int temp = pos + 1;
bool flag = true;
while (flag && temp < this->AllSum)
{
if (this->Arry[temp]->GetStartTime() == st)
this->Arry[temp]->ShowInformation();
else
flag = false;
temp++;
}
}
}
else
cout << "查无信息!" << endl;
x = false;
break;
}
case 7:
{
int et = 0;
cout << "请输入要查找的飞机的降落的时间:" << endl;
cin >> et;
this->SortEndTime();
int pos = this->LineSearchEndTime(et);
if (pos >= 0)
{
cout << "查找成功,信息如下:" << endl;
this->Arry[pos]->ShowInformation();
if (pos - 1 >= 0)//防止有重复的票价
{
int temp = pos - 1;
bool flag = true;
while (flag && temp >= 0)
{
if (this->Arry[temp]->GetEndTime() == et)
this->Arry[temp]->ShowInformation();
else
flag = false;
temp--;
}
}
if (pos + 1 <= this->AllSum - 1)//防止有重复的票价
{
int temp = pos + 1;
bool flag = true;
while (flag && temp < this->AllSum)
{
if (this->Arry[temp]->GetEndTime() == et)
this->Arry[temp]->ShowInformation();
else
flag = false;
temp++;
}
}
}
else
cout << "查无信息!" << endl;
x = false;
break;
}
default:
cout << "输入错误!" << endl;
x = false;
break;
}
}
}
std::system("pause");
std::system("cls");
}
void Aviation::CleanFIle()//清空文件
{
cout << "确认清空文件?" << endl;
cout << "1.确认" << endl;
cout << "2.返回" << endl;
int chose = 0;
cin >> chose;
if (chose == 1)
{
ofstream ofs;
//打开方式,ios::trunc如果存在文件,删除并且重新创建
ofs.open(FILENAME, ios::trunc);
ofs.close();
if (this->m_FileIsEmpty)
{
for (int i = 0; i < this->AllSum; i++)
if (this->Arry[i])
delete this->Arry[i];
this->AllSum = 0;
delete this->Arry;
this->Arry = nullptr;
this->m_FileIsEmpty = true;
}
cout << "清除成功!" << endl;
}
system("pause");
system("cls");
}
void Aviation::SortArry()
{
if (this->m_FileIsEmpty)
cout << "文件为空!" << endl;
else
{
int chose = 0;
while (chose < 1 || chose>2)
{
cout << "请选择升序排序还是降序排序:" << endl;
cout << "1.按照票价进行升序排序" << endl;
cout << "2.按照票价进行降序排序" << endl;
cin >> chose;
if (chose < 1 || chose>2)
cout << "输入错误,请重新输入!" << endl;
}
//这里使用选择排序的方法
for (int i = 0; i < this->AllSum; i++)
{
int pos = i;
for (int j = i + 1; j < this->AllSum; j++)
{
if (chose == 1)//升序排序
if (this->Arry[pos]->GetTicket() > this->Arry[j]->GetTicket())
pos = j;
if (chose == 2)//降序排序
if (this->Arry[pos]->GetTicket() < this->Arry[j]->GetTicket())
pos = j;
}
if (i != pos)//交换
{
Plane* temp = this->Arry[i];
this->Arry[i] = this->Arry[pos];
this->Arry[pos] = temp;
}
}
}
cout << "排序成功,排序后的结果如下:" << endl;
this->SaveInformationToFile();
this->ShowInformation();
}
int Aviation::Is_ExistLine(string pn)
{
int mid, low = 0, high = this->AllSum - 1;
while (low <= high)
{
mid = (low + high) / 2;
if (pn < this->Arry[mid]->GetPlaneNumber())high = mid - 1;
else if (pn > this->Arry[mid]->GetPlaneNumber())low = mid + 1;
else return mid;
}
return -1;
}
void Aviation::QuickSort(int first, int last)//快速排序票价
{
if (first >= last)
{
return;
}
else
{
int pivot = Partition(first, last);//一次划分
this->QuickSort(first, pivot - 1);//左侧快速排序
this->QuickSort(pivot + 1, last);//右侧快速排序
}
}
int Aviation::Partition(int first, int last)
{
int i = first, j = last;//初始化一次划分的区间
Plane* temp=new Plane;
while (i < j)
{
while (i < j && this->Arry[i]->GetTicket() <= this->Arry[j]->GetTicket())j--;//右侧扫描
if (i < j)
{
temp = this->Arry[i];
this->Arry[i] = this->Arry[j];
this->Arry[j] = temp;
i++;
}
while (i < j && this->Arry[i]->GetTicket() <= this->Arry[j]->GetTicket())i++;//左侧扫描
if (i < j)
{
temp = this->Arry[i];
this->Arry[i] = this->Arry[j];
this->Arry[j] = temp;
j--;
}
}
return i;
}
void Aviation::SortStartTime()
{
//这里使用选择排序的方法
for (int i = 0; i < this->AllSum; i++)
{
int pos = i;
for (int j = i + 1; j < this->AllSum; j++)
{
//升序 排序
if (this->Arry[pos]->GetStartTime() > this->Arry[j]->GetStartTime())
pos = j;
}
if (i != pos)//交换
{
Plane* temp = this->Arry[i];
this->Arry[i] = this->Arry[pos];
this->Arry[pos] = temp;
}
}
}
void Aviation::SortEndTime()
{
//这里使用选择排序的方法
for (int i = 0; i < this->AllSum; i++)
{
int pos = i;
for (int j = i + 1; j < this->AllSum; j++)
{
//升序 排序
if (this->Arry[pos]->GetEndTime() > this->Arry[j]->GetEndTime())
pos = j;
}
if (i != pos)//交换
{
Plane* temp = this->Arry[i];
this->Arry[i] = this->Arry[pos];
this->Arry[pos] = temp;
}
}
}
int Aviation::LineSearchTicket(int t)
{
int low = 0, high = this->AllSum - 1,mid;
while(low<=high)
{
mid = (low + high) / 2;
if (t < this->Arry[mid]->GetTicket())high = mid - 1;
else if (t > this->Arry[mid]->GetTicket())low = mid + 1;
else return mid;
}
return -1;
}
int Aviation::LineSearcchStartTime(int st)//折半查找起始时间
{
int low = 0, high = this->AllSum - 1, mid;
while (low <= high)
{
mid = (low + high) / 2;
if (st < this->Arry[mid]->GetStartTime())high = mid - 1;
else if (st > this->Arry[mid]->GetStartTime())low = mid + 1;
else return mid;
}
return -1;
}
int Aviation::LineSearchEndTime(int et)//折半查找终点时间
{
int low = 0, high = this->AllSum - 1, mid;
while (low <= high)
{
mid = (low + high) / 2;
if (et < this->Arry[mid]->GetEndTime())high = mid - 1;
else if (et > this->Arry[mid]->GetEndTime())low = mid + 1;
else return mid;
}
return -1;
}