4、公交车管理系统问题描述:设计一个公交车管理系统,公交车需设置不同的类型,每个线路的公交车会经过多个不同的车站,车站、公交车可设计成不同的类,给出某个公交车可以显示它所经过所有站点。基本功能:(1)公交车要设置不同的类型,如电车、油车等;(2)公交车信息包括:车牌号,类型,线路号,座位数,长度,宽度,高度,油耗等;(3)公交车信息要保存在文件中;(4)系统可以实现对公交车和车站信息的添加,修改,删除,查找等功能;(5)系统界面应该尽量友好,方便使用。用c++写一个简单的程序代码
时间: 2023-11-17 13:04:50 浏览: 34
好的,以下是一个简单的程序代码:
```c++
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
// 车站类
class Station {
public:
Station(string name, int id) : name(name), id(id) {}
string getName() { return name; }
int getId() { return id; }
private:
string name;
int id;
};
// 公交车类
class Bus {
public:
Bus(string plateNum, string type, int routeNum, int seatNum, int length, int width, int height, int fuelConsumption)
: plateNum(plateNum), type(type), routeNum(routeNum), seatNum(seatNum), length(length), width(width), height(height), fuelConsumption(fuelConsumption) {}
string getPlateNum() { return plateNum; }
string getType() { return type; }
int getRouteNum() { return routeNum; }
int getSeatNum() { return seatNum; }
int getLength() { return length; }
int getWidth() { return width; }
int getHeight() { return height; }
int getFuelConsumption() { return fuelConsumption; }
vector<Station*> getStations() { return stations; }
void addStation(Station* station) { stations.push_back(station); }
private:
string plateNum;
string type;
int routeNum;
int seatNum;
int length;
int width;
int height;
int fuelConsumption;
vector<Station*> stations;
};
// 车辆管理类
class BusManager {
public:
void addBus(Bus* bus) { buses.push_back(bus); }
void removeBus(string plateNum) {
for (auto it = buses.begin(); it != buses.end(); ++it) {
if ((*it)->getPlateNum() == plateNum) {
buses.erase(it);
break;
}
}
}
Bus* findBus(string plateNum) {
for (auto it = buses.begin(); it != buses.end(); ++it) {
if ((*it)->getPlateNum() == plateNum) {
return *it;
}
}
return nullptr;
}
void saveToFile(string filename) {
ofstream fout(filename);
for (auto bus : buses) {
fout << bus->getPlateNum() << " " << bus->getType() << " " << bus->getRouteNum() << " "
<< bus->getSeatNum() << " " << bus->getLength() << " " << bus->getWidth() << " "
<< bus->getHeight() << " " << bus->getFuelConsumption() << " ";
for (auto station : bus->getStations()) {
fout << station->getId() << " ";
}
fout << endl;
}
fout.close();
}
void loadFromFile(string filename) {
ifstream fin(filename);
string plateNum, type;
int routeNum, seatNum, length, width, height, fuelConsumption;
while (fin >> plateNum >> type >> routeNum >> seatNum >> length >> width >> height >> fuelConsumption) {
Bus* bus = new Bus(plateNum, type, routeNum, seatNum, length, width, height, fuelConsumption);
int stationId;
while (fin >> stationId) {
Station* station = stationManager.findStation(stationId);
if (station == nullptr) {
cout << "Error: Station not found!" << endl;
return;
}
bus->addStation(station);
}
addBus(bus);
}
fin.close();
}
private:
vector<Bus*> buses;
};
// 车站管理类
class StationManager {
public:
void addStation(Station* station) { stations.push_back(station); }
void removeStation(int id) {
for (auto it = stations.begin(); it != stations.end(); ++it) {
if ((*it)->getId() == id) {
stations.erase(it);
break;
}
}
}
Station* findStation(int id) {
for (auto it = stations.begin(); it != stations.end(); ++it) {
if ((*it)->getId() == id) {
return *it;
}
}
return nullptr;
}
void saveToFile(string filename) {
ofstream fout(filename);
for (auto station : stations) {
fout << station->getName() << " " << station->getId() << endl;
}
fout.close();
}
void loadFromFile(string filename) {
ifstream fin(filename);
string name;
int id;
while (fin >> name >> id) {
addStation(new Station(name, id));
}
fin.close();
}
private:
vector<Station*> stations;
};
// 界面类
class Interface {
public:
Interface() {
busManager.loadFromFile("buses.txt");
stationManager.loadFromFile("stations.txt");
}
~Interface() {
busManager.saveToFile("buses.txt");
stationManager.saveToFile("stations.txt");
}
void showMenu() {
cout << "========== 公交车管理系统 ==========" << endl;
cout << "1. 添加公交车" << endl;
cout << "2. 删除公交车" << endl;
cout << "3. 查找公交车" << endl;
cout << "4. 添加车站" << endl;
cout << "5. 删除车站" << endl;
cout << "6. 查找车站" << endl;
cout << "7. 退出系统" << endl;
}
void addBus() {
string plateNum, type;
int routeNum, seatNum, length, width, height, fuelConsumption;
cout << "请输入公交车车牌号:";
cin >> plateNum;
if (busManager.findBus(plateNum) != nullptr) {
cout << "错误:该车牌号已存在!" << endl;
return;
}
cout << "请输入公交车类型:";
cin >> type;
cout << "请输入公交车线路号:";
cin >> routeNum;
cout << "请输入公交车座位数:";
cin >> seatNum;
cout << "请输入公交车长度:";
cin >> length;
cout << "请输入公交车宽度:";
cin >> width;
cout << "请输入公交车高度:";
cin >> height;
cout << "请输入公交车油耗:";
cin >> fuelConsumption;
Bus* bus = new Bus(plateNum, type, routeNum, seatNum, length, width, height, fuelConsumption);
while (true) {
int stationId;
cout << "请输入公交车经过的车站编号(输入-1结束):";
cin >> stationId;
if (stationId == -1) {
break;
}
Station* station = stationManager.findStation(stationId);
if (station == nullptr) {
cout << "错误:该车站不存在!" << endl;
continue;
}
bus->addStation(station);
}
busManager.addBus(bus);
cout << "添加成功!" << endl;
}
void removeBus() {
string plateNum;
cout << "请输入要删除的公交车车牌号:";
cin >> plateNum;
busManager.removeBus(plateNum);
cout << "删除成功!" << endl;
}
void findBus() {
string plateNum;
cout << "请输入要查找的公交车车牌号:";
cin >> plateNum;
Bus* bus = busManager.findBus(plateNum);
if (bus == nullptr) {
cout << "未找到该公交车!" << endl;
} else {
cout << "车牌号:" << bus->getPlateNum() << endl;
cout << "类型:" << bus->getType() << endl;
cout << "线路号:" << bus->getRouteNum() << endl;
cout << "座位数:" << bus->getSeatNum() << endl;
cout << "长度:" << bus->getLength() << endl;
cout << "宽度:" << bus->getWidth() << endl;
cout << "高度:" << bus->getHeight() << endl;
cout << "油耗:" << bus->getFuelConsumption() << endl;
cout << "经过的车站:" << endl;
for (auto station : bus->getStations()) {
cout << station->getName() << " ";
}
cout << endl;
}
}
void addStation() {
string name;
int id;
cout << "请输入车站名称:";
cin >> name;
cout << "请输入车站编号:";
cin >> id;
stationManager.addStation(new Station(name, id));
cout << "添加成功!" << endl;
}
void removeStation() {
int id;
cout << "请输入要删除的车站编号:";
cin >> id;
stationManager.removeStation(id);
cout << "删除成功!" << endl;
}
void findStation() {
int id;
cout << "请输入要查找的车站编号:";
cin >> id;
Station* station = stationManager.findStation(id);
if (station == nullptr) {
cout << "未找到该车站!" << endl;
} else {
cout << "车站名称:" << station->getName() << endl;
cout << "车站编号:" << station->getId() << endl;
}
}
private:
BusManager busManager;
StationManager stationManager;
};
int main() {
Interface interface;
while (true) {
interface.showMenu();
int choice;
cout << "请选择操作:";
cin >> choice;
switch (choice) {
case 1:
interface.addBus();
break;
case 2:
interface.removeBus();
break;
case 3:
interface.findBus();
break;
case 4:
interface.addStation();
break;
case 5:
interface.removeStation();
break;
case 6:
interface.findStation();
break;
case 7:
cout << "感谢使用本系统!" << endl;
return 0;
default:
cout << "输入错误,请重新输入!" << endl;
}
}
return 0;
}
```
这个程序并不完整,需要你自己完成文件操作和界面交互部分。同时,你可能需要根据实际需求对类进行修改和扩展。