📄 hangban.cpp
字号:
#include "hangban.h"
int createHB(HANGBAN &t) //新建或增加一个航班信息,返回新建后的信息个数
{
int i=t[0].number;
i++;
cout<<"请输入终点站名:"; cin>>t[i].des; cout<<endl;
cout<<"航班号:"; cin>>t[i].hbh; cout<<endl;
cout<<"飞机号:"; cin>>t[i].fjh; cout<<endl;
cout<<"飞行日期:"; cin>>t[i].date; cout<<endl;
cout<<"飞行周日:"; cin>>t[i].day; cout<<endl;
cout<<"乘员定额:"; cin>>t[i].number; cout<<endl;
cout<<"余票量:"; cin>>t[i].resnum; cout<<endl;
t[0].number=i;
return i;
}
int createHB(HANGBAN &t,ifstream& infile) //从文件中读取信息新件一个航班信息表.返回新建后的信息个数
{
char ch;
char cur1[50];
char cur2[50];
char cur3[50];
char cur4[50];
char cur5[50];
int i=1;
infile.get(cur1,50);
while(infile)
{
t[i].des=cur1;
infile.get(ch);
infile.get(cur2,50);
t[i].hbh=cur2;
infile.get(ch);
infile.get(cur3,50);
t[i].fjh=cur3;
infile.get(ch);
infile.get(cur4,50);
t[i].date=cur4;
infile.get(ch);
infile.get(cur5,50);
t[i].day=cur5;
infile.get(ch);
infile>>t[i].number;
infile.get(ch);
infile>>t[i].resnum;
infile.get(ch);
t[i].L=(LinkList)new LNode;
InitQueue(t[i].queue);
i++;
infile.get(cur1,50);
}
i--;
t[0].number=i;
return i;
}
int printHBnum(HANGBAN t) //显示航班的信息个数,并将它返回
{
cout<<"航班的个数为:"<<t[0].number<<endl;
return t[0].number;
}
void printHB(HANGBAN t) //显示所有航班的信息
{
int i=1;
cout<<"终点站名 | 航班号 | 飞机号 | 飞行日期 | 飞行周日 | 乘员定额 | 余票量"<<endl;
while(i<=t[0].number)
{
cout<<t[i].des<<" "<<t[i].hbh<<" "<<t[i].fjh<<"\t "<<t[i].date<<"\t"<<t[i].day
<<"\t\t"<<t[i].number<<"\t "<<t[i].resnum<<endl;
i++;
}
}
void printHB(HANGBAN t,int i) //显示第i个航班的信息
{
cout<<"终点站名 | 航班号 | 飞机号 | 飞行日期 | 飞行周日 | 乘员定额 | 余票量"<<endl;
cout<<t[i].des<<" "<<t[i].hbh<<" "<<t[i].fjh<<"\t "<<t[i].date<<"\t"<<t[i].day
<<"\t\t"<<t[i].number<<"\t "<<t[i].resnum<<endl;
}
int searchHB(HANGBAN t) //按终点站查找航班。若成功,则返回所以位置i.否则返回0
{
newString cur;
cout<<"请输入终点站名:"; cin>>cur; cout<<endl;
int i=1;
while(i<=t[0].number)
{
if(cur==t[i].des)return i;
i++;
}
cout<<"没有符合的航班"<<endl;
return 0;
}
int searchHB_HBH(HANGBAN t) //按航班号查找航班。若成功,则返回所以位置i.否则返回0
{
newString cur;
cout<<"请输入航班号:"; cin>>cur; cout<<endl;
int i=1;
while(i<=t[0].number)
{
if(cur==t[i].hbh)return i;
else i++;
}
cout<<"没有符合的航班"<<endl;
return 0;
}
void evaluateHB(HANGBAN &t,int i,int j) //将第j各元素的值赋给i
{
t[i].des=t[j].des;
t[i].date=t[j].date;
t[i].hbh=t[j].hbh;
t[i].fjh=t[j].fjh;
t[i].number=t[j].number;
t[i].resnum=t[j].resnum;
t[i].day=t[j].day;
}
int Partition(HANGBAN &t,int low,int high) //快速排序的算法
//交换顺序表他t中子表t[low..high]的纪录,枢轴记录到位,并返回其所在位置,此时
//在它之前(后)记录均不大(小)于它
{
evaluateHB(t,0,low);
newString pivotkey=t[low].date;
while(low<high)
{
while(low<high&&t[high].date>=pivotkey)high--;
evaluateHB(t,low,high);
while(low<high&&t[low].date<=pivotkey)low++;
evaluateHB(t,high,low);
}
evaluateHB(t,low,0);
return low;
}
void HBsort(HANGBAN &t,int low,int high) //对t[low..high]快速排序
{
if(low<high) //长度大于1
{
int middle=Partition(t,low,high); //将t[low..high]一分为二
HBsort(t,low,middle-1); //对低子表递归排序
HBsort(t,middle+1,high); //对高子表递归排序
}
}
void sortHB(HANGBAN &t) //排序。t[0]用作一个中转站
{
int cur=t[0].number;
HBsort(t,1,cur);
t[0].number=cur;
}
void saveHB(HANGBAN t,ofstream& outfile) //保存所有航班的信息
{
int i=1;
while(i<=t[0].number)
{
outfile<<t[i].des<<endl;
outfile<<t[i].hbh<<endl;
outfile<<t[i].fjh<<endl;
outfile<<t[i].date<<endl;
outfile<<t[i].day<<endl;
outfile<<t[i].number<<endl;
outfile<<t[i].resnum<<endl;
i++;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -