本文整理汇总了C++中Floor::isPreferredDirectionUp方法的典型用法代码示例。如果您正苦于以下问题:C++ Floor::isPreferredDirectionUp方法的具体用法?C++ Floor::isPreferredDirectionUp怎么用?C++ Floor::isPreferredDirectionUp使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Floor
的用法示例。
在下文中一共展示了Floor::isPreferredDirectionUp方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: if
Building& Building::step(int nRidersToAdd)
{
//******************************************************************************
// the rider phase, you should see stranded riders randomly placed on floors, *
// and there should be no up-riders on the top floor and no down-riders on the *
// bottom floor, and elevators should remain idle. *
//******************************************************************************
for (int i = 0; i < nRidersToAdd; i++)
{
int max = floors.size(); // max number of floors
int from = 0;
int to = 0;
getDifferentInts(max, from, to); // create a rider with randomly selected from- and to-floors
(*(floors[from])).addNewRider(Rider(*(floors[to]))); // tell the from-floor to add this rider
}
// increment timer once per second
this->simTime++;
//***************************************************************************************
// the elevator phase, the elevators should (a) stop to pickup riders on the floors on *
// their way to another destination, and (b) go to floors with waiting riders after *
// they reach an idle state. *
//***************************************************************************************
// ELEVATOR ACTIONS [3]
for (int i = 0; i < getElevatorCount(); i++)
{
if (!elevators[i]->isDoorOpen()) // if elevator door is closed (move up or down) [3]
{
// if not near enough to destination to reach it in this time step, continue moving [3]
if (!elevators[i]->isNearDestination())
{
if (elevators[i]->isDirectionUp()) // if elevator direction is up, move up [3]
{
elevators[i]->moveUp();
}
else // otherwise, move down [3]
{
elevators[i]->moveDown();
}
}
else // otherwise it's near enough to destination to reach it in this time step... [4]
{
elevators[i]->moveToDestinationFloor(); // tell elevator to move to its destination floor [4]
elevators[i]->openDoor(); // tell elevator to open its door [4]
elevators[i]->removeRidersForDestinationFloor(); // tell elevator to remove riders for its destination floor
// get a non-const pointer to the elevator's destination floor (using const_cast) [5]
Floor* elvDestinationFl = const_cast<Floor*>(&elevators[i]->getDestination());
// if elevator is empty, choose a direction based on longest-waiting rider (the one with the smallest ID) on the floor: [5]
if (elevators[i]->getRiderCount() == 0)
{
if (elvDestinationFl->isPreferredDirectionUp()) // if the floor's chosen direction is up [5]
{
elevators[i]->setDirectionUp(); // tell elevator to set its direction to up [5]
}
else // otherwise [5]
{
elevators[i]->setDirectionDown(); // tell elevator to set its direction to down [5]
}
}
int n = elevators[i]->getAvailableSpace();
// if there is space in the elevator after letting off riders, board new ones [6]
if (n > 0)
{
if (elevators[i]->isDirectionUp()) // if elevator direction is up, board up-riders (if any)... [6]
{
elevators[i]->addRiders(elvDestinationFl->removeUpRiders(n));
}
else
{
elevators[i]->addRiders(elvDestinationFl->removeDownRiders(n)); // otherwise, board down-riders (if any) [6]
}
}
// reassess elevator destination based on its riders [8]
elevators[i]->setDestinationBasedOnRiders();
}
} // End if door is closed
else // otherwise if door is open (then it already let off riders, or is in its initial state) [7]
{
if (elevators[i]->getRiderCount() > 0) // if elevator has any riders [7]
{
elevators[i]->closeDoor(); // tell elevator to close its door [7]
}
else // otherwise [9]
{
elevators[i]->setIdle(); // tell elevator to go idle [9]
}
}
} // End elevator loop
//*********************************************************************************************************
//.........这里部分代码省略.........
示例2: if
Building& Building::step(int numRiders) // moves elevators and riders each second. numRiders = riders to randomly add to floors
{
int count = 0; // screen counter for pausing.
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@@@@@@@@@@@@@@@@@@@@@ Riders @@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
for (int i=0; i < numRiders; i++) // add riders on random floors
{
int randomStartFloor = rand() % buildingFloors.size(); // random number from 0 to buildingFloors max
int randomDestinationFloor = rand() % buildingFloors.size(); // random number from 0 to buildingFloors max
if (randomStartFloor == 0) // Basement case - no down
{
randomDestinationFloor = rand() % (buildingFloors.size()-1) + 1; // random number from 1 to (buildingFloors max -1)
}
else if (randomStartFloor == buildingFloors.size()) // Top case - no up
{
randomDestinationFloor = rand() % (buildingFloors.size()-1); // random number from 0 to (buildingFloors max -1)
}
else
{
while (randomDestinationFloor == randomStartFloor) // make sure starting != destination floor
{
randomDestinationFloor = rand() % buildingFloors.size();
}
}
buildingFloors[randomStartFloor]->addNewRider(Rider(*buildingFloors[randomDestinationFloor])); // add rider on rand floor w/ rand dest.
} // end for (adding riders to random floors)
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@@@@@@@@@@@@@@@@@@@@@ Elevator @@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
for (int i = 0; i<buildingElevators.size(); i++) // checking all elevators one by one [3]
{
if (buildingElevators[i]->isDoorOpen() == false) // if doors are closed [3]
{
if (!buildingElevators[i]->isNearDestination()) // executed if elevator is not near destination [3]
{
if (buildingElevators[i]->isDirectionUp()) buildingElevators[i]->moveUp(); // move up [3]
else if (buildingElevators[i]->isDirectionDown()) buildingElevators[i]->moveDown(); // move down [3]
} // end 'if not near destination' loop
else if (buildingElevators[i]->isNearDestination()) // if elevator is near to destination [4]
{
buildingElevators[i]->moveToDestinationFloor(); // [4]
buildingElevators[i]->openDoor(); // [4]
{
buildingElevators[i]->removeRidersForDestinationFloor();// remove riders; ignore returned vector of removed riders [4]
} // "{}" & cout are temp
Floor* elevatorsCurrentFloor = const_cast<Floor*>(&(buildingElevators[i]->getDestination())); // [5]
if (buildingElevators[i]->hasRiders() == false) // if elevator is empty [5]
{
if (elevatorsCurrentFloor->isPreferredDirectionUp() == true) // if floor direction = up [5]
{
buildingElevators[i]->setDirectionUp();
}
else // if floor direction != up [5]
{
buildingElevators[i]->setDirectionDown();
}
} // end if elevator = empty
if (buildingElevators[i]->getAvailableSpace()!=0) // if space in elevator, board riders [6]
{
if (buildingElevators[i]->isDirectionUp() == true) // if the elevator is going up
{
if (elevatorsCurrentFloor->hasUpRiders() == true) // if there are up riders on the floor
{
buildingElevators[i]->addRiders(elevatorsCurrentFloor->removeUpRiders(buildingElevators[i]->getAvailableSpace())); // board up-riders [6]
} // end if (has up riders)
} // end if elevator direction == up
else // for down elevators
{
if (elevatorsCurrentFloor->hasDownRiders() == true) // if there are down riders on the floor
{
buildingElevators[i]->addRiders(elevatorsCurrentFloor->removeDownRiders(buildingElevators[i]->getAvailableSpace()));// board down-riders [6]
} // end if (has down riders)
} // end else (for down riders)
} // end if space in elevator
buildingElevators[i]->setDestinationBasedOnRiders(); //reassess elevator's destination based on riders [8]
} // end if 'near destination' loop
} // end 'if elevator doors are closed' loop
else // "isDoorOpen() == true" (it already let off riders, or is in its initial state) [7]
{
if (buildingElevators[i]->hasRiders() == true) // has riders [7]
{
buildingElevators[i]->closeDoor(); // close door [7]
} // end if has riders
else // if no riders
{
//.........这里部分代码省略.........
示例3: if
Building& Building::step(int max)
{
for(int i = 0; i < max; i++)
{
int topFloor = getFloorCount();
int firstFloor; // starting floor int
int lastFloor; // ending floor int
getDifferentInts(topFloor, firstFloor, lastFloor); // call getDifferentInts creates 2 random ints that are not equal to one another
floors[firstFloor]->addNewRider(Rider(*floors[lastFloor])); // rider loop
}
// ELEVATOR ACTIONS [3]
for(int i = 0; i < getElevatorCount(); i++){// tell each elevator to perform an action [3]
if(getElevator(i).isDoorOpen() == false){ // if elevator door is closed
if(getElevator(i).isNearDestination() == false){ // if not near enough to destination to reach it in this time step, continue moving [3]
if(getElevator(i).isDirectionUp() == true){ // if elevator direction is up, move up [3]
elevators[i]->moveUp();
}
else{ // otherwise, move down [3]
elevators[i]->moveDown();
}
}
else{ // otherwise it's near enough to destination to reach it in this time step... [4]
elevators[i]->moveToDestinationFloor(); // tell elevator to move to its destination floor [4]
elevators[i]->openDoor(); // tell elevator to open its door [4]
elevators[i]->removeRidersForDestinationFloor(); // tell elevator to remove riders for its destination floor -- ignore returned vector of removed riders [4]
// get a non-const pointer to the elevator's destination floor (using const_cast) [5]
Floor* ncp;
const_cast<const Floor*&>(ncp) = &getElevator(i).getDestination();
if(getElevator(i).getRiderCount() == 0){ // if elevator is empty
//choose a direction based on longest-waiting rider (the one with the smallest ID) on the floor: [5]
if(ncp->isPreferredDirectionUp() == true) // if the floor's chosen direction is up [5]
elevators[i]->setDirectionUp(); // tell elevator to set its direction to up [5]
else // otherwise [5]
elevators[i]->setDirectionDown(); // tell elevator to set its direction to down [5]
}
if(getElevator(i).getAvailableSpace() > 0 && elevators[i]->isDoorOpen() == true){ // if there is space in the elevator after letting off riders
// board new ones [6]
if(getElevator(i).isDirectionUp() == true){ // if elevator direction is up, board up-riders (if any)... [6]
elevators[i]->addRiders(ncp->removeUpRiders(elevators[i]->getAvailableSpace()));
}
else{ // otherwise, board down-riders (if any) [6]
elevators[i]->addRiders(ncp->removeDownRiders(elevators[i]->getAvailableSpace()));
}
}
elevators[i]->setDestinationBasedOnRiders(); // reassess elevator destination based on its riders [8]
}
}
else{ // otherwise (then it already let off riders, or is in its initial state) [7]
if(getElevator(i).hasRiders() == true){ // if elevator has any riders
elevators[i]->closeDoor(); // tell elevator to close its door [7]
}
else{
elevators[i]->setIdle();
}
}
}
// FLOOR ACTIONS [2]
// check each floor (for waiting riders) [2]
for(int i = 0; i < getFloorCount();i++){
if(getFloor(i).hasRidersWaiting() == false){ // if there are no riders waiting on this floor, continue with next floor [2]
continue;
}
for(int j = 0; j < getElevatorCount(); j++){
int currentLoc = elevators[j]->getLocation() - floors[i]->getLocation(); // get elevator's relative location
if(getElevator(j).isIdle() == true){ // look at each elevator to see if it needs to be sent here [2]
if(currentLoc >= 0){ // elevator is above if positive
elevators[j]->setDirectionDown(); // set elevator direction to down [2]
elevators[j]->setDestination(floors[i]);// set elevator's destination to this floor [2]
elevators[j]->closeDoor(); // tell elevator to close its door [2]
}
else{
elevators[j]->setDirectionUp();// set elevator direction to up [2]
elevators[j]->setDestination(floors[i]); // set elevator's destination to this floor [2]
elevators[j]->closeDoor(); // tell elevator to close its door [2]
}
}
// else if there are riders on this floor waiting to go up, and the elevator is going up... [10]
else if(getFloor(i).hasUpRiders() == true && getElevator(j).isDirectionUp() == true){
// get distance from elevator's destination floor to this floor (positive if elevator destination is above this floor) [10]
double DistancetoFloor = getElevator(j).getDestination().getLocation() - getFloor(i).getLocation();
if (DistancetoFloor > 0 && currentLoc < 0){ // if elevator is below floor and elevator destination is above this floor [10]
elevators[j]->setDestination(&(getFloor(i))); // set elevator's destination to this floor [10]
}
}
//.........这里部分代码省略.........