用c++语言编写一个可运行的代码实现停车场管理系统应用于车辆的出、入管理。
以下是一个简单的停车场管理系统的C++代码示例:
#include <iostream>
#include <vector>
using namespace std;
class Vehicle {
public:
string licensePlate;
string brand;
Vehicle(string licensePlate, string brand) {
this->licensePlate = licensePlate;
this->brand = brand;
}
};
class ParkingLot {
private:
vector<Vehicle> vehicles;
int capacity;
public:
ParkingLot(int capacity) {
this->capacity = capacity;
}
void parkVehicle(Vehicle vehicle) {
if (vehicles.size() < capacity) {
vehicles.push_back(vehicle);
cout << "Vehicle with license plate " << vehicle.licensePlate << " parked successfully." << endl;
} else {
cout << "Parking lot is full. Cannot park vehicle with license plate " << vehicle.licensePlate << endl;
}
}
void removeVehicle(string licensePlate) {
for (int i = 0; i < vehicles.size(); i++) {
if (vehicles[i].licensePlate == licensePlate) {
vehicles.erase(vehicles.begin() + i);
cout << "Vehicle with license plate " << licensePlate << " removed successfully." << endl;
return;
}
}
cout << "Vehicle with license plate " << licensePlate << " not found." << endl;
}
void printVehicles() {
cout << "Vehicles in the parking lot:" << endl;
for (int i = 0; i < vehicles.size(); i++) {
cout << "License Plate: " << vehicles[i].licensePlate << ", Brand: " << vehicles[i].brand << endl;
}
}
};
int main() {
ParkingLot parkingLot(5);
Vehicle vehicle1("ABC123", "Toyota");
Vehicle vehicle2("XYZ789", "Honda");
Vehicle vehicle3("DEF456", "Ford");
parkingLot.parkVehicle(vehicle1);
parkingLot.parkVehicle(vehicle2);
parkingLot.parkVehicle(vehicle3);
parkingLot.printVehicles();
parkingLot.removeVehicle("XYZ789");
parkingLot.printVehicles();
return 0;
}
该代码实现了一个简单的停车场管理系统,使用Vehicle类表示车辆,ParkingLot类表示停车场。停车场有一个容量限制,可以通过parkVehicle方法将车辆停入停车场,通过removeVehicle方法将车辆移出停车场,通过printVehicles方法打印停车场中的车辆信息。
在main函数中,我们创建了一个容量为5的停车场对象parkingLot,并实例化了三个车辆对象。然后,我们依次将这三辆车停入停车场,并打印停车场中的车辆信息。接着,我们移出了一辆车辆,并再次打印停车场中的车辆信息。
注意:这只是一个简单的示例代码,实际的停车场管理系统可能需要更复杂的功能和数据结构。
原文地址: http://www.cveoy.top/t/topic/hCaC 著作权归作者所有。请勿转载和采集!