代码生成(用C++写一个停车收费管理系统)
时间: 2023-11-13 09:04:21 浏览: 30
好的,我可以为您提供一些参考代码,但是需要先了解一下停车收费管理系统的具体需求,比如收费标准、车辆类型、停车位管理等等。以下是一个简单的停车收费管理系统的 C++ 代码示例:
```cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 定义车辆类型
enum VehicleType { CAR, BUS, TRUCK };
// 定义车辆信息类
class Vehicle {
public:
Vehicle(VehicleType type, string licensePlate) : type(type), licensePlate(licensePlate) {}
VehicleType getType() { return type; }
string getLicensePlate() { return licensePlate; }
private:
VehicleType type;
string licensePlate;
};
// 定义停车场类
class ParkingLot {
public:
ParkingLot(int capacity) : capacity(capacity) {}
bool parkVehicle(Vehicle vehicle) {
if (vehicles.size() >= capacity) {
return false; // 停车场已满
}
vehicles.push_back(vehicle);
return true;
}
bool removeVehicle(string licensePlate) {
for (int i = 0; i < vehicles.size(); i++) {
if (vehicles[i].getLicensePlate() == licensePlate) {
vehicles.erase(vehicles.begin() + i);
return true;
}
}
return false; // 没有找到对应车辆
}
int getNumOfVehicles() { return vehicles.size(); }
int getNumOfCars() {
int num = 0;
for (Vehicle vehicle : vehicles) {
if (vehicle.getType() == CAR) {
num++;
}
}
return num;
}
int getNumOfBuses() {
int num = 0;
for (Vehicle vehicle : vehicles) {
if (vehicle.getType() == BUS) {
num++;
}
}
return num;
}
int getNumOfTrucks() {
int num = 0;
for (Vehicle vehicle : vehicles) {
if (vehicle.getType() == TRUCK) {
num++;
}
}
return num;
}
private:
int capacity;
vector<Vehicle> vehicles;
};
// 定义收费标准类
class ParkingFee {
public:
ParkingFee(double carFee, double busFee, double truckFee) : carFee(carFee), busFee(busFee), truckFee(truckFee) {}
double getFee(Vehicle vehicle) {
switch (vehicle.getType()) {
case CAR:
return carFee;
case BUS:
return busFee;
case TRUCK:
return truckFee;
}
}
private:
double carFee;
double busFee;
double truckFee;
};
// 主函数
int main() {
ParkingLot parkingLot(100); // 停车场容量为 100
ParkingFee parkingFee(5, 10, 15); // 收费标准:汽车 5 元/小时,公交车 10 元/小时,卡车 15 元/小时
// 模拟停车过程
Vehicle car(CAR, "粤B12345");
Vehicle bus(BUS, "粤B67890");
Vehicle truck(TRUCK, "粤B24680");
parkingLot.parkVehicle(car);
parkingLot.parkVehicle(bus);
parkingLot.parkVehicle(truck);
// 输出停车场信息
cout << "停车场共有 " << parkingLot.getNumOfVehicles() << " 辆车,其中:" << endl;
cout << "汽车 " << parkingLot.getNumOfCars() << " 辆,公交车 " << parkingLot.getNumOfBuses() << " 辆,卡车 " << parkingLot.getNumOfTrucks() << " 辆。" << endl;
// 模拟离开过程
string licensePlate = "粤B12345";
double duration = 2; // 停车时间为 2 小时
double fee = parkingFee.getFee(car) * duration;
parkingLot.removeVehicle(licensePlate);
cout << "车牌号为 " << licensePlate << " 的车辆停车时间为 " << duration << " 小时,停车费用为 " << fee << " 元。" << endl;
return 0;
}
```
以上代码仅供参考,实际应用中需要根据具体需求进行修改。