用c++设计一个公交车管理系统,公交车需设置不同的类型,每个线路的公交车会经过多个不同的车站,车站、公交车可设计成不同的类,给出某个公交车可以显示它所经过所有站点。 基本功能: (1)公交车要设置不同的类型,如电车、油车等; (2)公交车信息包括:车牌号,类型,线路号,座位数,长度,宽度,高度,油耗等;0 (3)公交车信息要保存在文件中; (4)系统可以实现对公交车和车站信息的添加,修改,删除,查找等功能; (5)系统界面应该尽量友好,方便使用。
时间: 2023-11-14 14:04:22 浏览: 27
以下是一个简单的公交车管理系统的设计,其中包含车站、公交车、线路等类。
```c++
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
// 车站类
class Station {
private:
string name; // 车站名称
public:
Station(string n) : name(n) {}
string getName() const { return name; }
};
// 公交车类
class Bus {
private:
string plateNumber; // 车牌号
string type; // 类型
int routeNumber; // 线路号
int seatCount; // 座位数
double length; // 长度
double width; // 宽度
double height; // 高度
double fuelConsumption; // 油耗
vector<Station> stations; // 经过的车站
public:
Bus(string pn, string t, int rn, int sc, double l, double w, double h, double fc) :
plateNumber(pn), type(t), routeNumber(rn), seatCount(sc), length(l), width(w), height(h),
fuelConsumption(fc) {}
string getPlateNumber() const { return plateNumber; }
string getType() const { return type; }
int getRouteNumber() const { return routeNumber; }
int getSeatCount() const { return seatCount; }
double getLength() const { return length; }
double getWidth() const { return width; }
double getHeight() const { return height; }
double getFuelConsumption() const { return fuelConsumption; }
void addStation(Station& s) { stations.push_back(s); }
void printStations() const {
cout << "Stations on route " << routeNumber << ":" << endl;
for (const auto& s : stations) {
cout << "- " << s.getName() << endl;
}
}
};
// 线路类
class Route {
private:
int routeNumber; // 线路号
vector<Bus> buses; // 该线路上的公交车
public:
Route(int rn) : routeNumber(rn) {}
void addBus(Bus& b) { buses.push_back(b); }
void printBuses() const {
cout << "Buses on route " << routeNumber << ":" << endl;
for (const auto& b : buses) {
cout << "- " << b.getPlateNumber() << " (" << b.getType() << ")" << endl;
}
}
};
// 公交车管理系统类
class BusManagementSystem {
private:
vector<Bus> buses; // 所有公交车
vector<Station> stations; // 所有车站
vector<Route> routes; // 所有线路
public:
void addBus() {
string pn, t;
int rn, sc;
double l, w, h, fc;
cout << "Enter plate number: ";
cin >> pn;
cout << "Enter type: ";
cin >> t;
cout << "Enter route number: ";
cin >> rn;
cout << "Enter seat count: ";
cin >> sc;
cout << "Enter length: ";
cin >> l;
cout << "Enter width: ";
cin >> w;
cout << "Enter height: ";
cin >> h;
cout << "Enter fuel consumption: ";
cin >> fc;
Bus newBus(pn, t, rn, sc, l, w, h, fc);
buses.push_back(newBus);
cout << "New bus added." << endl;
}
void addStation() {
string n;
cout << "Enter station name: ";
cin >> n;
Station newStation(n);
stations.push_back(newStation);
cout << "New station added." << endl;
}
void addRoute() {
int rn;
cout << "Enter route number: ";
cin >> rn;
Route newRoute(rn);
routes.push_back(newRoute);
cout << "New route added." << endl;
}
void addBusToRoute() {
string pn;
int rn;
cout << "Enter plate number of the bus: ";
cin >> pn;
cout << "Enter route number: ";
cin >> rn;
bool busFound = false;
bool routeFound = false;
for (auto& b : buses) {
if (b.getPlateNumber() == pn) {
busFound = true;
for (auto& r : routes) {
if (r.getRouteNumber() == rn) {
routeFound = true;
r.addBus(b);
cout << "Bus " << pn << " added to route " << rn << "." << endl;
break;
}
}
if (!routeFound) {
cout << "Route " << rn << " not found." << endl;
}
break;
}
}
if (!busFound) {
cout << "Bus " << pn << " not found." << endl;
}
}
void addStationToBus() {
string pn;
string sn;
cout << "Enter plate number of the bus: ";
cin >> pn;
cout << "Enter station name: ";
cin >> sn;
bool busFound = false;
bool stationFound = false;
for (auto& b : buses) {
if (b.getPlateNumber() == pn) {
busFound = true;
for (auto& s : stations) {
if (s.getName() == sn) {
stationFound = true;
b.addStation(s);
cout << "Station " << sn << " added to bus " << pn << "." << endl;
break;
}
}
if (!stationFound) {
cout << "Station " << sn << " not found." << endl;
}
break;
}
}
if (!busFound) {
cout << "Bus " << pn << " not found." << endl;
}
}
void printStationsOnRoute() const {
int rn;
cout << "Enter route number: ";
cin >> rn;
bool routeFound = false;
for (const auto& r : routes) {
if (r.getRouteNumber() == rn) {
routeFound = true;
r.printBuses();
cout << endl;
for (const auto& b : r.buses) {
b.printStations();
cout << endl;
}
break;
}
}
if (!routeFound) {
cout << "Route " << rn << " not found." << endl;
}
}
};
int main() {
BusManagementSystem bms;
int choice;
do {
cout << "Bus management system menu:" << endl;
cout << "1. Add bus" << endl;
cout << "2. Add station" << endl;
cout << "3. Add route" << endl;
cout << "4. Add bus to route" << endl;
cout << "5. Add station to bus" << endl;
cout << "6. Print stations on route" << endl;
cout << "0. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
bms.addBus();
break;
case 2:
bms.addStation();
break;
case 3:
bms.addRoute();
break;
case 4:
bms.addBusToRoute();
break;
case 5:
bms.addStationToBus();
break;
case 6:
bms.printStationsOnRoute();
break;
case 0:
cout << "Goodbye!" << endl;
break;
default:
cout << "Invalid choice." << endl;
break;
}
} while (choice != 0);
return 0;
}
```