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

📄 fjview.cpp

📁 停车场仿真程序仿真原理:首先有两个定时器自动的随机产生请求入场的卡号和请求出场的卡号
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		write(c);
		CView::OnTimer(nIDEvent);
		return;
	}
	else if(WM_ROLL_IN == nIDEvent)
	{
		//松开过道使用权
		m_bLotLocked = false;

		//清空过道
		setEmpty();
		m_bHasStartRollIn = false;

		//不忘关入口
		if(m_defence.isInON() && m_defence.m_intInMoveing == false) 
		{
			SetTimer(WM_DEFENCE1_DOWN, WM_DEFENCE1_DOWN_INTERVAL, NULL);
			m_defence.m_intInMoveing = true;
		}

		CPoint p = m_park.getCardPosition(m_intRecentIn);

#ifdef _DEBUG
		if(intRecent == -1)
		{
			CString s ="不应该看到的对话框,进车位事件处理时intRecent的值不是期望的是-1!";	
			
			write((LPSTR)(LPCTSTR)s);
		}
#endif
		if(p.x >= 0 && p.y >= 0)
		{
			m_park.updateLot(p, 1, m_intRecentIn);
			
			CString s;
			s.Format("WM_ROLL_IN车辆 %d完成进入车位!", m_intRecentIn);
			write((LPSTR)(LPCTSTR)s);
			m_intRecentIn = -1;
		}
		else
		{
#ifdef _DEBUG//错误信息提示
				MessageBox("不应该看到的对话框,进车位事件处理时intRecent的值不是期望的!", "错误提示",MB_ICONERROR | MB_OK);
#endif
		}
		

		KillTimer(nIDEvent);
		CView::OnTimer(nIDEvent);
		return;
	}

	//车辆行驶出车位
	else if(WM_ROLL_OUT == nIDEvent)
	{
		if(m_intRecentOut != -1)
		{
			m_bLotLocked = true;
			CPoint loc = m_park.getCardPosition(m_intRecentOut);
			
			if(loc.x >= 0 && loc.y >= 0)
			{
				setEmpty();
				setObstacle(loc.x);

				//过道上的车要准备出去哦
				m_bincome = false;
			}
			else 
			{
#ifdef _DEBUG//错误信息提示
				MessageBox("不应该看到的对话框,场内集与车场实际数据不一致!", "错误提示",MB_ICONERROR | MB_OK);
#endif
			}
		}
#ifdef _DEBUG//调试版本错误信息
		else 
		{

				MessageBox("不应该看到的对话框,出场卡号错误!", "错误提示",MB_ICONERROR | MB_OK);
		}
#endif	

		LPSTR c = "WM_ROLL_OUT车辆完成离开车位!" ;
		write(c);
		KillTimer(nIDEvent);
		m_bHasStartRollOut = false;
		CView::OnTimer(nIDEvent);
		return;
	}

	//随机产生请求入场员工号
	else if(WM_IN == nIDEvent)
	{
		int n ;
		m_queIncomeCars.push(n = getRam( m_park.getCapacity() / 2));

		CString s ;
		s.Format("WM_IN产生卡号 %d",n);
		write((LPSTR)(LPCTSTR)s);
		CView::OnTimer(nIDEvent);
		return;
	}

	//随机产生请求出场员工卡号
	else if(WM_OUT == nIDEvent)
	{
		//随机产生要求出场的车位号。
		int n;
		m_queOutCars.push( n = getRam( theApp.m_intE));

		CString s ;
		s.Format("WM_OUT出场员工卡号 %d",n);
		write((LPSTR)(LPCTSTR)s);
		CView::OnTimer(nIDEvent);
		return;
	}

	//每秒钟处理一次场务
	else if(WM_ELAPSE == nIDEvent)
	{
		m_fTotalTime += m_park.getSize();

		//排除所有非本公司员工的卡号。
		while( m_queIncomeCars.size() != 0 && (!m_ccCards.isValid(m_queIncomeCars.front()) 
				|| m_ccCards.findCard(m_queIncomeCars.front())))//或本卡当前已经入场
		{
			//读卡时间是1秒
			Sleep(1000);

			int n = m_queIncomeCars.front();
			m_queIncomeCars.pop();
			CString s ;
			s.Format("WM_ELAPSE排除非本公司员工或已经入场的卡号 %d",n);
			write((LPSTR)(LPCTSTR)s);
		}
		
		//如果车场车位已满,则全部拒绝入场。清空等待队列。
		if(m_park.isFull())
		{
			//不需要读卡。
			while(m_queIncomeCars.size() != 0) 
			{
				int n = m_queIncomeCars.front();
				CString s ;
				s.Format("WM_ELAPSE车场车位已满清除 %d",n);
				write((LPSTR)(LPCTSTR)s);
				m_queIncomeCars.pop();
			}
		}

		//过道是空的。有车要进入.第一种情况发生了。
		if( !isObstacle() && m_queIncomeCars.size() != 0) 
		{

			//有车等着要进场,且进场杆放下,且杆静止的。
			if(!m_defence.isInON() && !m_defence.m_intInMoveing)
			{
				LPSTR c = "过道是空的。有车要进入,进场杆放下,且杆静止的,让其开启" ;
				write(c);
					
				//发出抬进杆指令
				SetTimer(WM_DEFENCE1_UP, WM_DEFENCE1_UP_INTERVAL, NULL);
				m_defence.m_intInMoveing = true;
				m_bincome = true;
					
				m_intRecentIn = m_queIncomeCars.front();
									
				//删除入场队列中第一个卡号,把卡号放入等他的车位
				CPoint empty = m_park.getLeftEmpty();

				//虽然车还没有到位,但是先预定了等他的车位
				m_park.updateLot(empty, 1, m_intRecentIn );

				//把卡号加入场内集
				m_ccCards.AddCard(m_queIncomeCars.front());
				m_queIncomeCars.pop();

				m_ulTotalIn++;

				//预定过道使用权
				m_bLotLocked = true;
			}
			else if(m_defence.isInON())
			{
				LPSTR c = "过道是空的。有车要进入,进场杆已开启" ;
				write(c);
				
			}
			
			CView::OnTimer(nIDEvent);
			return;
		}//第一种情况处理结束

		//过道上有车在行进,第二种情况发生了。
		else if(isObstacle())
		{
			LPSTR c = "过道上有车在行进" ;
			write(c);

			//有进场车在过道上
			if(m_bincome)
			{
				//得到在等他的车位
				CPoint empty = m_park.getCardPosition(m_intRecentIn);
				
				CString s;
				s.Format( "过道上有车在行进,有进场车在过道上,等他的车位是X:%d,Y:%d,当前已到:%d", empty.x, empty.y,getObstaclePosition());
				write((LPSTR)(LPCTSTR)s);

				if(getObstaclePosition() >= 1 && m_defence.isInON() &&  m_defence.m_intInMoveing == false)
				{
					SetTimer(WM_DEFENCE1_DOWN, WM_DEFENCE1_DOWN_INTERVAL, NULL );
					m_defence.m_intInMoveing = true;
				}

				//如果还没到达等他的车位
				if(getObstaclePosition() < empty.x + 1)
				{
					int next = 1 + getObstaclePosition();
					setEmpty();
					setObstacle(next);
					m_bLotLocked = true;
		
					LPSTR c = "过道上有车在行进,有进场车在过道上,还没到达等他的车位" ;
					write(c);
					
					//如果入口处于打开状态且车尾过杆。则让其2秒后关闭。
					if(m_defence.isInON() && getObstaclePosition() == 0 )
					{
						LPSTR c = "过道上有车在行进,有进场车在过道上,还没到达等他的车位,如果入口处于打开状态且车尾过杆。则让其2秒后关闭" ;
						write(c);
						SetTimer(WM_DEFENCE1_DOWN, WM_DEFENCE1_DOWN_INTERVAL, NULL);
						m_defence.m_intInMoveing = true;
					}	
				}
					
				//到达空车位
				else
				{
					if(!m_bHasStartRollIn)
					{
						LPSTR c = "过道上有车在行进,有进场车在过道上,到达空车位" ;
						write(c);

						SetTimer(WM_ROLL_IN, WM_ROLL_INTERVAL, NULL);
						m_bHasStartRollIn = true;
					}
				}
			}
		
			//有出场车在过道上
			else
			{
				//还没有到达出口
				if(getObstaclePosition() < theApp.m_intN + 1)
				{
					LPSTR c = "过道上有车在行进,有出场车在过道上,还没有到达出口" ;
					write(c);
					int next = 1 + getObstaclePosition();
					setEmpty();
					setObstacle(next);
					m_bLotLocked = true;
				}

				//车已到达出口.
				else
				{

					if(!m_defence.isOutON() && m_defence.m_intOutMoveing == false)
					{
						LPSTR c = "过道上有车在行进,有出场车在过道上,车已到达出口,	出口处于关闭状态,则让其打开" ;
						write(c);
						SetTimer(WM_DEFENCE2_UP, WM_DEFENCE2_UP_INTERVAL, NULL);		
						m_defence.m_intOutMoveing = true;
					}
					
					if(m_defence.isOutON() && m_defence.m_intOutMoveing == false)
					{
						CPoint outLocation = m_park.getCardPosition( m_intRecentOut);
						Sleep(2000);
	
						//得到的是有效车位
						if(outLocation.x >= 0 && outLocation.y >= 0)
						{
							//从场内集中删除当前卡号
							m_ccCards.deleteCard( m_intRecentOut);
							CString s ;
							s.Format("过道上有车在行进,有出场车在过道上,车已到达出口,处于开启状态,卡号是有效车位,%d 出场成功", m_intRecentOut);
							write((LPSTR)(LPCTSTR)s);

							m_ulTotalOut++;
					
							//腾出车位.
							m_park.updateLot(outLocation, 0, -1);

							//关闭出口栅栏.
							SetTimer(WM_DEFENCE2_DOWN, WM_DEFENCE2_DOWN_INTERVAL, NULL);
							m_defence.m_intOutMoveing = true;
							m_intRecentIn = -1;
						}

						//没有达到正确返回位置
						else
						{
#ifdef _DEBUG
							MessageBox("不应该看到的对话框,场内集与车场实际数据不一致!", "错误提示",MB_ICONERROR | MB_OK);
#endif
						}
					}
					else if(!m_ccCards.findCard(m_intRecentOut))//不在场内的本公司卡号到达出场杆想要出场
					{
						m_intRecentOut = -1;
						setEmpty();
						m_bLotLocked = false;
					}
				}
			}

			CView::OnTimer(nIDEvent);
			return;
		}//第二种情况处理结束了。
		

		//现在处理出场请求。如果有车请求了的话
		 if(m_queOutCars.size() != 0 && m_defence.m_intInMoveing == false)
		{
			//无效卡号(非本公司员工卡号)通通去掉,1~N合法
			while (m_queOutCars.size() != 0
						&&( !m_ccCards.isValid( m_queOutCars.front()) //非本公司卡号。
						|| !m_ccCards.findCard( m_queOutCars.front())))//合法卡却不在场内集中,也请出去
			{
				LPSTR c = "处理出场请求,遇到非本公司员工卡号,合法卡却不在场内集中" ;
				write(c);
				m_queOutCars.pop();
			}

			//如果过道不忙。且还有出场请求
			if(m_bLotLocked == false && 0 != m_queOutCars.size())
			{
				//琐定过道
				m_bLotLocked = true;

				//供执行出车位指令时使用.
				m_intRecentOut = m_queOutCars.front();
				m_queOutCars.pop();
				LPSTR c = "处理出场请求,如果过道不忙。且还有出场请求,发出出车位指令" ;
				write(c);
				//发出出车位指令
				if(!m_bHasStartRollOut)
				{	m_bHasStartRollOut = true;
					SetTimer(WM_ROLL_OUT, WM_ROLL_INTERVAL, NULL);	
				}
			}
		}
	}

	CView::OnTimer(nIDEvent);
}

/*************************************************************************
 *
 * 函数名称:
 *   getRam()
 *
 * 参数:
 *   int max			- 指定产生数的最大值,只能是1到该值.
 * 返回值:
 *   INT                - 产生的随机数
 *
 * 说明:
 * 该函数用于产生的随机数
 * 
 * 
 ************************************************************************/
INT CFJView::getRam(int max)
{
	if(max > 1)
	{
		srand((unsigned)time(NULL));
		return rand()%max + 1; //1~max
	}
	else
	{
		return 1;
	}
}

/*********************************************************************
 * 函数名 isObstacle()
 * 
 * 类型: BOOL
 * 功能:判断是否有车在过道上。
 *********************************************************************/
BOOL CFJView::isObstacle()
{
	for(int i = 0; i < m_vctLots.size(); i++)
	{
		if(m_vctLots[i] != 0)
			return TRUE;
	}
	return FALSE;
}

/*************************************************************************
 *
 * 函数名称:
 *   setObstacle()
 *
 * 参数:
 *   int hipPOS			- 指指定车尾在过道上的位置.
 * 返回值:
 *   void
 *
 * 说明:
 * 该函数用于设置车辆在过道上的位置
 * 
 * 
 ************************************************************************/
VOID CFJView::setObstacle(int hipPOS )
{
	if(hipPOS >= 0 && hipPOS < theApp.m_intN + 2)
	{
		m_vctLots[hipPOS] = 1;
	}
}

/*************************************************************************
 *
 * 函数名称:
 *   setEmpty()
 *
 * 参数:
 *   
 * 返回值:
 *   void
 *
 * 说明:
 * 该函数用于过道占用标记.
 * 
 * 
 ************************************************************************/
VOID CFJView::setEmpty()
{
	for(int i = 0; i < m_vctLots.size(); i++)
	{
		m_vctLots[i] = 0;
	}
}

/*************************************************************************
 *
 * 函数名称:
 *   getObstaclePosition()
 *
 * 参数:
 *   
 * 返回值:
 *   
 *
 * 说明:
 * 该函数用于产生得到当前在过道上的车辆的位置.如果过道是空的则返回-1
 * 
 * 
 ************************************************************************/
INT CFJView::getObstaclePosition()
{
	if(!isObstacle()) return -1;
	for(int i = 0; i < m_vctLots.size(); i++)
	{
		if(1 == m_vctLots[i])
		{
			return i;	
		}
	}

	return -1;
}

/*************************************************************************
 *
 * 函数名称:
 *   write()
 *
 * 参数:
 *   LPCSTR str			- 要写进文件的内容
 * 返回值:
 *   void
 *
 * 说明:
 * 该函数用于写日志
 * 
 * 
 ************************************************************************/
VOID CFJView::write(LPSTR str)
{
	CString time = CMyTime::getCurrentTime();
	m_filOut.Write((LPSTR)(LPCTSTR)time, strlen((LPSTR)(LPCTSTR)time));

	m_filOut.Write(str, strlen(str) );
	LPCSTR s = "\r\n";
	m_filOut.Write(s, strlen(s));

}

⌨️ 快捷键说明

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