www.pudn.com > MyElevator.rar > Step.cpp
#include "stdafx.h"
#include "Step.h"
bool CStep::GetHStop(CStop& stop)
{
for(int i = 4 ; i >= 0 ; --i)
{
if (m_StopVector[i].m_bAlreadyHaveTask)
{
stop = m_StopVector[i];
return true;
}
}
return false;
}
float CStep::GetPartTime(CStop stop , float fHeight)
{
CStop stop1 , stop2;
float ftime = 0.0;
if (!GetHStop(stop1) || !GetLStop(stop2))
{
return 0.0;
}
if (stop2.GetFloor() == stop1.GetFloor())
{
return 0.0;
}
if (UP == m_status)
{
for(int i = stop2.GetFloor() ; i < fHeight ; ++i)
{
ftime += 2.0;
if (!m_StopVector[i-1].IsEmpty())
{
ftime += 2.0;
}
if (stop.GetFloor() >= i)
{
break;
}
}
ftime += ((int)fHeight - fHeight + 1 )/ 1.4;
return ftime;
}
if (DOWN == m_status)
{
for(int i = stop1.GetFloor() ; i > fHeight ; --i)
{
ftime += 2.0;
if (!m_StopVector[i-1].IsEmpty())
{
ftime += 2.0;
}
if (stop.GetFloor() <= i)
{
break;
}
}
ftime += (fHeight - (int)fHeight)/ 1.4;
return ftime;
}
return 0.0;
}
bool CStep::Empty()
{
for(int i = 0 ; i < 5 ; ++i)
{
if (!m_StopVector[i].IsEmpty())
{
return false;
}
}
return true;
}
bool CStep::CanLoad(CStop stop1 , CStop stop2)
{
if (Empty())
{
return true;
}
else
{
if (stop2.GetFloor() > stop1.GetFloor() && UP == m_status)
{
CStop stop3;
GetLStop(stop3);
if (stop1.GetFloor() >= stop3.GetFloor())
{
return true;
}
}
if (stop2.GetFloor() < stop1.GetFloor() && DOWN == m_status)
{
CStop stop3;
GetHStop(stop3);
if (stop1.GetFloor() <= stop3.GetFloor())
{
return true;
}
}
}
return false;
}
float CStep::GetAllTime()
{
float ftime = 0.0;
CStop stop1 , stop2;
GetHStop(stop2);
GetLStop(stop1);
if (!GetHStop(stop2) || !GetHStop(stop1))
{
return 0.0;
}
for(int i = stop1.GetFloor() ; i < stop2.GetFloor() ; ++i)
{
ftime += 2.0;
if (!m_StopVector[i-1].IsEmpty())
{
ftime += 2.0;
}
}
ftime += 2.0;
return ftime;
}
bool CStep::GetLStop(CStop& stop)
{
for(int i = 1 ; i <= 5 ; ++i)
{
if (m_StopVector[i-1].m_bAlreadyHaveTask)
{
stop = m_StopVector[i-1];
return true;
}
}
return false;
}
CStep::CStep()
{
m_status = WAIT;
}
CStep::~CStep()
{
}
bool CStep::InsertStop(_STATUS status , bool bFirst,UINT iHeight ,CStop stop1 , CStop stop2)
{
if (stop1.GetFloor() > stop2.GetFloor())
{
if (bFirst)
{
if (DOWN == status)
{
CStop stop ,stop3;
if(!GetHStop(stop))//空的
{
m_StopVector[stop1.GetFloor()-1] = stop1;
m_StopVector[stop2.GetFloor()-1] = stop2;
SetStatus(DOWN);
return true;
}
else
{
if (stop.GetFloor() >= stop1.GetFloor())
{
GetLStop(stop3);
// if((!stop.IsEmpty()) || (iHeight / 10.0 + 1.0) >= stop1.GetFloor())
if (stop3.GetFloor() <= stop2.GetFloor() &&
(iHeight / 10.0 + 1.0) >= stop1.GetFloor())
{
if (m_StopVector[stop1.GetFloor()-1].IsEmpty())
{
m_StopVector[stop1.GetFloor()-1] = stop1;
}
else
{
m_StopVector[stop1.GetFloor()-1].EmbedNewStop(stop1);
}
if (m_StopVector[stop2.GetFloor()-1].IsEmpty())
{
m_StopVector[stop2.GetFloor()-1] = stop2;
}
else
{
m_StopVector[stop2.GetFloor()-1].EmbedNewStop(stop2);
}
return true;
}
}
}
}
}
else
{
if (DOWN == m_status)
{
CStop stop ,stop3;
if(!GetHStop(stop))//空的
{
m_StopVector[stop1.GetFloor()-1] = stop1;
m_StopVector[stop2.GetFloor()-1] = stop2;
SetStatus(DOWN);
return true;
}
else
{
if (stop.GetFloor() >= stop1.GetFloor())
{
GetLStop(stop3);
// if((!stop.IsEmpty()) || (iHeight / 10.0 + 1.0) >= stop1.GetFloor())
if (stop3.GetFloor() <= stop2.GetFloor())
{
if (m_StopVector[stop1.GetFloor()-1].IsEmpty())
{
m_StopVector[stop1.GetFloor()-1] = stop1;
}
else
{
m_StopVector[stop1.GetFloor()-1].EmbedNewStop(stop1);
}
if (m_StopVector[stop2.GetFloor()-1].IsEmpty())
{
m_StopVector[stop2.GetFloor()-1] = stop2;
}
else
{
m_StopVector[stop2.GetFloor()-1].EmbedNewStop(stop2);
}
return true;
}
}
}
}
}
}
else if (stop1.GetFloor() < stop2.GetFloor())
{
if(bFirst)
{
if (UP == status)
{
CStop stop ,stop3;
if(!GetLStop(stop))//空的
{
m_StopVector[stop1.GetFloor()-1] = stop1;
m_StopVector[stop2.GetFloor()-1] = stop2;
SetStatus(UP);
return true;
}
else
{
if (stop.GetFloor() <= stop1.GetFloor())
{
GetHStop(stop3);
// if((!stop.IsEmpty()) || (iHeight / 10.0 + 1.0) <= stop1.GetFloor())
if(stop3.GetFloor() >= stop2.GetFloor() &&
(iHeight / 10.0 + 1.0) <= stop1.GetFloor())
{
if (m_StopVector[stop1.GetFloor()-1].IsEmpty())
{
m_StopVector[stop1.GetFloor()-1] = stop1;
}
else
{
m_StopVector[stop1.GetFloor()-1].EmbedNewStop(stop1);
}
if (m_StopVector[stop2.GetFloor()-1].IsEmpty())
{
m_StopVector[stop2.GetFloor()-1] = stop2;
}
else
{
m_StopVector[stop2.GetFloor()-1].EmbedNewStop(stop2);
}
return true;
}
}
}
}
}
else
{
if (UP == m_status)
{
CStop stop ,stop3;
if(!GetLStop(stop))//空的
{
m_StopVector[stop1.GetFloor()-1] = stop1;
m_StopVector[stop2.GetFloor()-1] = stop2;
SetStatus(UP);
return true;
}
else
{
if (stop.GetFloor() <= stop1.GetFloor())
{
GetHStop(stop3);
// if((!stop.IsEmpty()) || (iHeight / 10.0 + 1.0) <= stop1.GetFloor())
if(stop3.GetFloor() >= stop2.GetFloor())
{
if (m_StopVector[stop1.GetFloor()-1].IsEmpty())
{
m_StopVector[stop1.GetFloor()-1] = stop1;
}
else
{
m_StopVector[stop1.GetFloor()-1].EmbedNewStop(stop1);
}
if (m_StopVector[stop2.GetFloor()-1].IsEmpty())
{
m_StopVector[stop2.GetFloor()-1] = stop2;
}
else
{
m_StopVector[stop2.GetFloor()-1].EmbedNewStop(stop2);
}
return true;
}
}
}
}
}
}
else
return false;
return false;
}
bool CStep::IsPassStep()
{
for(int i = 0 ; i < 5 ; ++i)
{
if (!m_StopVector[i].IsEmpty())
{
return false;
}
}
return true;
}
bool CStep::IsGoToTaskStop(UINT iHeight ,UINT &in , UINT &out , bool & IsAttainFloor, bool & b)
{
int Floor = iHeight/10;
b = false;
IsAttainFloor = false;
if (0 == (iHeight%10))
{
IsAttainFloor = true;
if (!m_StopVector[Floor].IsEmpty())//有任务
{
in = m_StopVector[Floor].GetInNum();
out = m_StopVector[Floor].GetOutNum();
m_StopVector[Floor].DeleteTask();
if (IsPassStep())
{
b = true;
}
return true;
}
}
return false;
}