数据结构课设(停车场管理系统的设计与实现)

目录

一、设计目的

二、问题描述

三、数据结构设计

四、功能(函数设计)

五、图例分析(有图有真相!)

        队列——栈类成员 

        停车场管理类 

        主函数流程图

六、编码实现

        # Queue_Stack.h(头文件,其实是一个类资源文件,根据上机实验修改)

        #Queue_Stack.cpp(相应功能的实现) 

        #Parking_Management.h(类资源文件,包含基本模块、个性化功能)

        #Parking_Management.cpp(相应功能的实现)

        # main.cpp(测试文件)

七、界面设计

        欢迎界面

        用户操作界面

        进入停车场

        离开停车场

        查看停车场状况

         退出系统

八、运行与测试


一、设计目的

        理解线性表的逻辑结构和存储结构,进一步提高使用理论知识指导解决实际问题的能力。

二、问题描述

        设停车场只有一个可停放几辆车的狭长通道,只有一个大门可供汽车出入。汽车在停车场内按车辆到达的先后顺序依次排列,若车场内已经停满了几辆车;则后来的汽车只能在门外的便道上等候,一旦停车场内有车辆开走,则排在便道上的第一辆汽车即刻进入;当停车场内某辆汽车要开走时,由于停车场是狭长的通道,在它之后开入的车辆必须先退出车场为它让路,待车辆开出大门,为它让路的车辆再按原次序进入停车场。试设计这有一个停车场模拟管理系统。

三、数据结构设计

        (1)为了便于区分每辆汽车并了解每辆车当前所处的位置,需要记录汽车的车牌号和和汽车的当前状态。

        (2)为了便于停车场管理,要为每个车位分配一个固定的编号。

        (3)但停车场的停车位都已停满了汽车,又有新的汽车到来时把它调度到便道上,便道上的车辆要按照便道的先后顺序顺次序放在便道上,为便道上的每一个位置分配一个固定的编号。但有车从停车位离开后,便道上的第一辆汽车就立即进入停车场的某个车位。

        (4)当某辆车离开停车场时,比它后进停车位的车要给它让路,而且当它开走之后让路的车还要按照原来的停放次序再次进入停车位的某个车位上,为完成这项功能,请定义一个结构体。

四、功能(函数设计)

本程序从总体上分为4个功能模块,分别为:

        (1)程序功能介绍和操作提示模块

        (2)汽车进入停车位的管理模块

        (3)汽车离开停车位的管理模块

        (4)查看停车场状态的查询模块

个性化功能设计():

        (1)将停车场状态信息、便道信息写入文件

        (2)设计一个计时器管理停车场内每辆车辆的停车时长

五、图例分析(有图有真相!)

        队列——栈类成员 

        停车场管理类 

        主函数流程图

        


六、编码实现

该模拟系统的功能实现,我分了五个文件来完成(其实是两个类文件 + 一个测试文件main.cpp),依次为:Queue_Stack.h(头文件)及其相应的Queue_Stack.cpp文件Parking_Management.h(头文件)及其Parking_Management.cpp文件,最后加一个用于测试的文件,即main.cpp文件

        # Queue_Stack.h(头文件,其实是一个类资源文件,根据上机实验修改)

         就是一个队列——栈类文件,包含有基本的与队列、栈有关的函数,还包含了数据成员停车场栈S1、辅助栈S2、便道汽车组成的队列Q1

//             Author: 风吹麦浪~ /  leisure-pp     //
//             History:2023年6月1日               //

#pragma once
#define   _CRT_SECURE_NO_WARNINGS 1
#include<iostream>           //输入输出流
#include<string>   
#include<fstream>           //文件操作
//#include<assert.h>
#include<Windows.h>  
using namespace std;

#define stack_init_size 5

typedef string ElemType;//重命名

typedef struct _node  //为什么这么用,因为在定义结构体栈时,用c语言的char 、char* 
//,跟C++的string.c_str()的相互转化不方便
{
	string info = "NULL";
}_node;

typedef struct stack
{
	_node* base;   //栈空间基址(结点类型)
	int capacity;  //容量
	int top;      //栈顶指针
}SeqStack;

typedef SeqStack SeqS;  //重命名 


typedef string ET;   //重命名

typedef struct node  //队列结点
{
	ET data;
	node* next;  //指针
}node;

typedef struct linkQueue  //队列
{
	node* front;  //队列头结点
	node* rear;   //队尾
}linkQueue;

typedef linkQueue LQ;  //重命名

///
///
///


class Queue_Stack  //创建一个队列和栈的类
{

public:
	///  栈  
	void Init_Stack(SeqStack* s);
	bool EmptyStack(SeqStack* s);
	bool Full_Stack(SeqStack* s);
	void Push_Stack(SeqStack* s, string x);
	void Pop_Stack(SeqStack* s);
	string Gethead_Stack(SeqStack* s);
	void Show_Stack(SeqStack* s);
	int Length_Stack(SeqStack* s);
	//void clear(SeqStack* s);
	//void destroy(SeqStack* s);



	// 队列  //
	void Init_Queue(LQ* Q);
	void EnQueue(LQ* Q, ET x);
	void Pop_Queue(LQ* Q);
	void Show_Queue(LQ* Q);
	int Length_Queue(LQ* Q);
	ET Gethead_Queue(LQ* Q);
	bool Empty_Queue(LQ* Q);


	~Queue_Stack() {};  //析构函数

public:
	SeqS S1; //已停进去的汽车构成的栈
	SeqS S2; //当有汽车离开时,再次进入,保证次序的辅助栈
	LQ Q1; //在便道上等待的汽车构成的队列
};

        #Queue_Stack.cpp(相应功能的实现) 

//             Author: 风吹麦浪~ /  leisure-pp     //
//             History:2023年6月1日                //


#pragma once
#include "Queue_Stack.h"

//  栈  //

//初始化栈
void Queue_Stack::Init_Stack(SeqStack * s)
{
	s->base = new _node[stack_init_size];  //开辟stack_init_size个连续的结点空间
	s->capacity = stack_init_size;         //栈的容量
	s->top = 0;                        //从0开始
}
//判断栈是否为空
bool Queue_Stack::EmptyStack(SeqStack* s)
{
	return s->top == 0;
}
//判断栈是否满了
bool Queue_Stack::Full_Stack(SeqStack* s)
{
	return s->top >= s->capacity;
}
//入栈
void Queue_Stack::Push_Stack(SeqStack* s, string x)
{
	s->base[s->top++].info = x;   //从0开始存储数据,记录车牌号信息(控制台输出)
}

//出栈
void Queue_Stack::Pop_Stack(SeqStack* s)
{
	if (EmptyStack(s))
		return;
	s->base[s->top - 1].info = "NULL";
	s->top--;
}

//获取栈顶元素
string Queue_Stack::Gethead_Stack(SeqStack* s)
{
	if (EmptyStack(s))
	{
		return "NULL";
	}
	return s->base[s->top - 1].info;

}

//显示栈的数据信息
void Queue_Stack::Show_Stack(SeqStack* s)
{
	for (int i = stack_init_size - 1; i >= 0; --i)
	{
		string p = s->base[i].info;
		cout << "车位" << i + 1 << "对应的车牌号:" << s->base[i].info << endl;
	}
	cout << endl;
}

//获取栈的长度
int Queue_Stack::Length_Stack(SeqStack* s)
{
	return s->top;
}


//  队列  //

//初始化队列
void Queue_Stack::Init_Queue(LQ* Q)
{
	Q->rear = Q->front = new node;//开辟一个node类型空间,!!!注意栈中base的结点为  _node
	//assert(Q->front != NULL);
	Q->front->next = NULL;
}

//入队列
void Queue_Stack::EnQueue(LQ* Q, ET x)
{
	node* p = new node;
	//assert(p != NULL);
	p->data = x;
	p->next = NULL;

	Q->rear->next = p;
	Q->rear = p;
}

//出队列
void Queue_Stack::Pop_Queue(LQ* Q)
{
	node* p = Q->front;
	if (p == Q->rear)
		return;
	p = p->next;
	delete(Q->front);
	Q->front = p;
}

//显示队列的基本信息
void Queue_Stack::Show_Queue(LQ* Q)
{
	node* p = Q->front->next;

	for (int i = 1; i <= stack_init_size; i++)
	{
		if (p != NULL)
		{
			cout << "便道" << i << "位置的车牌号:" << p->data << endl;
			p = p->next;
		}
		else
		{
			cout << "便道" << i << "位置的车牌号:" << "NULL" << endl;
		}
	}

	cout << endl;
}

//获取队列的长度
int Queue_Stack::Length_Queue(LQ* Q)
{
	int i = 0;
	node* p = Q->front;  //若改为 p=Q->front->next ,while(p!=NULL)
	while (p != Q->rear) //        { i++; p=p->next;} 也行
	{
		i++;
		p = p->next;
	}
	return i;
}

//获取队列的第一个元素
ET Queue_Stack::Gethead_Queue(LQ* Q)
{
	node* p = Q->front;
	if (p == Q->rear)
		return NULL;
	else
	{
		p = p->next;  //头指针不充当存储数据的结点,所以要让p后移
	}
	return p->data;
}

//判断队列是否为kong
bool Queue_Stack::Empty_Queue(LQ* Q)
{
	if (Q->front == Q->rear)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

        #Parking_Management.h(类资源文件,包含基本模块、个性化功能)

        还是,再一次不厌其烦的介绍下停车场管理类的基本信息吧

         包含了成员函数:

                        Parking_Management()构造函数

                        void     Enter_Parking()进入停车场

                        void     Exit_Parking()离开停车场

                        void     Print()查看停车场的状态

                        void    Save()写入文件

                        void    Get_CurTime()获取每辆车进入停车场的时间

                        void    Out_Time_Price(int tt, string c)缴纳对应停车时长的停车费

        包含的数据成员:

                int capacity;  //停车场最大容量

                Postion_information Pos_info[MAX_CAPACITY];   //车位信息

                _Path path[MAX_CAPACITY];   //便道信息

                const int price=5;   //收费标准5元/分钟

                Time_node Time[MAX_CAPACITY];    //计时结点

                Queue_Stack*  QS;  //队列——栈类,包含了S1,S2,Q3,及其对应的函数

//
//             Author: 风吹麦浪~ /  leisure-pp     //
//             History:2023年6月1日                //
//


#pragma once
#include"Queue_Stack.h"

#include<ctime>     //应用头文件 ,获取系统当前的时间

#define MAX_CAPACITY stack_init_size //最大车位数

typedef struct Postion_information
{
	int number;   //车位编号
	string condition = "未使用";  //车位的状态(正在使用、未使用)
	string license_number = "NULL";     //该车位所对应的编号
}Postion_information;

typedef struct _Path
{
	int number;   //便道编号
	string license_number = "NULL";     //该便道所对应的编号
	string condition = "未使用"; //便道的状态
}_Path;


//计时操作(结点保存时间)
struct Time_node
{
	SYSTEMTIME In;      //详细时间 ~年~月~日~分
	int begin_time;  //入栈时间
	int finish_time; //出栈时间
	int op_time;  //停车时长
};


//
//   停车场管理类 Parking_Management  
//

class Parking_Management //Parking_Management管理类
{
public:
	Parking_Management();  //初始化停车场基本信息

	void Enter_Parking();  //进入停车场(先判断是否满了)

	void Exit_Parking();   //离开停车场(需要一个辅助栈来实现)

	void Print();  //打印目前停在停车场汽车对应的编号


	//个性化
	void Save();//文件保存停车场、便道信息

	void Get_CurTime();//每辆车的获取入栈时间
	void Out_Time_Price(int tt, string c);  // 输出要缴纳的停车费


	virtual ~Parking_Management() {}

private:
	int capacity;  //最大容量

	const int price = 5;  //  5元/分钟

	Postion_information Pos_info[MAX_CAPACITY]; //车位信息
		_Path path[MAX_CAPACITY];  //便道信息

	Queue_Stack* QS;//队列—栈类,包含了S1,S2,Q3,及其对应的函数


	Time_node Time[MAX_CAPACITY];  //计时结点
};


           #Parking_Management.cpp(相应功能的实现)

//             Author: 风吹麦浪~ /  leisure-pp     //
//             History:2023年6月1日                //


#include "Parking_Management.h"

//基本功能
Parking_Management::Parking_Management()  //构造函数
{
	capacity = MAX_CAPACITY;  //容量,即停车场的车位数
	QS = new Queue_Stack;  //申请一个类的空间

	QS->Init_Stack(&QS->S1);  //初始化栈S1
	QS->Init_Stack(&QS->S2);  //初始化栈辅助栈S2
	QS->Init_Queue(&QS->Q1);  //初始化队列Q3
	for (int i = 0; i < MAX_CAPACITY; i++)
	{
		Pos_info[i].number = i + 1;   //初始化编号
		path[i].number = i + 1;
	}
}



void Parking_Management::Enter_Parking()
{
	system("cls");

	string flag = "1";  //判断该入栈操作是否继续或者是结束

	do
	{
		string license_n;  //车牌号
		cout << "请输入您的车牌号:";
		cin >> license_n;

		if (QS->Full_Stack(&QS->S1)) //如果栈已满,则进入便道
		{
			if (QS->Length_Queue(&QS->Q1) != MAX_CAPACITY)
			{
				QS->EnQueue(&QS->Q1, license_n);//进入便道

				int t = QS->Length_Queue(&QS->Q1);//每次都获取队列长度
				path[t - 1].license_number = license_n;  //更新便道状态
				path[t - 1].condition = "正在占用";
				cout << "\n停车位已满,正在进入便道.....\n";
			}
			else
			{
				cout << "\n便道也满了,请先执行[3]操作" << endl;
				Sleep(1300);
				return;
			}
		}//end of if

		else
		{
			QS->Push_Stack(&QS->S1, license_n);  //入栈,也即进入停车场
			int t = QS->Length_Stack(&QS->S1);  //获取当前栈的长度
			Pos_info[t - 1].license_number = license_n;//记录车牌号
			Pos_info[t - 1].condition = "正在使用";  //更新停车位状态

			Get_CurTime();  //记录进入停车场的时间
		}

		do  //做异常输入处理
		{
			cout << "       是否继续(0结束、1继续):";
			cin >> flag;
		} while (flag != "0" && flag != "1");

	} while (flag == "1");
}





void Parking_Management::Exit_Parking()
{
	int sign = 0, flag = 0;
	int tt;    //保存修改信息的停车位的下标位置

	Print();

	string ss1, p;

	if (!QS->EmptyStack(&QS->S1))
	{
		cout << "\n请输入停车场要离开的车牌号:";
		cin >> ss1;
	}
	else
	{
		cout << "\n停车场为空,请先执行[2]操作...\n\n";
		system("pause");
		return;
	}

	//int S_len = QS->Length_Stack(&QS->S1);

	while (!QS->EmptyStack(&QS->S1))
	{
		p = QS->Gethead_Stack(&QS->S1);  //获取栈顶元素

		if (p == ss1)   //找到了该车的位置
		{

			//判断便道上是否有车辆
			if (!QS->Empty_Queue(&QS->Q1))
			{
				sign = 1;
				for (int i = 0; i < MAX_CAPACITY; i++)
				{
					if (p == Pos_info[i].license_number)  //找到相应车位对应的车牌号
					{
						cout << endl;
						int j = i + 1;
						for (; j < MAX_CAPACITY && Pos_info[j].license_number != "NULL"; j++)
						{
							cout << "车牌号:" << Pos_info[j].license_number << "已让位" << endl;
						}
						cout << endl;

						p = QS->Gethead_Queue(&QS->Q1);
						Pos_info[i].license_number = p;  //更新车位信息
						QS->Pop_Queue(&QS->Q1);  //删除队列的第一个元素

						tt = i;

						for (int i = 0; i < MAX_CAPACITY - 1; i++) // 更新便道信息
						{
							path[i].condition = path[i + 1].condition;  //前移一位
							path[i].license_number = path[i + 1].license_number;
						}

						path[MAX_CAPACITY - 1].license_number = "NULL";
						path[MAX_CAPACITY - 1].condition = "未使用";

						cout << "   请按任意键查看该车的交费信息...";

						getchar();
						getchar();

						system("cls");

						Out_Time_Price(tt, ss1);

						break;
					}
				}
			}


			//便道没车
			else
			{
				if (flag == 0)
				{
					flag = 1;

					int i;
					for (i = 0; i < MAX_CAPACITY; i++) {
						if (ss1 == Pos_info[i].license_number) {
							tt = i;

							cout << endl;
							for (i = i + 1; i < MAX_CAPACITY && Pos_info[i].license_number != "NULL"; i++)
							{
								cout << "车牌号:" << Pos_info[i].license_number << "已让位" << endl;
							}
							cout << endl;

							break;
						}
					}
					cout << "   请按任意键查看该车的交费信息...";
					getchar();
					getchar();
					system("cls");

					Out_Time_Price(tt, ss1);

					for (i = tt; i < MAX_CAPACITY - 1; i++)
					{
						//信息前移的同时,时间也要跟着移动
						Time[i].In = Time[i + 1].In;
						Time[i].begin_time = Time[i + 1].begin_time;

						Pos_info[i].license_number = Pos_info[i + 1].license_number;
						Pos_info[i].condition = Pos_info[i + 1].condition;
					}
					Pos_info[i].license_number = "NULL";
					Pos_info[i].condition = "未使用";
				}
			}
		}


		if (p != ss1)
		{
			string h = p;
			QS->Push_Stack(&QS->S2, h);  //用辅助栈储存信息
		}

		QS->Pop_Stack(&QS->S1);     //删除栈顶元素
	}



	//重新计时
	if (sign == 1)
	{
		SYSTEMTIME t;
		GetLocalTime(&t);
		Time[tt].In = t;
		Time[tt].begin_time = t.wMinute;
		Time[tt].op_time = 0;
	}

	while (!QS->EmptyStack(&QS->S2))    //重新将辅助栈的信息导入停车场栈结构
	{
		string p = QS->Gethead_Stack(&QS->S2);
		QS->Push_Stack(&QS->S1, p);
		QS->Pop_Stack(&QS->S2);
	}

	system("cls");

}



void Parking_Management::Print()
{
	system("cls");

	cout << "\n停车场基本信息:共" << MAX_CAPACITY << "个车位" << endl << endl;

	if (QS->EmptyStack(&QS->S1))
	{
		cout << "当前停车场为空...\n\n" << endl;
		return;
	}
	QS->Show_Stack(&QS->S1);

	cout << " \n便道基本信息:  (队列)\n\n";
	QS->Show_Queue(&QS->Q1);
}


//个性化
void Parking_Management::Save()
{
	ofstream outfile("停车场管理系统.txt", ios::trunc);
	if (!outfile)
	{
		cerr << "未打开文件\n";
		return;
	}
	outfile << "\t停车场模拟管理系统\n" << endl;
	outfile << "\t停车场基本信息:" << endl;

	for (int i = MAX_CAPACITY; i >= 1; i--)
	{
		outfile << "车位" << i << "的状态:" << Pos_info[i - 1].condition << "    ";
		outfile << "车牌号:" << Pos_info[i - 1].license_number << endl;
	}

	outfile << endl << "\t便道基本信息:" << endl;
	for (int i = 1; i <= MAX_CAPACITY; i++)
	{
		outfile << "便道" << i << "的状态:" << path[i - 1].condition << "    ";
		outfile << "车牌号:" << path[i - 1].license_number << endl;
	}

	outfile.close();
}

void Parking_Management::Get_CurTime()
{
	SYSTEMTIME cur_time;
	GetLocalTime(&cur_time);  //获取当前的详细时间

	int p = QS->Length_Stack(&QS->S1) - 1;
	Time[p].In = cur_time;  //当前入栈的详细时间
	Time[p].begin_time = (int)cur_time.wMinute;  //我们只记录分钟
}


void Parking_Management::Out_Time_Price(int tt, string c)
{
	SYSTEMTIME out_time;
	GetLocalTime(&out_time);  //出栈时间

	int p = tt;  ///找到对应位置(为了获取)费用信息
	Time[p].finish_time = out_time.wMinute;

	if (Time[p].begin_time < Time[p].finish_time)  //未进位,即hour (时钟)相同
	{
		Time[p].op_time = Time[p].finish_time - Time[p].begin_time;
	}
	else if (Time[p].begin_time > Time[p].finish_time)  //让 finish_time += 60
	{
		Time[p].finish_time += 60;
		Time[p].op_time = Time[p].finish_time - Time[p].begin_time;
	}
	else if (Time[p].begin_time == Time[p].finish_time)
	{
		Time[p].op_time = 1;  //不足一分钟按一分钟计算,有疑问看结构体定义
	}

	cout << "   车牌号为:" << c << endl << endl;

	cout << "进入时间:";
	cout << Time[p].In.wYear << "年" << Time[p].In.wMonth << "月" << Time[p].In.wDay
		<< "日" << Time[p].In.wHour << "点" << Time[p].In.wMinute << "分" << endl;

	cout << "离开时间:";
	cout << out_time.wYear << "年" << out_time.wMonth << "月" << out_time.wDay
		<< "日" << out_time.wHour << "点" << out_time.wMinute << "分" << endl;

	cout << "停车时长为:" << Time[p].op_time << "分钟" << endl;
	cout << "应缴费用为:" << Time[p].op_time * price << "元" << endl << endl;

	system("pause");

}

        # main.cpp(测试文件)

//线性结构
//基础数据结构解决实际问题
//栈
//队列
//等候
//出栈麻烦
//状态
//车牌号
//车位编号
//便道编号

//两种思路
//顺序、替换

//个性化
//
//
//

#include<ctime>
#include"Queue_Stack.h"
#include"Parking_Management.h"

//开始
void Start()
{
	SYSTEMTIME time;
	GetLocalTime(&time);
	cout<<"      欢迎使用停车场模拟管理系统!\n\n";
	cout << "当前时间:";
	cout << time.wYear << "年" << time.wMonth << "月" << time.wDay
		<< "日" << time.wHour << "点" <<time.wMinute<<"分"<< endl;

	cout<<"我是您的小助理,接下来让我们继续!\n\n";
	system("pause");
	system("cls");
}

//用户操作界面
void Operation()
{
	printf("请根据如下信息进行您想要的操作\n\n");
	Sleep(200);
	printf("———  用户操作界面1 ———\n");
	printf("——————————————\n");
	printf("--    [1]查看停车场状况   --\n");
	printf("--    [2]进入停车场       --\n");
	printf("--    [3]离开停车场       --\n");
	printf("--    [4]退出系统         --\n");
	printf("——————————————\n");
	printf("请输入对应序号:");
}

int main0()
{
	double a = clock();
	Sleep(1000);
	double b = clock();
	cout <<a<<endl<<endl;

	SYSTEMTIME time,tiem1;
	GetLocalTime(&time);
	cout << time.wYear << " " << time.wMonth << " " << time.wDay << " " << time.wHour << " "
		<< time.wSecond << endl;

	GetLocalTime(&tiem1);
	//cout << tiem1 - time;

	system("pause");
	return 0;
}





int main()
{
	Parking_Management m; //定义停车场管理类m,调用已写好的函数
	Start();

	int item;//变量(用于条件判断)

	int s = 999;  //出车位置(用于缴费)
	string ch = "NULL";  //车牌号(用于缴费)

	do
	{
		Operation();
		scanf_s("%d", &item);
		switch (item)
		{
		case 1:
			m.Print();
			system("pause");
			break;
		case 2:
			m.Enter_Parking();
			break;
		case 3:
			m.Exit_Parking();  //获取出车的车牌号
			break;
		case 4:
			system("cls");
			cout << "\n     欢迎您再次使用该系统,再见...   \n"<<endl;
			Sleep(1500);
			exit(0);
			break;
		default:
			printf("小助理提醒您,输入序号错误,请等待更新界面...");
			Sleep(1300);
			break;
		}
		m.Save();

		system("cls");
	} while (item != 4);

	return 0;
}



七、界面设计

        停车场模拟系统欢迎界面  

                                

        管理系统用户操作界面

                                

                                

        进入停车场

                                        

                                

                                 

        离开停车场

                                

                                                

        查看停车场状况

                                 

         退出系统

八、运行与测试

        (1)连续有7辆汽车到来,牌照号分别为CF001、CF002、CF003、CF004、CF005、CF006、CF007,前5辆车应该进入停车位1-5车位,第6、7辆车应停入便道的1、2位置上。

        (2)上面(1)中的情况发生后,让牌照CF003的汽车从停车场开走,应显示CF005、CF004的让路动作和CF006从便道到停车位的动作。

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

        (4)其它正常操作的一般情况。

        版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
        本文链接:https://blog.csdn.net/icc_hhy/article/details/130966880

  • 89
    点赞
  • 383
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
实验二 停车场管理 班级:A0712 学号:12 姓名:冷清淼 成绩:__________ 指导教师签名:__________ 一、问题描述 设停车场是一个可停放n辆车的狭长通道,且只有一个大门可供汽车进出。在停车场 ,汽车按到达的先后次序,由北向南依次排列(假设大门在最南端)。若停车场已停满 n辆车,则后来的汽车需在门外的便道上等候,当有车开走时,便道上的第一辆车即可开 入。当停车场某辆车要离开时,在它之后进入的车辆必须先退出停车场为它让路,待该 辆车开出大门后,其他车辆再按原次序返回车场。每辆车离开停车场时,应按其停留时 间的长短交费(在便道上停留的时间不收费)。 设计要求: 1.模拟上述管理过程。要求以顺序栈模拟停车场,以链队列模拟便道。 2.从终端读入汽车到达或离去的数据,每组数据包括三项: (1)是"到达"还是"离开"; (2)汽车牌照; (3)"到达"或"离开"的时刻。 3.与每组输入信息相应的输出信息为:如果是到达的车辆,则输出其在停车场中或 便道上的位置;如果是离去的车辆,则输出其在停车场中停留的时间和应交的费用。 二、算法说明 1.数据结构说明 (1)用到两个堆栈:一个为车场栈;另一个为临时栈temp typedef struct NODE{ CarNode *stack[MAX+1]; int top; }SeqStackCar; /*模拟车场*/ 一个队列结构,存储便道车辆信息: typedef struct Node{ QueueNode *head; QueueNode *rear; }LinkQueueCar; /*模拟便道*/ 2.算法说明 (1) 功能模块说明:停车场管理系统含有三个模块,即:车辆到达、离开、列表显示 图1 (2)以模块为单位分析算法 1、"到达"模块:到达时有两种情况,即车场是否满,未满则直接进入停车场;满时,到 便道等待。如图2。 图2 2."离开"模块:离开时,当车库为空时,提示没有车,结束;否则车辆离开。如图3。 图3 3. "显示"模块:显示模块有两个显示选项,即:车场与便道。如图4。 图4 三、测试结果 (一)测试用例(说明:测试用例要合理并且足够,既要有正确用例,也要有错误用例 ,同时检验程序的正确性和强壮性) 1.第一组测试用例 (1)测试输入:停车场的车辆离开,如下表: "服务选择 "车牌号/车位 "到达/离开时间 " "1 "QH058 "15:25 " "1 "AB123 "18:45 " "1 "EA642 "23:15 " "2 "2 "0:30 " "2 "1 "0:65(错误) " (2)测试目的:测试离开方法时间格式控制以及费用计算是否正确。 (3)正确输出:第一次离开的是AB123,应交费3.45元。第二次时,当在输入65时, 应该提示输入错误,重输。 (4)实际输出: (5)错误原因:第一个错误是在计算时,一个数字错了;第二个是没有对时间格式 控制。 (6)当前状态:已改正 2.第二组测试用例 (1)测试输入:连续6辆车到达,如下表: " 服务选 " 车牌号 " 到达时间 " "择 " " " "1 "A8828 "7:56 " "1 "S2296 "8:25 " "1 "WW666 "8:45 " "1 "HK456 "15:50 " "1 "GH999 "12:30 " "1 "DD555 "13:40 " 测试目的:测试到达方法与列表显示方法能否正确完成。 (3)正确输出:先到达的五辆车先进入停车场,最后到达的一辆在便道等候。 (4)实际输出: (5)错误原因:没有作出时间先后的判断,而是先输入先进入。 (6)当前状态:待修改 3.第三组测试用例 (1)测试输入:接上一步输入离开信息,下表: "服务选择"离开车位"离开时间"便道车进入时 " " " " "间 " "2 "3 "13:30 "13:40 " (2)测试目的:测试离开方法功能是否成功以及便道进入车场是否正确。 (3)正确输出:输出3号车位的车辆离开信息清单,便道1号车进入停车场。 (4)实际输出: 错误原因:没有错误。 (6)当前状态:通过 (二)测试结果分析 此停车管理系统基本可能实现一个小的停车场管理,其"到达"与"离开"方法都相对比 较完整,以及结算清单明了。尽管在时间先后上有出现混乱,但当其用到实际应用时, 那个时间先后就可以避免了。但在输入数据时,要按照严格的格式输入,否则有可能出 现死去或崩溃。若本系统能加上保存功能就更好了,因为一个系统在使用过程中总会关 机等,而此系统的缺点却是没有保存功能,关闭之后就要重新建立了。会慢慢完善。 附录:源代码 ///系统说明:本系统适应于小型停车场,且停车时间在一天之的短期停放停车场。 //在此系统中,车库容量设置为5,便于测
停车场管理系统是一个常见的数据结构课程设计项目,它主要涉及到栈和队列的应用。在这个项目中,我们使用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; } ``` 这个示例代码演示了如何使用栈和队列来实现停车场管理系统。在主函数中,我们首先创建了一个空的停车场和一个空的便道。然后,我们模拟了几辆车的到达和离开操作,并输出相应的信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leisure-pp

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值