停车场管理系统c++代码实现并加以说明
时间: 2023-08-05 14:08:48 浏览: 59
停车场管理系统是一种常见的应用程序,它可以帮助停车场的管理者更好地管理停车场资源,提高停车场的使用效率。下面是一个简单的停车场管理系统的C++代码实现。
首先,我们需要定义一些数据结构来表示停车场的状态和车辆信息:
```c++
struct Car {
string license; // 车牌号
int type; // 车辆类型,1:小车,2:大车
time_t enter_time; // 进入停车场的时间戳
};
struct ParkingLot {
int total_space; // 停车场总车位数
int small_space; // 小车位数
int big_space; // 大车位数
int small_count; // 小车数量
int big_count; // 大车数量
vector<Car> cars; // 当前停车场内的车辆信息
};
```
接下来,我们需要实现一些基本的功能,例如车辆进入停车场、车辆离开停车场、查询停车场状态等:
```c++
void enter_parking_lot(ParkingLot& pl, Car& car) {
if (pl.small_count + pl.big_count >= pl.total_space) {
cout << "停车场已满,无法进入!" << endl;
return;
}
if (car.type == 1 && pl.small_count >= pl.small_space) {
cout << "小车位已满,无法进入!" << endl;
return;
}
if (car.type == 2 && pl.big_count >= pl.big_space) {
cout << "大车位已满,无法进入!" << endl;
return;
}
car.enter_time = time(NULL); // 记录进入时间
pl.cars.push_back(car); // 添加车辆信息
if (car.type == 1) {
pl.small_count++;
} else {
pl.big_count++;
}
cout << "车辆 " << car.license << " 进入停车场!" << endl;
}
void leave_parking_lot(ParkingLot& pl, string license) {
for (int i = 0; i < pl.cars.size(); i++) {
if (pl.cars[i].license == license) {
int type = pl.cars[i].type;
time_t enter_time = pl.cars[i].enter_time;
int duration = time(NULL) - enter_time; // 计算停车时间
pl.cars.erase(pl.cars.begin() + i); // 删除车辆信息
if (type == 1) {
pl.small_count--;
} else {
pl.big_count--;
}
cout << "车辆 " << license << " 离开停车场!" << endl;
cout << "停车时间:" << duration << " 秒" << endl;
return;
}
}
cout << "停车场内没有该车辆!" << endl;
}
void query_parking_lot(ParkingLot& pl) {
cout << "停车场状态:" << endl;
cout << "总车位数:" << pl.total_space << endl;
cout << "小车位数:" << pl.small_space << endl;
cout << "大车位数:" << pl.big_space << endl;
cout << "当前停车数量:" << pl.small_count + pl.big_count << endl;
cout << "小车数量:" << pl.small_count << endl;
cout << "大车数量:" << pl.big_count << endl;
}
```
最后,我们可以编写一个简单的主函数来测试停车场管理系统的功能:
```c++
int main() {
ParkingLot pl = {50, 40, 10, 0, 0, vector<Car>()};
Car car1 = {"京A12345", 1, 0};
enter_parking_lot(pl, car1);
Car car2 = {"京B67890", 2, 0};
enter_parking_lot(pl, car2);
query_parking_lot(pl);
leave_parking_lot(pl, "京A12345");
query_parking_lot(pl);
return 0;
}
```
以上是一个简单的停车场管理系统的C++代码实现,可以根据具体需求进行扩展和修改。