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

📄 person.cpp

📁 某国际贸易中心共有40层
💻 CPP
字号:
# include <iostream.h>
# include <stdlib.h>
# include <math.h>
# include "person.h"

person::person()
{
  floorNowOn=-1;
  WaitingForElev=false;
  destination=-1;
  maxWaitTime=0;
  takingStairs=false;
  elevNowIn=-1;
}

void person::action(void)
{
  if (floorNowOn<0)
  {
    if (WantsToEnter())
    {
      destination=EnterDest();
      floorNowOn=0;
      maxWaitTime=Maxwait();
      WaitingForElev=true;
      takingStairs=false;
      elevNowIn=-1;
      totalPeople++;
      inBuilding++;
    }
  }
  else
  {
    if (WaitingForElev)
    {
      if ((maxWaitTime--)<=0)
      {
	WaitingForElev=false;
	takingStairs=true;
	maxWaitTime=30*abs(destination-floorNowOn);
	tookstair++;
      }
    }
    else
    {
      if (takingStairs)
      {
	if (maxWaitTime--<=0)
	{
	  floorNowOn=destination;
	  takingStairs=false;
	  maxWaitTime=Business();
	}
      }
      else
	if (elevNowIn<0)
	{
	  if (maxWaitTime--<=0)
	  {
	    if (leaving())
	      destination=0;
	    else
	      destination=EnterDest();
	    if (destination==floorNowOn)  destination=0;
	    maxWaitTime=Maxwait();
	    WaitingForElev=true;
	  }
	}
     }
  }
  if ((floorNowOn==0)&&(destination==0))
  {
    floorNowOn=-1;
    leftBuilding++;
    inBuilding--;
  }
}

bool person::upWaiting(int floorNumber)
{
  return WaitingForElev&&(floorNowOn==floorNumber)&&(destination>floorNowOn);
}

bool person::dnWaiting(int floorNumber)
{
  return WaitingForElev&&(floorNowOn==floorNumber)&&(destination<floorNowOn);
}

bool person::loadIfWaiting(int elevNumber,int floorNumber,int &pdest)
{
  if (WaitingForElev&&(floorNowOn==floorNumber))
  {
    WaitingForElev=false;
    elevNowIn=elevNumber;
    pdest=destination;
    return true;
  }
  return false;
}

bool person::loadIfGoing(int elevNumber,int floorNumber,direction dir,int &pdest)
{
  direction pdir;
  if (destination>floorNumber)
    pdir=UP;
  else
    pdir=DOWN;
  if (WaitingForElev&&(floorNowOn==floorNumber)&&(dir==pdir))
  {
    WaitingForElev=false;
    elevNowIn=elevNumber;
    pdest=destination;
    return true;
  }
  return false;
}

bool person::discharge(int elevNumber,int floorNumber)
{
  if ((elevNowIn==elevNumber)&&(destination==floorNumber))
  {
    elevNowIn=-1;
    floorNowOn=destination;
    if (floorNowOn!=0)  maxWaitTime=Business();
    return true;
  }
  else
  return false;
}

void perSet::action()
{
  for (int i=0;i<MAXPERSONS;i++)
    pa[i].action();
}
void perSet::numWaiting(int floorNumber,int &nup,int &ndn)
{
  nup=ndn=0;
  for (int i=0;i<MAXPERSONS;i++)
  {
    nup+=pa[i].upWaiting(floorNumber);
    ndn+=pa[i].dnWaiting(floorNumber);
  }
}

bool perSet::loadAny(int elevNumber,int floorNumber,int &pdest)
{
  for (int i=0;i<MAXPERSONS;i++)
    if (pa[i].loadIfWaiting(elevNumber,floorNumber,pdest))  return true;
  return false;
}

bool perSet::loadOne(int elevNumber,int floorNumber,direction dir,int &pdest)
{
  for (int i=0;i<MAXPERSONS;i++)
    if (pa[i].loadIfGoing(elevNumber,floorNumber,dir,pdest))  return true;
  return false;
}

int perSet::discharge(int elevNumber,int floorNumber)
{
  int n=0;
  for (int i=0;i<MAXPERSONS;i++)
    n+=pa[i].discharge(elevNumber,floorNumber);
  return n;
}

⌨️ 快捷键说明

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