C++栈和队列模拟停车场管理系统

一、题目

某停车场可以停放n辆汽车,该停车场只有一个大门, 每辆汽车离开停车场都要求之前的汽车必须先退出停车场为它让道,而后让道的汽车再次驶入停车场,停车场示意图如下:在这里插入图片描述

要求设计合理的数据组织方式,设计算法实现停车管理系统,实现车辆的进入、离开并根据停车时间计费。

二、效果图

  • 五辆车进入只能停三辆车的停车场(假设停车场容量为3)
    在这里插入图片描述
  • 查看车场状态同时让一辆车离开 候车场的车进入
    在这里插入图片描述
  • 查看车场状态再让一辆车离开 候车场的车进入
    在这里插入图片描述
  • 查看车场离开的那两辆车的历史停车付费记录
    在这里插入图片描述

三、分析

  • 对于这一题来说,我们只要使用栈和队列的数据结构就可以很好地完成需求,由于停车场只有一个大门,所以我们可以近似的将这个停车场看作是一个栈(容量为3),先进入的车辆要出来必须要让后面的车为他让路,而当停车场满了时,我们可以利用队列来模拟一个候车场,一旦停车场有车离开,后车场的车就可以进去,这时的候车场遵循的规则就是先来先进,所以用队列十分适合,我们同时对车进入和离开的时间进行一个统计,然后再和我们设置的金额进行计算便可得到每辆车应付的金额,同时我还添加了一个查看历史停车记录和清屏的功能,查看历史停车记录可以方便管理员对车辆的排查,防止有人对停车的金额有异议,清屏的功能主要就是能够令界面更加清爽,当输出信息过多时,可以进行清屏操作。

四、代码

//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;
		}
	}
}

猜你喜欢

转载自blog.csdn.net/qq_43663263/article/details/106854126