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

📄 floor.cpp

📁 这个程序是对电梯的模拟
💻 CPP
字号:
// Floor.cpp: implementation of the Floor class.
//
//////////////////////////////////////////////////////////////////////

#include "Floor.h"



//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Floor::Floor(int floors,int num):bfloors(floors)
{
	 number=num;
	 upButton.SetCaption(1);       //上按钮
     downButton.SetCaption(-1);  //下按钮
}


void  Floor::SetFloorNumber(int num)
{
	number=num;
}

Floor::~Floor()
{
	list<People *>::iterator pit=NULL;
	People *p=NULL;
	while(pList[0].size() > 0){
		pit=pList[0].begin();
		p=*pit;
		pList[0].pop_front();
		if(p!=NULL)
		   delete p;
		}

}


int Floor::CreatePeople(Elevator * &leu)//随机产生人
{
     People *p;
	 int upcount=0;
	 Elevator *led=leu;
	 int fnum=rand() % RANDOM_FLOORS_MAX+1;    //随机产生
	 for(int i=0; i<fnum; i++){
		 p=new People();
		 cout<<SPACE2<<"a new man in floor "<<number;

		 //people press up and down button
		 if(number>0)
		    if(number < bfloors-1)  //random up and down
		       p->PressButton(0,1,-1);
			else
				p->PressButton(-1,1,-1);//this is the top floor
		 else
			 p->PressButton(1,1,-1);//this is the bottom floor

         //add to plist
         if(p->up){
			 upcount++;
			 pList[1].push_back(p);
			 cout<<"  &  wants to  up  <"<<char(24)<<">"<<endl; }
		 else{
			 pList[0].push_back(p);
			 cout<<"  &  wants to down <"<<char(25)<<">"<<endl;
		 }//end_if

		 //set status
		 // +_______________________________+
		 // | old status | -1     0     1   |
		 // |~~~~~~~~~~~~|~~~~~~~~~~~~~~~~~~|
		 // | p->up=ture |  0     0     1   | 
		 // |p->up=false | -1     0     0   |
		 // +-------------------------------|
		 // the number of the right bottom is the new status
		 
	 }//end_for

	 //set runline
	 if(upcount > 0)
		 if(!(leu->runline[number] & UP))
			 leu->runline[number]+=UP;
	 if(fnum-upcount > 0)
		 if(!(leu->runline[number] & DOWN))
			 led->runline[number]+=DOWN;
     
	/* 	// like this
	if(fnum-upcount > 0)
	    switch(leu->runline[number]){
	    case UP_DOWN     :
		case DOWN        :
		case OUT_DOWN    :
		case OUT_UP_DOWN : break;
		case UP     :
		case TT     :
		case OUT    :
		case OUT_UP : led->runline[number]+=DOWN; break;
		}//end_switch
     */
	 return fnum;
}//CreatePeople

void Floor::DeletePeople(People *people)
{
    delete people;
}

bool Floor::IsPeopleWaitUp()
{
	return pList[1].size()>0 ;

}

bool Floor::IsPeopleWaitDown()
{
	return pList[0].size()>0 ;

}


Time Floor::GetFistTime()
{
	Time tm;
	tm.CurTime();
	list<People *>::iterator pit1,pit0;
	if(pList[0].size() < 1)
		if(pList[1].size() < 1)  //down=0;up=0
			return tm;
		else{                      //down=0;up>0
			pit1=pList[1].begin();
			return (*pit1)->tm;
		}
	else
		if(pList[1].size() < 1){  //down>0;up=0
            pit0=pList[0].begin();
			return (*pit0)->tm;
		}
		else{                      //down>0;up>0
            pit0=pList[0].begin();
		    pit1=pList[1].begin();
	    	return (*pit1)->tm < (*pit0)->tm ? (*pit1)->tm : (*pit0)->tm ;
		}
}

⌨️ 快捷键说明

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