#include iostream#include string#include stack#include queueusing namespace std; 车辆信息结构体struct Car string licensePlate; 车牌号 int hour; 进场小时数 int minute; 进场分钟数; 类模板:停车场templateint Capacityc
/**
- 停车场管理系统
- 实现车辆入场、出场、查询和停车场信息输出的功能
- @author: Your Name
- @date: 2021-xx-xx */
#include
// 车辆信息结构体 struct Car { string licensePlate; // 车牌号 int hour; // 进场小时数 int minute; // 进场分钟数 };
// 类模板:停车场
template
public: // 构造函数 ParkingLot() { parkingCount = 0; }
// 压栈:车辆入场
void push(const Car& car) {
// 检查车辆是否已经在停车场或等待队列中
if (find(car.licensePlate)) {
cout << "车辆 [" << car.licensePlate << "] 已经在停车场或等待队列中." << endl;
return;
}
if (parkingCount < Capacity) {
parkingStack.push(car);
parkingCount++;
cout << "车辆 [" << car.licensePlate << "] 进入停车场." << endl;
} else {
waitList.push(car);
cout << "车辆 [" << car.licensePlate << "] 正在等待列表中等待." << endl;
}
}
// 退栈:车辆出场
bool pop(const string& licensePlate) {
// 在栈中查找车辆
bool found = false;
stack<Car> tempStack;
while (!parkingStack.empty()) {
Car car = parkingStack.top();
parkingStack.pop();
if (car.licensePlate == licensePlate) {
found = true;
cout << "车辆 [" << car.licensePlate << "] 离开了停车场.停车时间: " << car.hour << ":" << car.minute << endl;
break;
}
tempStack.push(car);
}
// 未找到车辆,返回错误
if (!found) {
cout << "车辆 [" << licensePlate << "] 在停车场找不到." << endl;
while (!tempStack.empty()) { // 还原栈
parkingStack.push(tempStack.top());
tempStack.pop();
}
return false;
}
// 更新停车场信息
parkingCount--;
while (!tempStack.empty()) { // 还原栈
parkingStack.push(tempStack.top());
tempStack.pop();
}
// 检查等待队列
if (!waitList.empty()) {
Car car = waitList.front();
waitList.pop();
parkingStack.push(car);
cout << "车辆 [" << car.licensePlate << "] 从等候名单中进入停车场." << endl;
}
return true;
}
// 入队列:车辆到场
void enqueue(const Car& car) {
push(car);
}
// 出队列:车辆出场
bool dequeue() {
if (parkingStack.empty()) {
cout << "停车场为空,无车辆出场." << endl;
return false;
}
Car car = parkingStack.top();
parkingStack.pop();
cout << "车辆 [" << car.licensePlate << "] 离开了停车场.停车时间: " << car.hour << ":" << car.minute << endl;
parkingCount--;
// 检查等待队列
if (!waitList.empty()) {
Car newCar = waitList.front();
waitList.pop();
parkingStack.push(newCar);
cout << "车辆 [" << newCar.licensePlate << "] 从等候名单中进入停车场." << endl;
}
return true;
}
// 查询车辆信息
bool find(const string& licensePlate) {
stack<Car> tempStack = parkingStack;
while (!tempStack.empty()) {
Car car = tempStack.top();
tempStack.pop();
if (car.licensePlate == licensePlate) {
cout << "车辆 [" << car.licensePlate << "] 在停车场." << endl;
return true;
}
}
queue<Car> tempQueue = waitList;
while (!tempQueue.empty()) {
Car car = tempQueue.front();
tempQueue.pop();
if (car.licensePlate == licensePlate) {
cout << "车辆 [" << car.licensePlate << "] 在等候名单中." << endl;
return true;
}
}
cout << "车辆 [" << licensePlate << "] 未找到." << endl;
return false;
}
// 输出停车场信息
void printParkingLot() {
cout << "====停车场====" << endl;
cout << "容量: " << Capacity << endl;
cout << "占用率: " << parkingCount << endl;
if (!parkingStack.empty()) {
cout << "----停车位----" << endl;
stack<Car> tempStack = parkingStack;
while (!tempStack.empty()) {
Car car = tempStack.top();
tempStack.pop();
cout << "[" << car.licensePlate << "] 停车时间: " << car.hour << ":" << car.minute << endl;
}
}
if (!waitList.empty()) {
cout << "----等候名单----" << endl;
queue<Car> tempQueue = waitList;
while (!tempQueue.empty()) {
Car car = tempQueue.front();
tempQueue.pop();
cout << "[" << car.licensePlate << "] 等待" << endl;
}
}
cout << "===================" << endl;
}
};
int main() { ParkingLot<5> parkingLot; // 创建容量为5的停车场
while (true) {
cout << "请选择操作:" << endl;
cout << "1. 车辆入场" << endl;
cout << "2. 车辆出场" << endl;
cout << "3. 查询车辆信息" << endl;
cout << "4. 输出停车场信息" << endl;
cout << "5. 退出程序" << endl;
int choice;
cin >> choice;
if (choice == 1) {
string licensePlate;
int hour, minute;
cout << "请输入车牌号:";
cin >> licensePlate;
cout << "请输入进场时间(小时 分钟):";
cin >> hour >> minute;
Car car;
car.licensePlate = licensePlate;
car.hour = hour;
car.minute = minute;
parkingLot.enqueue(car); // 车辆入场
} else if (choice == 2) {
string licensePlate;
cout << "请输入车牌号:";
cin >> licensePlate;
parkingLot.pop(licensePlate); // 车辆出场
} else if (choice == 3) {
string licensePlate;
cout << "请输入车牌号:";
cin >> licensePlate;
parkingLot.find(licensePlate); // 查询车辆信息
} else if (choice == 4) {
parkingLot.printParkingLot(); // 输出停车场信息
} else if (choice == 5) {
break;
} else {
cout << "无效的选择,请重新输入。" << endl;
}
}
return 0;
原文地址: https://www.cveoy.top/t/topic/htYz 著作权归作者所有。请勿转载和采集!