?? 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()
{
}
//接收某個樓層發(fā)出的向上呼叫請求
//floor代表發(fā)出呼叫的樓層
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;
}
}
}
//接收某個樓層發(fā)出的向下呼叫請求
//floor代表發(fā)出呼叫的樓層
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;
}
}
}
//接收電梯內(nèi)部乘客發(fā)出的到達某一目標(biāo)樓層的呼叫請求
//floor代表發(fā)出的目標(biāo)樓層呼叫
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;
}
}
}
//處理用戶按下電梯轎廂內(nèi)的“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);} */
}
//獲取當(dāng)前電梯所處的位置
float CElevator::GetLocation()
{
return m_location;
}
//獲取電梯的運行狀態(tài)
int CElevator::GetStatus()
{
return m_direction;
}
//電梯運行的中央控制邏輯
//主要功能是確定電梯運行的下一個目標(biāo)樓層
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;
}
//設(shè)置定時器
//參數(shù)表示設(shè)置的定時器時間間隔,單位為毫秒
void CElevator::SetMyTimer(UINT nElapse)
{
m_timerID = ::SetTimer(NULL,NULL,nElapse,MyTimerProc);
pThis=this;
}
//取消定時器
void CElevator::KillMyTimer()
{
KillTimer (NULL,m_timerID);
}
//定時器觸發(fā)事件
//后面的參數(shù)是預(yù)定義的,與邏輯無關(guān)。
void CALLBACK CElevator::MyTimerProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime)
{
//
pThis->CentralControl ();
pThis->IsStay ();
pThis->Move ();
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -