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

📄 building.cpp

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

#include "Building.h"
#include <conio.h> 

#define OPENDOOR if(!stop){ ep->BellRing();	ep->OpenDoor();stop=true; }

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

Building::Building()
{
    InitBuilding();
}

Building::~Building()
{
	delete ep;
	for(int i=0; i<bfloors; i++)
	{
		delete fVector[i];
	}
}

Building::Building(int fcount)
{
    InitBuilding(fcount);
}

void Building::InitBuilding(int fcount)
{
    cout<<"Init Building..."<<endl;
	//init buiding's floor
    bfloors=fcount;
    while(bfloors<=0){
		cout<<"\nPlese input the number of the building's floor:";
		cin>>bfloors;
	}

	Floor *f;
	cout<<"Begin init the floors..."<<endl;
	for(int i=0; i<bfloors; i++)
	{
		f=new Floor(bfloors,i);
		fVector.push_back(f);
		cout<<"  "<<i<<" finish!..."<<endl;
	}
	f=NULL;

    //init one elevator
	ep=new Elevator(bfloors,1);

	cout<<"Init building finished! "<<endl;
}

void Building::SysRun()//Run Run Run Run Run Run Run Run 
{
	char key;
	do{
		cout<<"\nchoose->";
		key=getch();
		switch(key){
		case ENTER://Create people
			       cout<<"Create people..."<<endl;
			       SysCreatePeople();
				   break;
		case SPACE://The elevator run to next floor
			       cout<<"Elevator run to next..."<<endl;
			       SysElevatorRunNext();
				   break;
		case 'H':
		case 'h':  SysChooseHelp();break;
		case ESC:  //stop
			       cout<<"stop!!!"<<endl;
			       SysStop();
				   break;
		}
	}while(key!=ESC);

}

void Building::SysCreatePeople()
{
	int rfnum=rand() % RANDOM_PEOPLE_MAX+1;//rfnum is the number how many the floors will create people.
	for(int i=0; i<rfnum; i++)
	{
	   int r=rand() % bfloors;	
       fVector[r]->CreatePeople(ep);
	   
	}

}

void Building::SysElevatorRunNext()
{
	int  floorAt=ep->GetFloorAt()+ep->GetStatus();
	Floor* &thisfloor=fVector[floorAt];

	ep->floorAt=floorAt;//!!!!!!!!!!!!!!!!!

	//the infomation
    cout<<" #Elevator No."<<ep->number<<" at floor —("<<ep->floorAt<<")—"<<endl;
    //new
	/********************************************/
	bool stop=false;
	int c=0;
	int r=ep->runline[floorAt];//stor
	if(r>0)
	{
		switch(ep->status){
		case -1: //down
			if(r & OUT){
                OPENDOOR
			    ep->PeopleOut();}
			if(r & DOWN){
                OPENDOOR
				ep->PeopleEnter(thisfloor->pList[0],0); }//down
			break;
		case  0: //stop
			if(r==UP){
				OPENDOOR
			    ep->PeopleEnter(thisfloor->pList[1],1); }//up
			if(r==DOWN){
                OPENDOOR
				ep->PeopleEnter(thisfloor->pList[0],0); }//down
			if(r==UP_DOWN){
				OPENDOOR
				if(fVector[floorAt]->pList[0].size() < fVector[floorAt]->pList[1].size())
				    ep->PeopleEnter(thisfloor->pList[1],1); //up
			     else
					 ep->PeopleEnter(thisfloor->pList[0],0); //down
			}
			break;
		case  1: //up
			if(r & OUT) {
				OPENDOOR
				ep->PeopleOut(); }
			if(r & UP){ 
				OPENDOOR
				ep->PeopleEnter(thisfloor->pList[1],1); }//up
			break;
		}//END_switch
	}//end_if

	//deel the status of 0
	if(ep->status==0)
	{
		if(r==TT){
			Time tm,tt;
		    int tem=-1;
		    tm.CurTime();
		    for(int j=0;j<bfloors;j++){
               tt=fVector[j]->GetFistTime();//get the 
			   if((ep->runline[j] > 0) && (tm > tt)){
			    	tm=tt;
                    tem=j;  }
			}//end_for
			if(tem!=-1){
			    if(tem < floorAt)
			    	ep->status=-1;
		    	if(tem > floorAt)
			    	ep->status=1;
			}//if
		}
	    else{
		    switch(ep->runline[floorAt]){
        	case UP : 
					OPENDOOR
				ep->PeopleEnter(thisfloor->pList[1],1);
				break;
			case DOWN :
				OPENDOOR
				ep->PeopleEnter(thisfloor->pList[0],0);
				break;
			case UP_DOWN :
				OPENDOOR
				if(fVector[floorAt]->pList[0].size() < fVector[floorAt]->pList[0].size())
					ep->PeopleEnter(thisfloor->pList[1],1); //up
				else
					ep->PeopleEnter(thisfloor->pList[0],0); //down
				break;
			}//end_switch
		}//end_if_else
	}//end_if


	if(stop)
		ep->CloseDoor();//Close Door

	if(floorAt>=(bfloors-1) && ep->status==1)
	    	ep->status=0;
	    else if(floorAt<=0 && ep->status==-1)
		    ep->status=0;

}//end_SysElevatorRunNext


void Building::SysStop()
{

}

void Building::SysChooseHelp()
{
	cout<<"help..."<<endl
		<<"   +—————————————————————————————————+"<<endl
	    <<"   |  choose key:                                                     |"<<endl
		<<"   |       ENTER---------------Create people                          |"<<endl
		<<"   |       SPACE---------------The elevator runs to next floor        |"<<endl
		<<"   |       H/h-----------------Help                                   |"<<endl
		<<"   |       ESC-----------------Exit the Simulation                    |"<<endl
		<<"   +—————————————————————————————————+"<<endl
		;
}

⌨️ 快捷键说明

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