停车场有M个入口,P个出口,停车场共有N个车位,其中每5个车位为一个小型立体车库,先来后到原则进行入场,入场后开始进行计费。若停车场满了之后,门口的车一次排队,出来一辆车之后,放行一辆车再入场。要求:1)每个入口使用队列结构模拟排队场景,排序车辆需要记录排队时间,根据时间先后进场放行。2)每个小型立体车库只用散列表进行模拟入库及出库。3)计费原则:15分钟以下免费,超过15分钟按0.5元/15分钟。小型车夜间停车1元/小时。使用C++设计一段完整代码
时间: 2024-03-26 11:38:06 浏览: 17
好的,下面是一个C++实现的停车场管理系统的完整代码,具体实现细节已在注释中说明:
```c++
#include <iostream>
#include <queue>
#include <unordered_map>
#include <string>
#include <ctime>
using namespace std;
// 定义车辆类
class Car {
public:
string plate_num; // 车牌号
time_t enter_time; // 入场时间
time_t exit_time; // 出场时间
};
// 定义停车场类
class ParkingLot {
public:
int capacity; // 车位数
int small_unit; // 每个小型立体车库的车位数
int num_small; // 小型立体车库的个数
int num_entrance; // 入口数
int num_exit; // 出口数
queue<Car> *entrance_queue; // 入口队列数组
unordered_map<int, Car> *parking_map; // 停车场散列表
ParkingLot(int c, int s, int n, int m, int p) {
capacity = c;
small_unit = s;
num_small = c / s;
num_entrance = m;
num_exit = p;
entrance_queue = new queue<Car>[m];
parking_map = new unordered_map<int, Car>();
}
// 进入停车场
void enter(Car car) {
if (parking_map->size() < capacity) {
// 停车场未满,直接进入停车场
for (int i = 0; i < num_small; i++) {
for (int j = 1; j <= small_unit; j++) {
int pos = i * small_unit + j;
if (parking_map->find(pos) == parking_map->end()) {
// 找到空闲车位,将车辆存入散列表中
parking_map->insert(make_pair(pos, car));
cout << "车辆 " << car.plate_num << " 进入停车场,停在第 " << pos << " 个车位,入场时间为 " << car.enter_time << endl;
return;
}
}
}
} else {
// 停车场已满,加入到入口队列中等待
int min_idx = 0;
time_t min_time = entrance_queue[0].empty() ? time(NULL) : entrance_queue[0].front().enter_time;
for (int i = 1; i < num_entrance; i++) {
time_t t = entrance_queue[i].empty() ? time(NULL) : entrance_queue[i].front().enter_time;
if (t < min_time) {
min_time = t;
min_idx = i;
}
}
entrance_queue[min_idx].push(car);
cout << "车辆 " << car.plate_num << " 进入停车场,停在入口 " << min_idx << " 的排队队列中,入队时间为 " << car.enter_time << endl;
}
}
// 离开停车场
void exit(Car car) {
int pos = -1;
for (auto it = parking_map->begin(); it != parking_map->end(); it++) {
if (it->second.plate_num == car.plate_num) {
pos = it->first;
break;
}
}
if (pos != -1) {
// 找到车辆所在的车位,计算停车费用
time_t diff = car.exit_time - car.enter_time;
if (diff < 900) { // 15分钟以下免费
cout << "车辆 " << car.plate_num << " 停车费用为 0 元" << endl;
} else {
double fee = (diff / 900 + (diff % 900 == 0 ? 0 : 1)) * 0.5;
if (car.exit_time >= time(NULL) + 22 * 3600 || car.exit_time < time(NULL) + 6 * 3600) {
// 夜间停车费用为 1 元/小时
fee += ((car.exit_time - car.enter_time) / 3600 + ((car.exit_time - car.enter_time) % 3600 == 0 ? 0 : 1)) * 1;
}
cout << "车辆 " << car.plate_num << " 停车费用为 " << fee << " 元" << endl;
parking_map->erase(pos);
cout << "车辆 " << car.plate_num << " 离开停车场,出场时间为 " << car.exit_time << endl;
}
} else {
cout << "未找到车辆 " << car.plate_num << endl;
}
}
// 从入口队列中放行车辆
void release() {
for (int i = 0; i < num_entrance; i++) {
if (!entrance_queue[i].empty()) {
Car car = entrance_queue[i].front();
entrance_queue[i].pop();
enter(car);
break;
}
}
}
// 显示停车场的使用情况
void display() {
cout << "停车场使用情况:" << endl;
for (int i = 0; i < num_small; i++) {
for (int j = 1; j <= small_unit; j++) {
int pos = i * small_unit + j;
if (parking_map->find(pos) != parking_map->end()) {
Car car = parking_map->at(pos);
cout << "第 " << pos << " 个车位:车牌号为 " << car.plate_num << ",入场时间为 " << car.enter_time << endl;
} else {
cout << "第 " << pos << " 个车位:空闲" << endl;
}
}
}
}
~ParkingLot() {
delete[] entrance_queue;
delete parking_map;
}
};
int main() {
// 初始化停车场
ParkingLot parking_lot(20, 5, 4, 2, 2);
// 车辆进入停车场
Car car1 = {"京A12345", time(NULL)};
parking_lot.enter(car1);
Car car2 = {"京B67890", time(NULL)};
parking_lot.enter(car2);
Car car3 = {"京C45678", time(NULL)};
parking_lot.enter(car3);
Car car4 = {"京D23456", time(NULL)};
parking_lot.enter(car4);
Car car5 = {"京E78901", time(NULL)};
parking_lot.enter(car5);
// 显示停车场的使用情况
parking_lot.display();
// 车辆离开停车场
car1.exit_time = time(NULL) + 2000;
parking_lot.exit(car1);
// 显示停车场的使用情况
parking_lot.display();
// 车辆进入停车场
Car car6 = {"京F34567", time(NULL)};
parking_lot.enter(car6);
// 显示停车场的使用情况
parking_lot.display();
// 从入口队列中放行车辆
parking_lot.release();
// 显示停车场的使用情况
parking_lot.display();
return 0;
}
```
以上代码中,我们使用了队列和散列表来模拟停车场中车辆的排队和存储情况,同时实现了计费规则和夜间停车费用的计算。在主函数中,我们模拟了车辆进入、离开和放行等情况,并显示了停车场的使用情况。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)