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

📄 elevator.cpp

📁 这是一个模拟电梯的程序
💻 CPP
字号:
#include<iostream>

using std::cout;
using std::endl;

#include "elevator.h"
#include "person.h"
#include "floor.h"

const int Elevator::ELEVATOR_TRAVEL_TIME=5;
const int Elevator::UP=0;
const int Elevator::DOWN=1;

Elevator::Elevator(Floor &firstFloor,Floor &secondFloor)
  : elevatorButton(* this),
    currentBuildingClockTime(0),
	moving(false),
	direction(UP),
	currentFloor(Floor::FLOOR1),
	arrivalTime(0),
	floor1NeedsService(false),
	floor2NeedsService(false),
	floor1Ref(firstFloor),
	floor2Ref(secondFloor),
	passengerPtr(0)

{
	cout<<"elevator constructed"<<endl;
}

Elevator::~Elevator()
{
	delete passengerPtr;
	cout<<"elevator destructed"<<endl;
}

void Elevator:processTime(int time)
{
	currentBuildingClockTime=time;

	if(moving)
		processPossibleArrival();
	else
		processPossibleDeparture();
	if(!moving)
		cout<<"elevator at rest on floor"
		     <<currentFloor<<endl;
}

void Elevator::processPossibleArrival()
{
	if (currentBuildingClockTime==arrivalTime){
		currentFloor=
			(currentFloor==Floor::FLOOR1?Floor::FLOOR2:Floor::FLOOR1);

		direction=
			(currentFloor==Floor::FLOOR1?UP:DOWN);
		cout<<"elevator arrives on floor"
			<<currentFloor<<endl;

		arrivalFloor(currentFloor==Floor::FLOOR1?floor1Ref:floor2Ref);

		return;
	}

	cout<<"elevator moving"
		<<(direction==UP?"up":"down")<<endl;
}

void Elevator::processPossibleDeparture()
{
	bool currentFloorNeedsService=
		currentFloor==Floor::FLOOR1 ? 
          floor1NeedsService:floor2NeedsService;

    bool otherFloorNeedsService=
		currentFloor==Floor::FLOOR1 ?
         floor2NeedsService : floor1NeedsService;

	if(currentFloorNeedsService){
		arrivalAtFloor(currentFloor==Floor::FLOOR1 ?
           floor1Ref : floor2Ref);

		return;
	}


	if(otherFloorNeedsService)
		prepareToLeave(true);
          
}

void Elevator::arrivalAtFloor(Floor &arrivalFloor)
{
	moving=false;

	cout<<"elevator reset its button"<<endl;
	elevatorButton.resetButton();

	bell.ringBell();

	Person *floorPersonPtr = arrivalFloor.elevatorArrived();

	door.openDoor(
		passengerPtr,floorPersonPtr,arrivalFloor,*this);

	bool currentFloorNeedsService=
		currentFloor==Floor::FLOOR1 ?
        floor1NeedsService : floor2NeedsService;

	bool otherFloorNeedsService=
		currentFloor==Floor::FLOOR1 ?
        floor2NeedsService : floor1NeedsService;

	if (!currentFloorNeedsService)
		prepareToLeave(otherFloorNeedsService);
	else
		currentFloor==Floor::FLOOR1 ?
		floor1NeedsService=false : floor2NeedsService=false;

}

void Elevator::summonElevator(int floor)
{
	floor==Floor::FLOOR1 ?
		floor1NeedsService = true : floor2NeedsService = true;

}

void Elevator::passengerEnters(Person * const personPtr)
{
	passengerPtr=personPtr;
	cout<<"person"<<passengerPtr->getID()
		<<"enters elevator from floor"
		<<currentFloor<<endl;
}

void Elevator::passengerExits()
{
	passengerPtr=0;
}

void Elevator::prepareToLeave(bool leaving)
{
    Floor &thisFloor=
		currentFloor==Floor::FLOOR1 ?floor1Ref:floor2Ref;

	thisFloor.elevatorLeaving();

	door.closeDoor(thisFloor);

	if(leaving)
		move();

}

void Elevator::move()
{
	moving=true;

	arrivalTime=currentBuildingClockTime+ELEVATOR_TRAVEL_TIME;

	cout<<"elevator begins moving"
		<<(direction==DOWN ? "down" : "up")
		<<"to floor"
		<<(direction==DOWN ? '1' : '2')
		<<"(arrives at time"<<arrivalTime<<')'
		<<endl;
}

⌨️ 快捷键说明

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