C ++スタックおよびキューシミュレーション駐車場管理システム

1.タイトル

特定の駐車場はn台の車を駐車でき、駐車場にはゲートが1つしかないため、駐車場を出るすべての車は、前の車が駐車場を出て道を譲る必要があります。その後、駐車場に戻った車は再び駐車場に移動します。概略図は次のとおりです。ここに写真の説明を挿入

合理的なデータ編成方法の設計、駐車場管理システムを実現するためのアルゴリズムの設計、車両の出入りの実現、駐車時間に応じた課金が必要です。

第二に、レンダリング

  • 5台が3台で駐車場に入る(駐車場容量が3台と仮定)
    ここに写真の説明を挿入
  • 待合室を出た車を入れながら駐車場の状況を確認する
    ここに写真の説明を挿入
  • 駐車場の状況を確認し、車を待合室から出させて入場させます
    ここに写真の説明を挿入
  • 駐車場を出た2台の車両の過去の駐車料金記録を表示する
    ここに写真の説明を挿入

三、分析

  • この質問では、スタックとキューのデータ構造を使用するだけで要件を満たせます。駐車場にはゲートが1つしかないため、駐車場をスタック(容量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