请用C++stl航空管理系统
时间: 2023-06-26 14:07:48 浏览: 84
基于c++的航空管理系统
5星 · 资源好评率100%
好的,以下是一个简单的航空管理系统的实现,使用了C++的STL库:
```cpp
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Flight {
public:
Flight(string id, string from, string to, int seats) : m_id(id), m_from(from), m_to(to), m_seats(seats) {}
string getID() const { return m_id; }
string getFrom() const { return m_from; }
string getTo() const { return m_to; }
int getSeats() const { return m_seats; }
private:
string m_id;
string m_from;
string m_to;
int m_seats;
};
class Airline {
public:
Airline(string name) : m_name(name) {}
string getName() const { return m_name; }
const vector<Flight>& getFlights() const { return m_flights; }
void addFlight(const Flight& flight) { m_flights.push_back(flight); }
private:
string m_name;
vector<Flight> m_flights;
};
class Airport {
public:
void addAirline(const Airline& airline) { m_airlines.push_back(airline); }
const vector<Airline>& getAirlines() const { return m_airlines; }
private:
vector<Airline> m_airlines;
};
int main() {
Airport airport;
// 添加航空公司和航班信息
Airline airline1("Airline 1");
airline1.addFlight(Flight("001", "Beijing", "Shanghai", 200));
airline1.addFlight(Flight("002", "Beijing", "Guangzhou", 150));
airline1.addFlight(Flight("003", "Shanghai", "Guangzhou", 180));
airline1.addFlight(Flight("004", "Shanghai", "Beijing", 200));
airport.addAirline(airline1);
Airline airline2("Airline 2");
airline2.addFlight(Flight("101", "Beijing", "Shanghai", 250));
airline2.addFlight(Flight("102", "Beijing", "Guangzhou", 200));
airline2.addFlight(Flight("103", "Shanghai", "Guangzhou", 220));
airline2.addFlight(Flight("104", "Shanghai", "Beijing", 250));
airport.addAirline(airline2);
// 查询航班信息
string from = "Beijing";
string to = "Guangzhou";
for (const Airline& airline : airport.getAirlines()) {
for (const Flight& flight : airline.getFlights()) {
if (flight.getFrom() == from && flight.getTo() == to) {
cout << airline.getName() << " " << flight.getID() << " " << flight.getSeats() << endl;
}
}
}
// 排序航班信息
vector<Flight> allFlights;
for (const Airline& airline : airport.getAirlines()) {
allFlights.insert(allFlights.end(), airline.getFlights().begin(), airline.getFlights().end());
}
sort(allFlights.begin(), allFlights.end(), [](const Flight& a, const Flight& b) { return a.getSeats() < b.getSeats(); });
for (const Flight& flight : allFlights) {
cout << flight.getID() << " " << flight.getFrom() << " " << flight.getTo() << " " << flight.getSeats() << endl;
}
return 0;
}
```
这个系统包含三个类:Flight(航班信息)、Airline(航空公司)和Airport(机场)。一个机场可以拥有多个航空公司,每个航空公司可以有多个航班。
在这个简单的示例中,我们添加了两个航空公司,每个航空公司有四个航班。我们还演示了如何查询从北京到广州的航班信息,以及如何按航班座位数排序所有航班信息。
阅读全文