【数据结构c++项目】基于栈和队列的停车场管理系统

题目:

设停车场是一个可停放n 辆汽车的狭长通道,且只有一个大门可供汽车进出。汽车在停车场内按车辆达到时间的先后顺序,依次由北向南排列(大门在最南端,最先达到的第一辆车停放在车场的最北端),若车场内已停满n辆汽车,则后来的汽车只能在门外的便道上等候,一旦有车开走,则排在便道上的第一辆车即可开入;当停车场内某辆车要离开时,在它之后进入的车辆必须先退车车场为它让路,待赶辆车开出大门外,其它车辆在按原次序进入车场, 每辆停放在车场的车在它离开停车场时必须按它停留的时间长短叫纳费用。试为停车场编制按上述要求进行管理的模拟程序。

基本要求:

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

(2)最好有操作菜单。

【实现提示】

需另设一个栈,临时停放为给要离去的汽车让路而从停车场退出来的汽车,可以用顺序存储结构实现。 输入数据按到达或离去的时刻有序。栈中每个元素表示一辆汽车,包括两个数据项:汽车的牌照号码和进入停车场的时刻。

【实现代码】

1、main.cpp

#include <iostream>
#include <time.h>
#include <string.h>
#include "seqStack.h"
#include "seqQueue.h"
struct TIME
{
	int hour;
	int min;
	int sec;
};
struct CarNo
{
	TIME time;
	char num[30];
};

 int main()
{
	
	CarNo c;
	seqStack<CarNo> s(4);
	seqStack<CarNo> st(4);
	seqQueue<CarNo> q(4);
	char no[30];
	int op;
	int i,j,temp;
	int hour,min,sec,zong1,zong2,zong;
	while(1)
	{
		
	 	printf("\t\t\t           停车场管理系统            \n");
	    printf("\t\t\t_________-------------------_________\n");
	    printf("\t\t\t|                                   |\n");
	    printf("\t\t\t|    1. 车辆进入停车场              |\n");
	    printf("\t\t\t|    2. 车辆离开停车场              |\n");
	    printf("\t\t\t|    3. 查看停车场内车辆信息        |\n");
	    printf("\t\t\t|    4. 查看便道上等待的车辆信息    |\n");
	    printf("\t\t\t|    5. 退出                        |\n");
	    printf("\t\t\t_____________________________________\n\n\n");
		printf("请输入您所需要的功能:");
		scanf("%d",&op);
		switch(op)
		{
			case 1:  
			printf("请输入车牌号: ");
			scanf("%s",&c.num);
			
			time_t timepin;
			time(&timepin);
    		struct tm *tin;
			tin = gmtime(&timepin); 
			c.time.hour= 8 + tin->tm_hour;
    		c.time.min = tin->tm_min;
    		c.time.sec = tin->tm_sec; 
    		
			if (s.Push(c))       
			{
				printf("进入成功\n"); 
			}
			else                        //栈满 
			{
				if (q.enQueue(c))    
				{
					
					printf("进入等待区成功\n");
				}
				else
				{
					printf("候车场已满,不能停车\n");
				} 
			}
    		
			break;
			
			case 2:  
			printf("请输入要离开的车的车牌号: ");
			scanf("%s",&no);
			temp = s.pipei(no);//匹配 
			if(q.isEmpty())//等待区无车 
			{
				hour = s.array[i].time.hour;
	    		min = s.array[i].time.min;
				sec = s.array[i].time.sec;
				s.pops(); 
			} 
			else//等待区有车 
			{
				for(i=s.top;i>=temp;i--)
				{
					if(i == temp) //到目标车后,目标车出栈,同时等待区第1辆车进栈 
					{
						hour = s.array[i].time.hour;
	    				min = s.array[i].time.min;
						sec = s.array[i].time.sec;
						s.pops();
						for(j=st.top;j>=0;j--)
						{
							s.Pushtemp(st.array[j]);
							st.top--;
						}
						s.Pushtemp(q.array[0]);
						q.deQueue1();
					}
					if(i>temp)//目标车之后的车进入临时栈 
					{
						st.Pushtemp(s.array[i]);
						s.top--;
						
					}
				}
			}
			
			time_t timepint;
			time(&timepint);
    		struct tm *ton;
			ton = gmtime(&timepin); 
			c.time.hour= 8 + ton->tm_hour;
    		c.time.min = ton->tm_min;
    		c.time.sec = ton->tm_sec; 
    		zong1 = hour*3600 + min*60 +sec ;
			zong2 = c.time.hour*3600 + c.time.min*60 +c.time.sec ;
			zong = zong2 - zong1 ; 
			printf("车牌号%d离开成功\n离开时间为:%d时%d分%d秒\n",temp+1,c.time.hour,c.time.min,c.time.sec);
			printf("共计时长:%d秒\n",zong); 
			printf("共计花费:%d元\n",zong*5); 
			
			break;
			
			case 3:       
				s.display();
				break;
			case 4:    
				q.display();
				break;
			case 5:     
				printf("已退出运行\n");
				return 0;
			default:
				printf("输入的命令错误\n");
				break;
		}
	}
	return 0;
}

2、seqStack.h

class illegalSize{};
class outOfBound{};
template <class elemType>
class seqStack
{
    public:
    	elemType *array;    //栈存储数组,存放实际的数据元素。
        int top;            //栈顶下标。
        int maxSize;	    //栈中最多能存放的元素个数。
        void doubleSpace();
        
        seqStack(int initSize = 10); //初始化顺序栈

        bool isEmpty () { return ( top == -1 ); } ; //栈空返回true,否则返回false。
        bool isFull () { return (top == maxSize-1); };//栈满返回true,否则返回false。
        
        void clear() { top=-1; }; //清除栈中所有元素
        ~seqStack(){ delete []array;}; //释放栈占用的动态数组
        
        bool Push(const elemType &e1);
        void display ();
        void pops();
        int pipei(char no[30]);
        void Pushtemp(const elemType &e1); 
};

template <class elemType>
seqStack<elemType>::seqStack(int initSize)//初始化顺序栈
{
	array = new elemType[initSize];
	if (!array) throw illegalSize();
	top=-1;
    maxSize=initSize;
}

template <class elemType>
bool seqStack<elemType>::Push(const elemType &e1)//进栈
{
	if(isFull())
	{
		printf("停车场已满\n"); 
		return false;
	}
	else
	{
		array[++top] = e1;
		printf("目前停车位置:%d\n",top+1);
		printf("进入停车场时间为%d点%d分%d秒\n",array[top].time.hour,array[top].time.min,array[top].time.sec);
		return true;
	}
	
}
template <class elemType>
void seqStack<elemType>::display()//功能3:查看停车场车辆信息 
{
	printf("车牌号\t\t进入时间\t已停时间\t目前费用\t位置\n");
	for(int i = 0;i<=top;i++)
	{
		time_t timep;
		time(&timep);
		struct tm *tin;
		tin = gmtime(&timep);
		int hour = 8 + tin->tm_hour;
	    int min = tin->tm_min;
		int sec = tin->tm_sec;
		
		int zong1 = hour*3600 + min*60 +sec ;
		int zong2 = array[i].time.hour*3600 + array[i].time.min*60 +array[i].time.sec ;
		int zong = zong1 - zong2 ; 
		
		printf("%s\t\t",array[i].num);
		printf("%d点%d分%d秒\t",array[i].time.hour,array[i].time.min,array[i].time.sec);
		printf("%d秒\t\t",zong);
		printf("%d元\t\t",zong*5);
		printf("%d\n",i+1);
	}
}
template <class elemType>
void seqStack<elemType>::pops()   //出栈方式2 
{
	if (top==-1)
	{
		printf("出栈失败"); 
		
	}
	else
	{
		top--;
	}
}
template <class elemType>
int seqStack<elemType>::pipei(char no[30])   //匹配 
{
	for (int i=0; i<=top+1; i++)
	{
		if (i>top)
			printf("未找到该编号的汽车\n");
		if(strcmp(no,array[i].num)==0) 
		{
			return i;
		}
	}  
}
template <class elemType>
void seqStack<elemType>::Pushtemp(const elemType &e1)//进栈2 
{
	if(isFull())
	{
		printf("失败\n"); 
	}
	else
	{
		array[++top] = e1;
		
	}
	
}

3、seqQueue.h

template <class T>
class seqQueue
{   
    public:
    	T *array;
        int maxSize;
    	int Front, Rear;
		         
    	seqQueue(int size=10); //初始化队列元素的存储空间
    	bool isEmpty(); //判断队空否,空返回true,否则为false
    	bool isFull(){return  (Rear+1)%maxSize == Front; };  //判断队满否,满返回true,否则为false
    	bool enQueue(const T &x); //将x进队,成为新的队尾
    	~seqQueue(){ delete [] array;} //释放队列元素所占据的动态数组
    	void display();
    	void deQueue1();
};
template <class T>
seqQueue<T>::seqQueue(int size )
{
	array = new T[size];
	if (!array) throw illegalSize();
	maxSize=size;
	Front=Rear=0;	
}

template <class T>
bool seqQueue<T>::enQueue(const T &x)      /*进队*/
{
	if(!isFull()) 
	{
		array[Rear]=x;
		Rear=(Rear+1)%maxSize;
		printf("进入等待区\n目前等待区位置:%d\n",Rear);
		printf("进入停车场时间为%d点%d分%d秒\n",array[Rear-1].time.hour,array[Rear-1].time.min,array[Rear-1].time.sec);
		return true;
	}
	else
	{
		printf("等待区已满\n"); 
		return false;
	}
}

template <class T>
bool seqQueue<T>::isEmpty()
{
	return Front==Rear; 
}


template <class T>
void seqQueue<T>::display()//展示 
{
	if(isEmpty()) printf("等待区为空");
	else
	{
		printf("车牌号\t\t进入时间\t位置\n");
		for(int i = Front;i<=Rear-1;i++)
		{
			
			printf("%s\t\t",array[i].num);
			printf("%d点%d分%d秒\t",array[i].time.hour,array[i].time.min,array[i].time.sec);
			printf("%d\n",i+1);
		}
	}
	
}
template <class T>
void seqQueue<T>::deQueue1()//出队 
{
	
	if(isEmpty()) printf("等待区为空\n");
	else
	{
		Front = (Front+1)%maxSize;
	 } 
	
	
}

  • 23
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
停车场管理系统可以使用栈或队列来实现。下面以使用队列为例: 1. 定义辆结构体: ```cpp struct Car { string licensePlate; // 牌号 int arrivalTime; // 到达时间 }; ``` 2. 定义停车场类,其中包含入场、出场、查询等操作: ```cpp class ParkingLot { private: queue<Car> cars; // 停车场队列 int capacity; // 停车场容量 int feeRate; // 收费标准 public: ParkingLot(int cap, int rate) : capacity(cap), feeRate(rate) {} bool park(Car car); // 入场 int leave(string licensePlate); // 出场 int query(string licensePlate); // 查询 }; ``` 3. 实现入场操作: ```cpp bool ParkingLot::park(Car car) { if (cars.size() == capacity) return false; // 停车场已满 cars.push(car); return true; } ``` 4. 实现出场操作: ```cpp int ParkingLot::leave(string licensePlate) { int fee = 0; queue<Car> temp; // 临时队列 while (!cars.empty()) { Car car = cars.front(); cars.pop(); if (car.licensePlate == licensePlate) { // 找到辆 fee = (time(NULL) - car.arrivalTime) * feeRate; // 计算费用 break; } temp.push(car); } while (!temp.empty()) { // 将临时队列中的辆重新加入停车场队列 cars.push(temp.front()); temp.pop(); } return fee; } ``` 5. 实现查询操作: ```cpp int ParkingLot::query(string licensePlate) { int time = -1; queue<Car> temp; // 临时队列 while (!cars.empty()) { Car car = cars.front(); cars.pop(); if (car.licensePlate == licensePlate) { // 找到辆 time = time(NULL) - car.arrivalTime; // 计算停时间 break; } temp.push(car); } while (!temp.empty()) { // 将临时队列中的辆重新加入停车场队列 cars.push(temp.front()); temp.pop(); } return time; } ``` 以上就是一个简单的停车场管理系统数据结构的实现。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值