前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【数据结构课程实验】模拟停车场 c++

【数据结构课程实验】模拟停车场 c++

作者头像
哈__
发布2024-04-08 21:18:31
1020
发布2024-04-08 21:18:31
举报
文章被收录于专栏:哈哈熊哈哈熊

题目:模拟停车厂管理

这是我之前在学习的时候写的代码,供大家参考。如果对您有帮助,请给博主一个小小的关注。

问题描述:

设停车厂只有一个可停放5辆汽车的狭长通道,且只有一个大门可供汽车进出。汽车在停车场内按车辆到达的先后顺序依次排列,若车场内已停满几辆汽车,则后来的汽车只能在门外的便道上等候,一旦停车场内有车开走,则排在便道上的第一辆车即可进入;当停车场内某辆车要离开时,由于停车场是狭长的通道,在它之后开入的车辆必须先退出车场为它让路,待该辆车开出大门后,为它让路的车辆再按原次序进入车场。在这里假设汽车不能从便道上开走。

基本要求:

以栈模拟停车场,以队列模拟车场外的便道,按照从终端输入数据序列进行模拟管理。每一组输入数据包括三个数据项:汽车“到达”或“离去”信息、汽车牌照号码以及到达或离去的时刻。对每一组输入数据进行操作后的输出信息为:若是车辆到达,则输出车辆在停车场内或便道上的停车位置;若是车辆离去,则输出车辆在停车场内停留的时间和应缴纳的费用(1小时以内不收费,1小时以上收费标准为2元/小时,在便道上停留的时间不收费)。栈以顺序结构出现,队列以链表结构实现。

测试数据:

(1)连续有7辆车到来,牌照号分别为JF001、JF002、JF003、JF004、、JF005、JF006、JF007,前5辆车应该进入停车位1-5车位,第6、7辆车应停入便道的1、2位置上。

(2)(1)中的情况发生后,让牌照号为JF003的汽车从停车厂开走,应显示JF005、JF004的让路动作和JF006从便道到停车位上的动作。

(3)随时检查停车位和便道的状态,不应该出现停车位有空位而便道上还有车的情况。

(4)程序容错性的测试,当按键输入错误的时候是否有错误提示给用户指导用户正确操作,并作出相应处理保证程序健康的运行。

话不多说直接上代码。直接运行即可。 

代码语言:javascript
复制
#include<iostream>
#include<map>
#include<string>
using namespace std;
#define MAXSIZE 5
class Info
{
public:
	Info(string number, int hour, int minute)
	{
		this->number = number;
		this->hour = hour;
		this->minute = minute;
		this->total_number += 1;;
	}
	string number;
	int hour;
	int minute;
	static int total_number;
};
int Info::total_number = 0;
class Parking //停车场 
{
public:
	Parking()
	{
		this->car_numberPark = 0;
		this->top = -1;
		this->parking = new string[MAXSIZE];
	}
	friend class TempParking;
	//friend int search_car();
	void car_inParking(string number);
	void car_outParking(string number);
	bool if_maxsize();
	void print_car(Info **p);
	string get_lastcar();
private:
	int car_numberPark;
	int top;
	string *parking;
};
class Plank//栈道 
{
public:
	Plank()
	{
		this->front = -1;
		this->tail = -1;
		this->plank = new string[5];
		this->car_number = 0;
		this->max_size = 5;
	}
	void car_inPlank(string number);
	string car_outPlank();
	bool wait_car();
	void print();  //test
	string *plank;
	int front;
	int tail;
	int car_number;
	int max_size;
};
class TempParking
{
public:
	friend class Parking;
	TempParking()
	{
		this->car = new string[5];
		this->top = -1;
	}
	void car_inTempParking(string number);
	void car_outTempParking();
	bool if_TempParking();
	string *car;
	int top;
};
int search_car(Info **p, string number)
{
	int n = Info::total_number;
	for (int i = 0;i < n;i++)
	{
		if (p[i] == NULL) continue;
		if (p[i]->number == number)
			return i + 1;
	}
	return 0;
}
void Parking::car_inParking(string number) //前提是停车场有空位 
{
	this->top++;
	this->parking[top] = number;
	this->car_numberPark++;
	cout << "车牌号为" << number << "的车已停入停车场。" << endl;
}
void Parking::car_outParking(string number)
{
	TempParking temp;
	while (this->parking[top] != number)
	{
		cout << "车牌号为" << this->parking[top] << "的车让开车位!" << endl;
		this->car_numberPark--;
		temp.car_inTempParking(this->parking[top]);
		top--;
	}
	cout << "车牌号为" << this->parking[top] << "的车离开了停车场!" << endl;
	this->top--;
	this->car_numberPark--;
	while (temp.if_TempParking())
	{
		//cout<<"车牌号为"<<temp.car[temp.top]<<"的车进入了停车场"<<endl;
		this->car_inParking(temp.car[temp.top]);
		//this->car_numberPark++;
		temp.car_outTempParking();
	}
}
bool Parking::if_maxsize()
{
	if (this->top+1 == MAXSIZE)
		return 1;
	else return 0;
}
void Plank::car_inPlank(string number)
{
	this->car_number++;
	if (this->car_number > this->max_size)
	{
		cout << "栈道已满!";
		//cout << this->front << " " << this->tail;
		//this->print();
		return;
	}
	else
	{
		cout << "车牌号为" << number << "的车进入栈道等待!" << endl;

		if (this->front == -1&&this->tail == -1)
		{
			this->front++;
			this->plank[this->front] = number;
			this->tail++;
		}
		else
		{
			this->tail++;
			this->plank[this->tail] = number;
		}
	}
}
void Parking::print_car(Info **p)
{
	cout << "当前停车场中的车辆为:" << endl;
	cout << "车牌号\t" << "入车时间\t" << endl;
	for (int i = 0;i < this->car_numberPark;i++)
	{
		cout << this->parking[i] << "\t";
		cout<< p[search_car(p,this->parking[i])-1]->hour<<":"<< p[search_car(p, this->parking[i])-1]->minute<<"\t"<< endl;
	}
}
string Parking::get_lastcar()
{
	return this->parking[top];
}
string Plank::car_outPlank()
{
	string number;
	if (this->car_number == 0)
	{
		cout << "栈道内没有车辆等候。" << endl;
	}
	else
	{
		this->car_number--;
		 number = this->plank[this->front];
		if (this->front ==0&& this->tail == 0)//只有一辆车
		{
			this->front = -1;
			this->tail = -1;
			return number;
		}
		else
		{
			for (int i = this->front + 1;i <= this->tail;i++)
			{
				this->plank[i - 1] = this->plank[i];
			}
			this->plank[this->tail] = "-1";
			this->tail--;
			return number;
		}
		
	}
		
	
}
void Plank::print()
{
	for (int i = this->front;i <= this->tail;i++)
	{
		cout << this->plank[i] << " ";
	}
}
bool Plank::wait_car()
{
	return this->car_number;
}
void TempParking::car_inTempParking(string number)
{
	this->top++;
	this->car[top] = number;
}
void TempParking::car_outTempParking()
{
	this->top--;
}
bool TempParking::if_TempParking()
{
	return this->top + 1;
}
void print_menu()
{
	cout << "***********************" << endl;
	cout << "**   1.停车场进车    **" << endl;
	cout << "**   2.停车场出车    **" << endl;
	cout << "**   3.显示停车场车辆**" << endl;
	cout << "**   0.退出系统      **" << endl;
	cout << "***********************" << endl;
}
bool time_right(int hour,int minute)
{
	if (hour < 0 || hour>23) return 0;
	else
	{
		if (minute < 0 || minute>60) return 0;
	}
	return 1;
}
int main()
{
	Info **inf = new Info*[100];
	//cout<<Info::total_number;
	Parking parking;
	TempParking t_parking;
	Plank plank;
	int choice, TRUE = 1;
	while (TRUE)
	{
		print_menu();
		cout << "请输入要进行的操作:";
		cin >> choice;
		while (!(choice != 1 || choice != 2 || choice != 3 || choice != 0))
		{
			cout << "请输入正确的选项:";
			cin >> choice;
		}
		string number;
		int hour, minute;
		switch (choice)
		{
		case 1:
			cout << "请输入车的车牌号:";
			//判断车牌号是否已经出现过 
			cin >> number;
			if (Info::total_number <= 0)
			{
				cout << "请输入进入停车场的时间(时):";
				cin >> hour;
				cout << "请输入进入停车场的时间(分):";
				cin >> minute;
				while (!time_right(hour, minute))
				{
					cout << "您输入的时间有误,请输入正确的时间!" << endl;
					cout << "请输入进入停车场的时间(时):";
					cin >> hour;
					cout << "请输入进入停车场的时间(分):";
					cin >> minute;
				}
				inf[Info::total_number-1] = new Info(number, hour, minute);
				parking.car_inParking(number);
			}
			else
			{
				while (search_car(inf, number))
				{
					cout << "请输入正确的车牌号!";
					cin >> number;
				}
				if (parking.if_maxsize())
				{
					cout << "该停车场已满!!!进入栈道等待" << endl;
					
					inf[Info::total_number-1] = new Info(number, -1, -1);  
					plank.car_inPlank(number);
				}
				else
				{

					cout << "请输入进入停车场的时间(时):";
					cin >> hour;
					cout << "请输入进入停车场的时间(分):";
					cin >> minute;
					while (inf[search_car(inf,parking.get_lastcar())-1]->hour > hour)
					{
						cout << "您输入的进入时间有问题,请重新输入(时):";
						cin >> hour;
						cout << "您输入的进入时间有问题,请重新输入(分):";
						cin >> minute;
						while (!time_right(hour, minute))
						{
							cout << "您输入的时间有误,请输入正确的时间!" << endl;
							cout << "请输入进入停车场的时间(时):";
							cin >> hour;
							cout << "请输入进入停车场的时间(分):";
							cin >> minute;
						}
					}
					if (inf[search_car(inf, parking.get_lastcar()) - 1]->hour == hour)
					{
						while (inf[search_car(inf, parking.get_lastcar()) - 1]->minute >= minute)
						{
							cout << "您输入的分有问题,请重新输入:";
							cin >> minute;
							while (!time_right(hour, minute))
							{
								cout << "您输入的时间有误,请输入正确的时间!" << endl;
								cout << "请输入进入停车场的时间(分):";
								cin >> minute;
							}
						}
					}
					while (!time_right(hour, minute))
					{
						cout << "您输入的时间有误,请输入正确的时间!" << endl;
						cout << "请输入进入停车场的时间(分):";
						cin >> minute;
					}
					//cout << Info::total_number;
					inf[Info::total_number-1] = new Info(number, hour, minute);
					//cout<<Info::total_number;      //判断时间是否合理   待做 
					parking.car_inParking(number);
				}
			}
			system("pause");
			system("cls");
			break;
		case 2:
			//string number;
			cout << "请输入要离开的车辆的车牌号;";
			cin >> number;
			if (search_car(inf, number))
			{

				//int hour,minute;        
				cout << "请输入离开停车场的时间(时):";
				cin >> hour;
				cout << "请输入离开停车场的时间(分):";
				cin >> minute;
				//判断时间是否合理 
				while (hour < inf[search_car(inf, parking.get_lastcar()) - 1]->hour)
				{
					cout << "您输入的时间有问题,出车时间应该大于最后停入车辆的入车时间!请重新输入时间(时):";
					cin >> hour;
					cout << endl << "请输入正确离开停车场的时间(分):";
					cin >> minute;
					while (!time_right(hour, minute))
					{
						cout << "您输入的时间有误,请输入正确的时间!" << endl;
						cout << "请输入进入停车场的时间(时):";
						cin >> hour;
						cout << "请输入进入停车场的时间(分):";
						cin >> minute;
					}
					/*while (inf[search_car(inf, number) - 1]->hour > hour)
					{
						cout << "请输入正确的时间,出车时间应大于自己入车时间!" << endl;
						cout << "请输入正确离开停车场的时间(时):";
						cin >> hour;
						cout << endl << "请输入正确离开停车场的时间(分):";
						cin >> minute;

						while (!time_right(hour, minute))
						{
							cout << "您输入的时间有误,请输入正确的时间!" << endl;
							cout << "请输入进入停车场的时间(时):";
							cin >> hour;
							cout << "请输入进入停车场的时间(分):";
							cin >> minute;
						}
					}
					if (inf[search_car(inf, number) - 1]->hour == hour)
					{
						while (inf[search_car(inf, number) - 1]->minute > minute)
						{
							cout << "请输入正确离开停车场的时间(分):";
							cin >> minute;
							while (!time_right(hour, minute))
							{
								cout << "您输入的时间有误,请输入正确的时间!" << endl;
								cout << "请输入进入停车场的时间(分):";
								cin >> minute;
							}
						}
					}*/
				}
				if (hour == inf[search_car(inf, parking.get_lastcar()) - 1]->hour)
				{
					while (minute <= inf[search_car(inf, parking.get_lastcar()) - 1]->minute)
					{
						cout << "您输入的时间有问题,出车时间应该大于最后停入车辆的入车时间!请重新输入时间(分):";
						cin >> minute;
						while (!time_right(hour, minute))
						{
							cout << "您输入的时间有误,请输入正确的时间!" << endl;
							cout << "请输入进入停车场的时间(分):";
							cin >> minute;
						}
					}
				}
				while (!time_right(hour, minute))
				{
					cout << "您输入的时间有误,请输入正确的时间!" << endl;
					cout << "请输入进入停车场的时间(时):";
					cin >> hour;
					cout << "请输入进入停车场的时间(分):";
					cin >> minute;
				}
				//计算费用和 
				int total_minute;
				int total_minute1 = inf[search_car(inf, number) - 1]->hour * 60 + inf[search_car(inf, number) - 1]->minute;
				int total_minute2 = hour * 60 + minute;
				total_minute = total_minute2 - total_minute1;
				if (total_minute <= 60) cout << "停车未满一小时,不计费用。" << endl;
				else
				{
					int consume_hour = (total_minute - 1) / 60;
					int money = 2 * consume_hour;
					cout << "您一共消费" << money << "元,祝您一路顺风。" << endl;
				}
				//停车场出车 

				parking.car_outParking(number);
				inf[search_car(inf, number) - 1] = NULL;
				//栈道进车 
				if (plank.wait_car())
				{
					//cout<<plank.wait_car();
					string Number = plank.car_outPlank();
					//cout << "111";
					parking.car_inParking(Number);
					inf[search_car(inf, Number) - 1]->hour = hour;
					inf[search_car(inf, Number) - 1]->minute = minute;
				}

			}
			else cout << "该车辆不在停车场之中!" << endl;
			system("pause");
			system("cls");
			break;
		case 3:
			parking.print_car(inf);

			system("pause");
			system("cls");
			break;
		case 0:
			cout << "谢谢您的使用。";
			TRUE = 0;
			break;
		}
	}
}

 结果截图

其他的不再展示了。 

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-04-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目:模拟停车厂管理
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档