c++实现的停车场管理系统
时间: 2023-10-30 10:02:32 浏览: 68
停车场管理系统是一个典型的应用程序,它需要对停车场进行管理,包括车辆的进出、停车位的分配、计费等。
C++是一种高级编程语言,可以用来实现停车场管理系统。下面是一个简单的停车场管理系统的示例代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
const int MAX_SIZE = 100; // 停车场最大容量
class Car {
public:
string plate_number; // 车牌号
int in_time; // 进入时间(以分钟计)
int out_time; // 出去时间(以分钟计)
int fee; // 停车费用(以元为单位)
Car(string plate_number, int in_time) {
this->plate_number = plate_number;
this->in_time = in_time;
this->out_time = 0;
this->fee = 0;
}
};
class ParkingLot {
private:
Car* cars[MAX_SIZE]; // 车辆数组
int size; // 当前停车场内车辆数
int capacity; // 停车场最大容量
public:
ParkingLot(int capacity) {
this->size = 0;
this->capacity = capacity;
}
bool park(Car* car) {
if (size >= capacity) {
return false; // 停车场已满
}
cars[size++] = car;
return true;
}
bool leave(string plate_number, int out_time) {
for (int i = 0; i < size; i++) {
if (cars[i]->plate_number == plate_number) {
cars[i]->out_time = out_time;
cars[i]->fee = calculateFee(cars[i]->in_time, out_time);
delete cars[i];
for (int j = i + 1; j < size; j++) {
cars[j - 1] = cars[j]; // 压缩车辆数组
}
size--;
return true;
}
}
return false; // 没有找到对应车辆
}
int calculateFee(int in_time, int out_time) {
int duration = out_time - in_time;
int fee = duration / 60 * 5; // 前两小时每小时收费5元
if (duration % 60 > 0) {
fee += 5; // 不足一小时按一小时计费
}
if (fee > 30) {
fee = 30; // 最高收费30元
}
return fee;
}
void display() {
cout << "停车场内共有" << size << "辆车:" << endl;
for (int i = 0; i < size; i++) {
cout << "车牌号:" << cars[i]->plate_number
<< ",进入时间:" << cars[i]->in_time
<< ",出去时间:" << cars[i]->out_time
<< ",停车费用:" << cars[i]->fee << "元" << endl;
}
}
};
int main() {
ParkingLot parkingLot(MAX_SIZE);
Car* car1 = new Car("粤A12345", 120);
Car* car2 = new Car("粤B23456", 180);
Car* car3 = new Car("粤C34567", 240);
parkingLot.park(car1);
parkingLot.park(car2);
parkingLot.park(car3);
parkingLot.display();
parkingLot.leave("粤A12345", 300);
parkingLot.leave("粤C34567", 360);
parkingLot.display();
return 0;
}
```
以上代码中,Car类表示一辆车,包含车牌号、进入时间、出去时间和停车费用等信息。ParkingLot类表示一个停车场,包含车辆数组、当前停车场内车辆数和停车场最大容量等信息,提供了停车、离开、计算停车费用和展示停车场内车辆等功能。主函数中创建了三辆车,并模拟车辆的进出和停车费用计算等操作。
需要注意的是,以上代码只是一个简单的示例,实际的停车场管理系统需要更多的功能和优化,例如考虑并发问题、车位分配算法、收费标准等。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![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_column_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)
![](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)
![](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)