📄 elevator1.cpp
字号:
// Elevator1.cpp: implementation of the CElevator class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Elevator.h"
#include "Elevator1.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CElevator* CElevator::pThis ;
CElevator::CElevator():m_stayAtFloor(16),m_door(4),m_move(8),m_space(0.125)
{
//初始化定时器ID号
m_timerID=0;
m_aim =0;
m_bStay =false;
m_direction =0;
for(int i=0;i<9;i++)
{
m_upRequest [i]=0;
m_downRequest [i]=0;
m_destRequest [i]=0;
};
m_destRequest [9]=0;
m_upRequestTime =0;
m_downRequestTime =0;
m_destRequestTime =0;
m_location =FLOOR_1;
m_nextFloor =0;
m_stayTime =0;
}
CElevator::~CElevator()
{
}
//接收某个楼层发出的向上呼叫请求
//floor代表发出呼叫的楼层
void CElevator::RequestUp(int floor)
{
if (m_upRequest [floor-1] ==0 )
{
switch (m_direction)
{
case 1 :
{
if (m_location+1<=floor+1000)
{
m_upRequestTime +=1;
m_upRequest[floor-1]=1;
}
else
{
StopHighLight(UP_1+floor-1);
}
}break;
case 0 :
{
if (m_location <floor+1000)
{
m_direction =1;
m_upRequestTime +=1;
m_upRequest[floor-1]=1;
}
if (m_location >floor+1000)
{
m_direction =-1;
m_upRequestTime +=1;
m_upRequest[floor-1]=1;
}
if (m_location ==floor+1000)
{
StopHighLight (UP_1+floor-1);
}
}break;
case -1 :
{
m_upRequestTime +=1;
m_upRequest[floor-1]=1;
}break;
}
}
}
//接收某个楼层发出的向下呼叫请求
//floor代表发出呼叫的楼层
void CElevator::RequestDown(int floor)
{
if (m_downRequest [floor-1] ==0 )
{
switch (m_direction)
{
case -1 :
{
if (m_location+1>floor+1000)
{
m_downRequestTime +=1;
m_downRequest[floor-1]=1;
}
else
{
StopHighLight(DOWN_2+floor-2);
}
}break;
case 0 :
{
if (m_location <floor+1000)
{
m_direction =1;
m_downRequestTime +=1;
m_downRequest[floor-1]=1;
}
if (m_location >floor+1000)
{
m_direction =-1;
m_downRequestTime +=1;
m_downRequest[floor-1]=1;
}
if (m_location ==floor+1000)
{
StopHighLight (DOWN_2+floor-2);
}
}break;
case 1 :
{
m_downRequestTime +=1;
m_downRequest[floor-1]=1;
}break;
}
}
}
//接收电梯内部乘客发出的到达某一目标楼层的呼叫请求
//floor代表发出的目标楼层呼叫
void CElevator::RequestDest(int floor)
{
if (m_destRequest [floor-1] ==0 )
{
switch (m_direction)
{
case -1 :
{
if (m_location+1>=floor+1000)
{
m_destRequestTime +=1;
m_destRequest[floor-1]=1;
}
else
{
StopHighLight(DEST_1+floor-1);
}
}break;
case 0 :
{
if (m_location <floor+1000)
{
m_direction =1;
m_destRequestTime +=1;
m_destRequest[floor-1]=1;
}
if (m_location >floor+1000)
{
m_direction =-1;
m_destRequestTime +=1;
m_destRequest[floor-1]=1;
}
if (m_location ==floor+1000)
{
StopHighLight (DEST_1+floor-1);
}
}break;
case 1 :
{
if (m_location+1<=floor+1000)
{
m_destRequestTime +=1;
m_destRequest[floor-1]=1;
}
else
{
StopHighLight(DEST_1+floor-1);
}
}break;
}
}
}
//处理用户按下电梯轿厢内的“Go”按钮请求
void CElevator::CloseDoor()
{
m_stayTime=12;
}
//电梯的运行逻辑
void CElevator::Move()
{
switch (m_aim)
{
case 1 : {
HighLight(LIGHT_UP);
m_location +=m_space;
StopHighLight((int)m_location-1);
HighLight((int)m_location);
}
break;
case 0 : {
StopHighLight(LIGHT_UP);
StopHighLight(LIGHT_DOWN);
m_location +=0;
}
break;
case -1 : {
HighLight(LIGHT_DOWN);
m_location -=m_space ;
StopHighLight((int)m_location+1);
HighLight((int)m_location);
}
break;
}
/* if ((m_destRequestTime ==0)){HighLight(GO);}
else {StopHighLight (GO);}
if ((m_destRequestTime ==-1)){HighLight(UP_1);}
else {StopHighLight (UP_1);}
if ((m_destRequestTime ==1)){HighLight(UP_2);}
else {StopHighLight (UP_2);}
if ((m_direction==0)){HighLight(DOWN_2);}
else {StopHighLight (DOWN_2);} */
}
//获取当前电梯所处的位置
float CElevator::GetLocation()
{
return m_location;
}
//获取电梯的运行状态
int CElevator::GetStatus()
{
return m_direction;
}
//电梯运行的中央控制逻辑
//主要功能是确定电梯运行的下一个目标楼层
void CElevator::CentralControl()
{
switch (m_direction)
{
case 1 :
{
if ((m_upRequestTime ==0)&&(m_destRequestTime ==0))
{
if (m_downRequestTime ==0)
{
m_direction=0;
}
else
for (int i=9;i>0;i--)
{
if (m_downRequest [i-1] ==1)
{
if (m_location <1000+i)
{
m_direction=1;
}
if (m_location >1000+i)
{
m_direction=-1;
}
i=1;
}
}
}
}break;
case -1 :
{
if ((m_downRequestTime ==0)&&(m_destRequestTime ==0))
{
if (m_upRequestTime ==0)
{
m_direction=0;
}
else
for (int i=1;i<9;i++)
{
if (m_upRequest [i-1] ==1)
{
if (m_location <1000+i)
{
m_direction=1;
}
if (m_location >1000+i)
{
m_direction=-1;
}
i=9;
}
}
}
}break;
}
}
int CElevator::IsStay()
{
if (m_location==(int)m_location)
{
switch (m_direction )
{
case 1 :
{
if ((m_downRequest [(int)m_location-1001]==1)&&(m_upRequestTime==0)&&(m_destRequestTime==0))
{
m_bStay=true;
m_downRequest [(int)m_location-1001]=0;
m_downRequestTime -=1;
StopHighLight (DOWN_2+(int)m_location-1002);
}
if (m_upRequest [(int)m_location-1001]==1)
{
m_bStay=true;
m_upRequest [(int)m_location-1001]=0;
m_upRequestTime -=1;
StopHighLight (UP_1+(int)m_location-1001);
}
if (m_destRequest [(int)m_location-1001]==1)
{
m_bStay=true;
m_destRequest [(int)m_location -1001]=0;
m_destRequestTime -=1;
StopHighLight (DEST_1+(int)m_location-1001);
}
}break;
case -1 :
{
if ((m_upRequest [(int)m_location-1001]==1)&&(m_downRequestTime==0)&&(m_destRequestTime==0))
{
m_bStay=true;
m_upRequest [(int)m_location-1001]=0;
m_upRequestTime -=1;
StopHighLight (UP_1+(int)m_location-1001);
}
if (m_downRequest [(int)m_location-1001]==1)
{
m_bStay=true;
m_downRequest [(int)m_location-1001]=0;
m_downRequestTime -=1;
StopHighLight (DOWN_2+(int)m_location-1002);
}
if (m_destRequest [(int)m_location-1001]==1)
{
m_bStay=true;
m_destRequest [(int)m_location -1001]=0;
m_destRequestTime -=1;
StopHighLight (DEST_1+(int)m_location-1001);
}
}break;
}
if (m_bStay==true)
{
m_aim=0;
m_stayTime +=1;
if (m_stayTime ==16)
{
m_stayTime =0;
m_bStay=false;
}
}
else
{
StopHighLight(GO);
m_aim =m_direction;
}
}
return m_stayTime;
}
//设置定时器
//参数表示设置的定时器时间间隔,单位为毫秒
void CElevator::SetMyTimer(UINT nElapse)
{
m_timerID = ::SetTimer(NULL,NULL,nElapse,MyTimerProc);
pThis=this;
}
//取消定时器
void CElevator::KillMyTimer()
{
KillTimer (NULL,m_timerID);
}
//定时器触发事件
//后面的参数是预定义的,与逻辑无关。
void CALLBACK CElevator::MyTimerProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime)
{
//
pThis->CentralControl ();
pThis->IsStay ();
pThis->Move ();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -