C++ stack and queue simulation parking lot management system

1. Title

A certain parking lot can park n cars, and the parking lot has only one gate. Every car leaving the parking lot requires that the previous car must exit the parking lot to make way for it, and then the cars that gave way to the parking lot again. The schematic diagram is as follows:Insert picture description here

It is required to design a reasonable data organization method, design an algorithm to realize a parking management system, realize the entry and departure of vehicles and charge according to the parking time.

Second, the renderings

  • Five cars enter the parking lot with only three cars (assuming the parking lot capacity is 3)
    Insert picture description here
  • Check the status of the parking lot while letting a car leaving the waiting area enter
    Insert picture description here
  • Check the status of the parking lot and let a car leave the waiting area and enter
    Insert picture description here
  • View the historical parking payment records of the two vehicles that left the parking lot
    Insert picture description here

Three, analysis

  • For this question, we only need to use the data structure of stacks and queues to complete the requirements. Since the parking lot has only one gate, we can approximate the parking lot as a stack (capacity 3) , The vehicle that enters first must make way for the car behind. When the parking lot is full, we can use the queue to simulate a waiting yard. Once the parking lot leaves, the cars in the back yard can enter. At this time, the waiting area follows the rule of coming first, so it is very suitable to use queues. We also calculate the time of entry and exit of cars, and then calculate with the amount we set to get the amount payable by each car At the same time, I also added a function of viewing historical parking records and clearing the screen. Viewing historical parking records can facilitate the administrator to check the vehicle and prevent people from disagreeing with the amount of parking. The function of clearing the screen is mainly to make the interface more refreshing. , When the output information is too much, you can clear the screen.

Fourth, the code

//XMUT ZRZ 2020.6.19
#include<iostream>
#include<queue>
#include<stack>
#include<time.h>
#include<ctime>
#include<windows.h>
#include <iomanip>
#define capacity 3
using namespace std;
typedef struct Car {
    
    
	string License="";
	double begin=0;
	double end=0;
	string begin_time;
	string end_time;
	int order_number=0;
	double cost=0;
}Car;
struct cmp2 {
    
    
	bool operator ()(Car& a, Car& b) {
    
    
		return a.begin > b.begin;//最大值优先  
	}
};
int total = 0;//统计一共来过多少辆车
int money = 1;//一分钟一块钱
bool vis[200];
queue<Car> Waiting;
stack<Car> Parking;
priority_queue<Car,vector<Car>,cmp2> history;
string getTime()
{
    
    
	time_t timep;
	time(&timep);
	char tmp[64];
	strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S", localtime(&timep));
	return tmp;
}
void Wait(string license)
{
    
    
	Car temp;
	auto t = clock();
	temp.License = license;
	temp.begin = t;
	temp.begin_time = getTime();
	temp.order_number = ++total;
	Waiting.push(temp);
	cout << temp.License << "进入侯车道" << endl;
}
void Add(string license)
{
    
    
	if (Parking.size()>=capacity)
	{
    
    
		Wait(license);
	}
	else
	{
    
    
		Car temp;
		auto t = clock();
		temp.License = license;
		temp.begin= t;
		temp.begin_time = getTime();
		temp.order_number = ++total;
		Parking.push(temp);
		cout << temp.License << "进入车场,开始计时收费" << endl;
	}
}
void Leave(string license)
{
    
    
	stack<Car> temp;
	int cnt = Parking.size();
	while(!Parking.empty())
	{
    
    
		Car car=Parking.top();
		Parking.pop();
		if (car.License == license)
		{
    
    
			cout << car.License << "退出车场";
			auto t = clock();
			car.end = t;
			car.end_time = getTime();
			car.cost = (car.end - car.begin)/60000 * money;
			cout << "花费停车金额:" << setprecision(2)<<car.cost << endl;
			history.push(car);
			break;
		}
		else
		{
    
    
			temp.push(car);
		}
	}
	if (temp.size() == cnt)
	{
    
    
		cout << "没有找到相应车牌号的车" << endl;
		while (!temp.empty())
		{
    
    
			Parking.push(temp.top());
			temp.pop();
		}
	}
	else
	{
    
    
		while(!temp.empty())
		{
    
    
			Parking.push(temp.top());
			temp.pop();
		}
		if (!Waiting.empty())
		{
    
    
			Car car = Waiting.front();
			Waiting.pop();
			Add(car.License);
		}
	}
}
void Check()
{
    
    
	if (!Parking.empty())
	{
    
    
		cout << "当前在停车场的车为:" << endl;
		stack<Car> temp = Parking;
		while (!temp.empty())
		{
    
    
			Car x = temp.top();
			cout << "车牌号为:"<< x.License << " 进入停车场时间:" << x.begin_time << endl;
			temp.pop();
		}
	}
	else
	{
    
    
		cout << "当前停车场没有车" << endl;
	}
	if (!Waiting.empty())
	{
    
    
		cout << "当前在侯车场的车为:" << endl;
		queue<Car> temp1 = Waiting;
		while (!temp1.empty())
		{
    
    
			Car x = temp1.front();
			cout <<"车牌号为:"<<x.License << " 进入候车场时间:" << x.begin_time << endl;
			temp1.pop();
		}
	}
	else
	{
    
    
		cout << "当前侯车场没有车" << endl;
	}
}
void History_Check()
{
    
    
	cout << "总共有" << history.size() << "辆车停过" << endl;
	priority_queue<Car, vector<Car>, cmp2> temp = history;
	int i = 1;
	while (!temp.empty())
	{
    
    
		
		Car x = temp.top();
		temp.pop();
		cout <<"第"<<i++<<"辆车的车牌为:"<<x.License << " 停入时间为:" << x.begin_time << " 离开时间为:" << x.end_time << " 停车费为:" << setprecision(2) << x.cost << endl;
	}
}
int main()
{
    
    
	cout << "--------------------------欢迎使用停车管理系统--------------------------" << endl;
	cout << "1.车辆进入 2.车辆离开 3.查看当前车场状态 4.查看车场历史记录 5.清屏" << endl;
	while (1) {
    
    
		int i;
		cout << "请输入要执行的操作: ";
		cin >> i;
		switch (i)
		{
    
    
		case 1:
			{
    
    
				string in;
				cout << "请输入进入车辆车牌:";
				cin >> in;
				Add(in);
			}
			break;
		case 2:
			{
    
    
				string out;
				cout << "请输入离开车辆车牌:";
				cin >> out;
				Leave(out);
			}
			break;
		case 3:
		{
    
    
			Check();
			break;
		}
		case 4:
		{
    
    
			memset(vis, false, sizeof(vis));
			History_Check();
			break;
		}
		case 5:
		{
    
    
			system("cls");
			cout << "--------------------------欢迎使用停车管理系统--------------------------" << endl;
			cout << "1.车辆进入 2.车辆离开 3.查看当前车场状态 4.查看车场历史记录 5.清屏" << endl;
		}
		break;
		}
	}
}

Guess you like

Origin blog.csdn.net/qq_43663263/article/details/106854126