【实习】C++实现数据结构停车场

  • 代码点小子:输入判定循环操作
  1. 停车场的头文件

(1)停车场需要设计的各种数据结构类,包括顺序栈,链式队列,车辆信息
(2)其中两个数据结构因为常用,所以设计为模板,包含了初始化,增加数据,删除数据的函数,可以直接套用其他数据类型

#pragma once
#define maxsize  2


template <typename T>
class stackls
{
public:
	T stacks[maxsize];			//顺序栈,停车场的容量和临时出来的车子
	int top=0;
	stackls() {};
	~stackls() {};
	void push(T data);
	T pop(T data);
};


template<typename T>
inline void stackls<T>::push(T data)
{
	if (this->top == maxsize)
		cout << "栈已满!";
	else {
		
		this->stacks[this->top] = data;
		this->top++;
	}
}



template<typename T>
inline T stackls<T>::pop(T data)
{
	if (this->top < 0)
		cout << "栈空!";
	else {
		this->top--;
		data = this->stacks[this->top];				//每一次清栈都只是让top值变,事实上栈数组只是不断重新赋值而已,无法置空的
		//this->stacks[this->top] = 0;
	}
	return data;
}

template <typename T>
class Lnode			//链式队列声明,存储待进入停车场的车辆
{
public:
	T data;
	Lnode *next=NULL;
};

template <typename T>
class LinkQueue
{
public:									//如果是私有的,在车辆信息类里调用的这个队列是不显示成员的
	Lnode<T> *front;
	Lnode<T> *rear;
	void creat();
	void append_queue(T dat);
	T delete_queue();
	int order();
};

template<typename T>
inline void LinkQueue<T>::creat()			//必须初始化!
{
	this->front = this->rear = new Lnode<T>();
	this->front->next = NULL;
}

template<typename T>
inline void LinkQueue<T>::append_queue(T dat)
{
	Lnode<T> *p=new Lnode<T>();
	p->data = dat;
	p->next = NULL;
	this->rear->next = p;
	this->rear = p;
}

template<typename T>
inline T LinkQueue<T>::delete_queue()
{
	Lnode<T> *p = new Lnode<T>();
	p = this->front->next;
	this->front->next = p->next;
	if (p->next == NULL)
	{
		this->rear = this->front;
	}
	T info = p->data;
	free(p);
	return	info;
}

template<typename T>
inline int LinkQueue<T>::order()
{
	int a = 0;
	Lnode<T> *p=this->front;
	while (p->next != NULL)
	{
		p = p->next;
		a++;

	}
	return a;
}
  


class CarInfo
{

public:
	char status;					//到达(A)or离开(D)
	char* license;				//车牌号
	int hour;						//时刻
	int minute;

	CarInfo() {};
	CarInfo(char status, char* license, int hour, int minute);
	void parking(stackls<CarInfo> *stack_lots, LinkQueue<CarInfo>* waitint_queue);				//停车判断入后出
	int find_licen(stackls<CarInfo> *stack_lots, char* licen);	//出车库查找其位置
	void waiting(LinkQueue<CarInfo>* waiting_queue,int statue, stackls<CarInfo>* stack_lots);					//便道等待车辆
	void temp_stack(stackls<CarInfo> *stack_lots, int find_number);			//临时栈调出其他挡路车辆
	void time_money(CarInfo &car_in);				//计算价格,形参为找到的车辆信息
	
	CarInfo operator=(const CarInfo &c)
	{
		hour = c.hour;
		minute = c.minute;
		license = c.license;
		status = c.status;
		return *this;
	}
};


  1. 停车场的cpp文件
  • 主要分为几个主要功能:

(1)车辆信息加入停车场
(2)多余车辆转移到链式队列
(3)车辆离开停车场
(4)链式队列的车辆弹出再加入停车场

// ParkingLots.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include"muban.h"
#include<iostream>
#include<string>
using namespace std;  


CarInfo::CarInfo(char sta, char* lice, int hr, int mi)
{
	hour = hr;
	minute = mi;
	status = sta;
	license = lice;
}


void CarInfo::parking(stackls<CarInfo> *stack_lots, LinkQueue<CarInfo>* waitint_queue)
{
	if (this->status == 'a'|| this->status == 'A')
	{
		if (stack_lots->top != maxsize)
		{
			stack_lots->push(*this);						//进行完压栈操作后top就已经+1了
					
		}
		else if (stack_lots->top >= maxsize) {
			cout << "车库已满,已为您安排便道等候…………\n";
			
			waiting(waitint_queue,1, stack_lots);
		}
	}
	else if (this->status == 'd'|| this->status == 'D')
	{
		if(stack_lots->top==0)
		{
			cout << "  输入错误,车库已经空了!";
		}
		else if (stack_lots->top > 0) 
		{
			CarInfo drive_in;

			int find_num = find_licen(stack_lots, this->license);
			drive_in.operator=(stack_lots->stacks[find_num]);
			if (find_num + 1 == stack_lots->top) {					//此时find为该栈数组末尾下标值+1,所以find-1才表示栈数组最后一项
				stack_lots->pop(*this);
				this->time_money(drive_in);
			}
			else if (find_num + 1 != stack_lots->top) {				//此时弹出肯定不行,但可以找到栈该车辆的信息直接算价格
				temp_stack(stack_lots, find_num);			
				
				this->time_money(drive_in);
			}
			if (waitint_queue->front->next != NULL) {
				waiting(waitint_queue, 2, stack_lots);
			}
		}
	}
}

int CarInfo::find_licen(stackls<CarInfo>* stack_lots, char * licen)
 {
	int a = 0;
	for (; a < maxsize; a++)
	{
		if (strcmp( stack_lots->stacks[a].license , licen)==0)
		{
			cout << " 此车辆在停车场的第" << a + 1 << "个位置\n";
			return a;
		}
	}
	return a;
}
 
void CarInfo::waiting(LinkQueue<CarInfo>* waiting_queue, int statue, stackls<CarInfo>* stack_lots)
{
	int ordr = 0;
	switch (statue) 
	{
	case 1:
		waiting_queue->append_queue(*this);
		ordr = waiting_queue->order();
		cout << "你的车辆已成功在便道找到位置,在便道第 "<<ordr<<" 个位置";
		break;
	case 2:
		CarInfo first_wait= waiting_queue->delete_queue();
		cout << " \n车牌号为 "<<first_wait.license<<" 的车主,您的车现在可以移出便道,进入停车场了,更新您的停车时间为 "<<this->hour<<" 点 "<<this->minute<<" 分\n";
		first_wait.hour = this->hour;
		first_wait.minute = this->minute;
		stack_lots->push(first_wait);
		break;
	}

}

void CarInfo::temp_stack(stackls<CarInfo>* stack_lots, int find_number)
{
	stackls<CarInfo>* temp = new stackls<CarInfo>;
	CarInfo dri_in;
	while (stack_lots->top != find_number)
	{
		dri_in = stack_lots->pop(dri_in);
		if (stack_lots->top == find_number) {
			break;
		}
		temp->push(dri_in);
	}
	while (temp->top != 0) 
	{
		dri_in = temp->pop(dri_in);
		stack_lots->push(dri_in);
	}
	cout << " 在您后面进入了 " << maxsize - find_number - 1 << " 辆车,需要让其他车辆先倒出来…………\n";
	cout << " 现在已经将你的车辆挪出来啦!\n";
}

void CarInfo::time_money(CarInfo & car_in)
{

	int period_hour = this->hour - car_in.hour - 1;
	int period_minute = 60 + this->minute - car_in.minute;
	if (period_hour == -1)
	{
		period_hour = 0;
		period_minute= this->minute - car_in.minute;
	}
	if (period_hour == 0)
	{
		if (period_minute >= 60) {
			period_hour = 1;
			period_minute -= 60;
		}
	}
	if (period_minute >= 60) {
		period_hour += 1;
		period_minute = 0;
	}
	cout <<"\n车牌号为 "<<this->license<< " 的车辆停车时长为:	  " << period_hour << "小时" << period_minute << "分钟";
}

int main()
{
	stackls<CarInfo> *stack_lots = new stackls<CarInfo>();		//顺序栈,停车场的容量和临时出来的车子
	LinkQueue<CarInfo>* waiting = new LinkQueue<CarInfo>(); 	//便道等待的链式队列
	waiting->creat();
	char a='Y';
	while (a=='Y'|| a == 'y')
	{
		cout << "********************************\n";
		char sta;
		char* licen = new char[20];
		int h, m;
		cout << "请输入进出状态(A or D)" << '\n';
		cin >> sta;
		cout << "\n请输入车牌号" << '\n';
		cin >> licen;
		cout << "\n请输入时刻(时 分)" << '\n';
		cin >> h >> m;
		CarInfo car(sta, licen, h, m);
		car.parking(stack_lots, waiting);
		
		cout << "\n\n是否继续操作?(Y/N): ";
		cin >> a;

	}
    return 0;
}

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值