⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 park.cpp

📁 停车场仿真程序仿真原理:首先有两个定时器自动的随机产生请求入场的卡号和请求出场的卡号
💻 CPP
字号:
// Park.cpp: implementation of the CPark class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "FJ.h"
#include "Park.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CPark::CPark(int width, int height)
{
	m_ppitLotArray = new CPoint[width * height];

	ASSERT(m_ppitLotArray);

	//保存车场车位布局
	m_pitMatric.x = width;
	m_pitMatric.y = height;
	//所有的车位都是空的 0为空; 1为占用。
	for(int i =0 ; i < width * height; i++)
	{
		(*(m_ppitLotArray + i)).x  = 0;
		(*(m_ppitLotArray + i)).y = -1;
	}
}

/*************************************************************************
 *
 * 函数名称:
 *   CPark()
 *
 * 参数:
 *   
 * 返回值:
 * 
 *
 * 说明:
 * 该函数为类CPark的无参构造函数
 * 
 * 
 ************************************************************************/
CPark::CPark()
{
	m_ppitLotArray = NULL;
	m_pitMatric.x = 0;
	m_pitMatric.y = 0;
}

/*************************************************************************
 *
 * 函数名称:
 *   ~CPark()
 *
 * 参数:
 *  
 * 返回值:
 * 
 *
 * 说明:
 * 该函数为类CPark的析构函数,清除内存.
 * 
 * 
 ************************************************************************/
CPark::~CPark()
{
	if(m_ppitLotArray != NULL) delete [] m_ppitLotArray;
}


/*************************************************************************
 *
 * 函数名称:
 *   getNumberOfEmpty()
 *
 * 参数:
 *   
 * 返回值:
 *   INT                - 空车位的个数
 *
 * 说明:
 * 该函数用于得到空车位的个数
 * 
 * 
 ************************************************************************/
int CPark::getNumberOfEmptyLot()
{
	ASSERT(m_ppitLotArray);
	int sum = 0;
	for(int i = 0; i < m_pitMatric.x * m_pitMatric.y; i++ )
	{
		if( (*(m_ppitLotArray + i)).x == 0) sum++;
	}

	return sum;
}

/*************************************************************************
 *
 * 函数名称:
 *   isFull()
 *
 * 参数:
 *   
 * 返回值:
 *   BOOL                - 是否车位已满 
 *
 * 说明:
 * 该函数用于得到车位是否已满
 * 
 * 
 ************************************************************************/
BOOL CPark::isFull()
{
	return (0 == getNumberOfEmptyLot());
}

/*************************************************************************
 *
 * 函数名称:
 *   getSize()
 *
 * 参数:
 *   
 * 返回值:
 *   int					- 得到当前正在使用的车位数
 *
 * 说明:
 * 该函数用于得到当前正在使用的车位数.
 * 
 * 
 ************************************************************************/
int CPark::getSize()
{
   return	getCapacity() - getNumberOfEmptyLot();
}

/*************************************************************************
 *
 * 函数名称:
 *   getCapacity()
 *
 * 参数:
 *   
 * 返回值:
 *   int                - 车场的容量
 *
 * 说明:
 * 该函数用于得到车场的容量.
 * 
 * 
 ************************************************************************/
int CPark::getCapacity()
{
	return m_pitMatric.x * m_pitMatric.y;
}

/*************************************************************************
 *
 * 函数名称:
 *   updateLot()
 *
 * 参数:
 *   CPoint loc			- 要更新的车的车位
 *	 int state			- 新的车位状态
 *	 int cardID			- 使用此车位的员工号
 * 返回值:
 *   void
 *
 * 说明:
 * 该函数用于更新车场的车位
 * 
 * 
 ************************************************************************/
void CPark::updateLot(CPoint loc, int state, int cardID)
{
	ASSERT(m_ppitLotArray);
	(*(m_ppitLotArray + loc.y * m_pitMatric.x  + loc.x)).x = state;
	(*(m_ppitLotArray + loc.y * m_pitMatric.x  + loc.x)).y = cardID;
}

/*************************************************************************
 *
 * 函数名称:
 *   getLeftEmpty()
 *
 * 参数:
 *   
 * 返回值:
 *   CPoint                - 车位
 *
 * 说明:
 * 该函数用于得到离入场口最近的空车位
 * 
 * 
 ************************************************************************/
CPoint CPark::getLeftEmpty()
{
	ASSERT(m_ppitLotArray);
	CPoint point;
	point.x = -1;
	point.y = -1;      //2
	for(int j =0 ; j < m_pitMatric.x; j++)
	{                    //3
		for(int i = 0; i < m_pitMatric.y; i++)
		{
			if(0 == (*(m_ppitLotArray + i*m_pitMatric.x + j)).x )
			{
				point.x = j;
				point.y = i;
				return point;
			}
		}	
	}
	return point;
}


/*************************************************************************
 *
 * 函数名称:
 *   getCardPosition()
 *
 * 参数:
 *   int cardID			- 指定查询的员工号.
 * 返回值:
 *   CPoint				- 该卡号当前使用的车位
 *
 * 说明:
 * 该函数用于得到某卡使用的车位,如果没有使用则返回CPoint(-1,-1)
 * 
 * 
 ************************************************************************/
CPoint CPark::getCardPosition(int cardID)
{
	ASSERT(m_ppitLotArray);
	CPoint point;
	point.x = -1;
	point.y = -1;      //2
	for(int i =0 ; i < m_pitMatric.y; i++)
	{                    //3
		for(int j = 0; j < m_pitMatric.x; j++)
		{
			if(cardID == (*(m_ppitLotArray + i*m_pitMatric.x + j)).y )
			{
				point.x = j;
				point.y = i;
				return point;
			}
		}	
	}

	return point;
}

/*************************************************************************
 *
 * 函数名称:
 *   isOccupy()
 *
 * 参数:
 *   CPoint location	- 指定查询的车位.
 * 返回值:
 *   INT                - 产生的随机数
 *
 * 说明:
 * 该函数用于查询车位是否占用.
 * 
 * 
 ************************************************************************/
BOOL CPark::isOccupy(CPoint location)
{
	ASSERT(m_ppitLotArray);

	if(location.x >= 0 && location.x < m_pitMatric.x && location.y >= 0 && location.y < m_pitMatric.y )
	{
		return 1 == (m_ppitLotArray + m_pitMatric.x * location.y + location.x)->x;
	}

	return false;
}


/*************************************************************************
 *
 * 函数名称:
 *   getCard()
 *
 * 参数:
 *   CPoint location	- 指定查询的车位.
 * 返回值:
 *   INT                - 在使用该车位的员工号
 *
 * 说明:
 * 该函数用于特定车位的使用者卡号。
 * 
 * 
 ************************************************************************/
int CPark::getCard(CPoint location)
{
	ASSERT(m_ppitLotArray);

	if(location.x >= 0 && location.x < m_pitMatric.x && location.y >= 0 && location.y < m_pitMatric.y )
	{
		return  (m_ppitLotArray + m_pitMatric.x * location.y + location.x)->y;
	}

	return -1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -