【C++/数据结构】航班信息的查询与检索

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
//航班号 起点站 终点站 航班期 起飞时间 到达时间 机型 票价
class Flight{
	public:
		char Pname[20];
		char Start[20];
		char End[20];
		char Period[20];
		int Stime;
		int Atime;
		char Pstyle[20];
		int Price;
	friend istream& operator>>(istream& in,Flight& obj){
			cout<<"航班号:";
			in>>obj.Pname;
			cout<<"起点站:";
			in>>obj.Start;
			cout<<"终点站:";
			in>>obj.End;
			cout<<"航班期:";
			in>>obj.Period;
			cout<<"起飞时间:";
			in>>obj.Stime;
			cout<<"到达时间:";
			in>>obj.Atime;			
			cout<<"机型 :";
			in>>obj.Pstyle;
			cout<<"票价:";
			in>>obj.Price;
		}
	friend ostream& operator<<(ostream& out,Flight& obj) {
			out<<"航班号:"<<obj.Pname<<endl;
			out<<"起点站:"<<obj.Start<<endl;
			out<<"终点站:"<<obj.End<<endl;
			out<<"航班期:"<<obj.Period<<endl;
			out<<"起飞时间:"<<obj.Stime<<endl;
			if(obj.Stime>obj.Atime)	
			out<<"到达时间:"<<obj.Atime<<"+1"<<endl;
			else out<<"到达时间:"<<obj.Atime<<endl;				
			out<<"机型 :"<<obj.Pstyle<<endl;
			out<<"票价:"<<obj.Price<<endl;
		}
};
class Plane{
	private:
		Flight f[1000];
		int sum;//当前航班总数 
		int maxsize;//飞机场航班最大数量 
	public:
		Plane(){
			maxsize=1000;
			sum=0;
		}
		bool Insert();
		bool Find();
		bool Sort();
		bool Modify(); 
};

bool Plane::Insert(){
	if(sum>=maxsize){
		cout<<"已满,无法安排新航班\n";
		return false;
	}
	else{
		Flight fnew;
		cin>>fnew;
		f[sum++]=fnew;
		return true;
	}
}
bool Plane::Find(){
	char s[20],e[20];
	cout<<"起点:";cin>>s;
	cout<<"终点:";cin>>e;
	for(int i=0;i<sum;i++){
		if(strcmp(s,f[i].Start)==0&&strcmp(e,f[i].End)==0){
			cout<<f[i];
		}
	}
	return true;
}
int cmp(Flight a,Flight b){
	return a.Stime<b.Stime;
}
bool Plane::Sort(){
	sort(f,f+sum,cmp);
	return true;
}
bool Plane::Modify(){
	char no[20];
	cout<<"输入航班号:";
	Flight fnew;
	cin>>fnew;
	for(int i=0;i<sum;i++){
		if(strcmp(no,f[i].Pname)==0){
		f[i]=fnew;
		return true;
		}
	}
	cout<<"不能找到该航班"<<endl;
	return false;
}

int main(){
	Plane q;
	int n;//选择选项 n=0 退出
	printf("菜单内容:\n\
1、 航班信息录入\n\
2、 航班信息查询\n\
3、 航班信息修改\n\
4、 航班信息排序\n\
5、 结束程序\n");
	cout<<"请输入选项:";
	cin>>n;
	while(n!=6) {
		switch(n) {
			case 1:
				int sn;
				cout<<"输入插入的航班数:";
				cin>>sn;
				while(sn--)q.Insert();
				break;
			case 2:
				q.Find();
				break;
			case 3:
				q.Modify();
				break;
			case 4:
				q.Sort();
				break;
			case 6:
				cout<<"成功退出"<<endl;
				return 0;
			default:
				cout<<"输入选项错误,请核对后输入\n";
		}
		cout<<"本功能已完成,请继续输入:";
		cin>>n;
	}
	return 0;
}

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
1.问题描述 该设计要求对飞机航班信息进行排序和查找。可按航班的航班号、起点站、终点站、起飞时间以及到达时间等信息进行查询。 2.扩展功能: ①能够在起点站与终点站相同的航班信息中筛选出票价最低及飞行时间最短的航班,并显示; ②能够不断添加新的航班信息; ③能够检验输入时间的合法性。 3.任务要求 对于本设计,可采用基数排序法对一组具有结构特点的飞机航班号进行排序,利用二分查找法对排好序的航班记录按航班号实现快速查找,按其他次关键字的查找可采用最简单的顺序查找方法进行,因此他们用得较少。 每个航班记录包括八项,分别是:航班号、起点站、终点站、班期、起飞时间、到达时间、飞机型号以及票价等,假设航班信息表(8条记录) 航班号 起点站 终点站 班期 起飞时间 到达时间 机型 票价 CA1544 合肥 北京 1.2.4.5 1055 1240 733 960 MU5341 上海 广州 每日 1420 1615 M90 1280 CZ3869 重庆 深圳 2.4.6 0855 1035 733 1010 MU3682 桂林 南京 2.3.4.6.7 2050 2215 M90 1380 HU1836 上海 北京 每日 0940 1120 738 1250 CZ3528 成都 厦门 1.3.4.5.7 1510 1650 CRJ 1060 MU4594 昆明 西安 1.3.5.6 1015 1140 328 1160 SC7425 青岛 海口 1.3.6 1920 2120 DH4 1630 其中航班号一项的格式为: K0 K1 K2 K3 K4 K5 C Z 3 8 6 9 其中K0和K1的输入值是航空公司的别称,用两个大写字母标示,后4位为航班号,这种航班号关键字可分成两段,即字母和数字。其余七项输入内容因为不涉及本设计的核心,因此除了票价为数值型外,均定义为字符串即可。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值