数据结构课设停车场管理
时间: 2024-01-08 11:04:01 浏览: 110
数据结构课程设计停车场管理
停车场管理系统是一个常见的数据结构课程设计项目,它主要涉及到栈和队列的应用。在这个项目中,我们使用C++编程语言来实现停车场管理系统。
停车场管理系统的基本要求是使用栈来模拟停车场,使用队列来模拟车场外的便道。系统通过从终端读入输入数据序列来进行模拟管理。每组输入数据包括三个数据项:汽车的到达或离去信息、车牌号以及到达或离开时间。根据每组输入数据的操作,系统会输出相应的信息:如果是车辆到达,则输出汽车在停车场内或便道上的停车位置;如果是车辆离开,则输出汽车在停车场内停留的时间和应缴纳的费用。
在这个项目中,我们可以使用顺序结构来实现栈,使用链表结构来实现队列。栈用于管理停车场内的车辆,而队列用于管理车场外的便道上的车辆。
以下是一个简单的示例代码,演示了如何使用栈和队列来实现停车场管理系统:
```cpp
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
struct Car {
string licensePlate;
int arrivalTime;
};
void parkCar(stack<Car>& parkingLot, queue<Car>& waitingQueue, Car car) {
if (parkingLot.size() < 5) {
parkingLot.push(car);
cout << "Car " << car.licensePlate << " is parked at position " << parkingLot.size() << " in the parking lot." << endl;
} else {
waitingQueue.push(car);
cout << "Car " << car.licensePlate << " is parked in the waiting queue." << endl;
}
}
void removeCar(stack<Car>& parkingLot, queue<Car>& waitingQueue, string licensePlate, int departureTime) {
bool found = false;
int position = 0;
// Check if the car is in the parking lot
stack<Car> tempStack;
while (!parkingLot.empty()) {
Car car = parkingLot.top();
parkingLot.pop();
position++;
if (car.licensePlate == licensePlate) {
found = true;
int stayTime = departureTime - car.arrivalTime;
int fee = stayTime * 10; // Assume the fee is 10 per hour
cout << "Car " << car.licensePlate << " stayed in the parking lot for " << stayTime << " hours and needs to pay " << fee << " yuan." << endl;
break;
}
tempStack.push(car);
}
// Restore the parking lot
while (!tempStack.empty()) {
Car car = tempStack.top();
tempStack.pop();
parkingLot.push(car);
}
// If the car is not in the parking lot, check the waiting queue
if (!found) {
queue<Car> tempQueue;
while (!waitingQueue.empty()) {
Car car = waitingQueue.front();
waitingQueue.pop();
position++;
if (car.licensePlate == licensePlate) {
found = true;
cout << "Car " << car.licensePlate << " is parked at position " << position << " in the waiting queue." << endl;
break;
}
tempQueue.push(car);
}
// Restore the waiting queue
while (!tempQueue.empty()) {
Car car = tempQueue.front();
tempQueue.pop();
waitingQueue.push(car);
}
}
if (!found) {
cout << "Car " << licensePlate << " is not found." << endl;
}
}
int main() {
stack<Car> parkingLot;
queue<Car> waitingQueue;
// Simulate the parking lot management
Car car1 = {"ABC123", 1};
Car car2 = {"DEF456", 2};
Car car3 = {"GHI789", 3};
parkCar(parkingLot, waitingQueue, car1);
parkCar(parkingLot, waitingQueue, car2);
parkCar(parkingLot, waitingQueue, car3);
removeCar(parkingLot, waitingQueue, "DEF456", 5);
removeCar(parkingLot, waitingQueue, "XYZ999", 6);
return 0;
}
```
这个示例代码演示了如何使用栈和队列来实现停车场管理系统。在主函数中,我们首先创建了一个空的停车场和一个空的便道。然后,我们模拟了几辆车的到达和离开操作,并输出相应的信息。
阅读全文