Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <stdlib.h>
- //r means roof floor
- int curFloor = 1;
- std::string dir;
- int calls = 0;
- int carrying = 0;
- std::string callList [10];
- std::string newCallInput;
- int destList[10];
- void printElevator(int floor, bool moving, bool goingUp)
- {
- std::string firstRow = " ";
- if(moving && goingUp)
- {
- firstRow += "^ ";
- }
- else
- {
- firstRow += " ";
- }
- firstRow += "1 2 3 4 5";
- std::cout << firstRow << std::endl;
- std::string secondRow = " ";
- for(int i = 0; i < 5; i++)
- {
- if(floor == i + 1)
- {
- secondRow += "^ ";
- }
- else if(floor == i + 6)
- {
- secondRow += "v ";
- }
- else
- {
- secondRow += " ";
- }
- }
- std::cout << secondRow << std::endl;
- std::string thirdRow = " ";
- if(moving && !goingUp)
- {
- thirdRow += "v ";
- }
- else
- {
- thirdRow += " ";
- }
- thirdRow += "6 7 8 9 R";
- std::cout << thirdRow << std::endl;
- std::cout << " .---------------." << std::endl;
- for(int j = 0; j < 15; j++)
- {
- std::cout << " || | ||" << std::endl;
- }
- std::cout << " '---------------'" << std::endl;
- }
- std::string inputToArr(int callFloor, bool dirUp)
- {
- std::string out;
- if(curFloor == 10)
- {
- out += "R";
- }
- else
- {
- out += std::to_string(callFloor);
- }
- out += "";
- if(dirUp)
- {
- out += "U";
- }
- else
- {
- out += "D";
- }
- return out;
- }
- std::string lowerCaser(std::string input)
- {
- std::string output;
- char c;
- for(unsigned i = 0; i < input.length(); i++)
- {
- c = input[i];
- output += (char) tolower(c);
- }
- return output;
- }
- int newInput()
- {
- std::cout << "Would you like to add a new call for the elevator? (Y/N)" << std::endl;
- std::cin >> newCallInput;
- newCallInput = lowerCaser(newCallInput);
- while(newCallInput != "n" && newCallInput != "y")
- {
- std::cout << "SPEAK CLEARLY." << std::endl;
- std::cout << "Would you like to add a new call for the elevator? (Y/N)" << std::endl;
- std::cin >> newCallInput;
- newCallInput = lowerCaser(newCallInput);
- }
- if(newCallInput == "n" && calls == 0 && carrying == 0 && curFloor == 1)
- {
- std::cout << "Alright. Guess we're done here then.";
- return 0;
- }
- else if(newCallInput == "y")
- {
- std::string callFloorIn;
- std::string dirIn;
- int floor;
- bool dirUp;
- std::cout << "Input the floor to call from, from 1-10." << std::endl;
- std::cin >> callFloorIn;
- while (callFloorIn != "1" && callFloorIn != "2" && callFloorIn != "3" && callFloorIn != "4" &&
- callFloorIn != "5" && callFloorIn != "6" && callFloorIn != "7" && callFloorIn != "8" &&
- callFloorIn != "9" && callFloorIn != "10") {
- std::cout << "SPEAK CLEARLY." << std::endl;
- std::cout << "Input the floor to call from, from 1-10." << std::endl;
- std::cin >> callFloorIn;
- }
- floor = std::stoi(callFloorIn);
- std::cout << "Are you going up (U) or down (D)?" << std::endl;
- std::cin >> dirIn;
- dirIn = lowerCaser(dirIn);
- while(dirIn != "u" && dirIn != "d")
- {
- std::cout << "You stubbed your finger on the input panel. Try again." << std::endl;
- std::cout << "Are you going up (U) or down (D)?" << std::endl;
- std::cin >> dirIn;
- dirIn = lowerCaser(dirIn);
- }
- dirUp = dirIn == "u";
- callList[calls] = inputToArr(floor, dirUp);
- calls++;
- return 1;
- }
- else
- {
- return 1;
- }
- }
- void pickUp()
- {
- bool realPickUp = false;
- std::string firstCall = callList[0];
- if(calls == 1 && firstCall[1] == dir[0] && ((firstCall[0] == 'R' && curFloor == 10) || (firstCall[0] - 48 == curFloor)))
- {
- realPickUp = true;
- }
- else
- {
- for(int i = 0; i < 10; i++)
- {
- std::string cur = callList[i];
- if(cur[0] == 'R' && curFloor == 10)
- {
- realPickUp = true;
- }
- if(cur[0] - 48 == curFloor && cur[1] == dir[0])
- {
- realPickUp = true;
- }
- }
- }
- if(realPickUp)
- {
- std::string numInput;
- int num;
- std::cout << "How many people are getting on at this floor?" << std::endl;
- std::cin >> numInput;
- while(numInput != "1" && numInput != "2" && numInput != "3" && numInput != "4" && numInput != "5" && numInput != "6" && numInput != "7" && numInput != "8" && numInput != "9" && numInput != "10")
- {
- std::cout << "You appear to have found Schrodinger's elevator passenger. Please try again." << std::endl;
- std::cout << "How many people are getting on at this floor?" << std::endl;
- std::cin >> numInput;
- }
- num = std::stoi(numInput);
- if(carrying + num > 10)
- {
- num = 10 - carrying;
- std::cout << "Unfortunately the elevator currently has too many passengers. Only the first " + std::to_string(num) + " passengers can get on." << std::endl;
- }
- for(int i = 0; i < num; i++)
- {
- std::string destFloorIn;
- int destFloor;
- std::string output = "What floor is the " + std::to_string(i + 1);
- if(i + 1 == 1)
- {
- output += "st";
- }
- else if(i + 1 == 2)
- {
- output += "nd";
- }
- else if(i + 1 == 3)
- {
- output += "rd";
- }
- else
- {
- output += "th";
- }
- output += " passenger going to? (Numbers 1-9, or R for the roof floor)";
- std::cout << output << std::endl;
- std::cin >> destFloorIn;
- while(destFloorIn != "R" && destFloorIn != "r" && destFloorIn != "1" && destFloorIn != "2" && destFloorIn != "3" && destFloorIn != "4" && destFloorIn != "5" && destFloorIn != "6" && destFloorIn != "7" && destFloorIn != "8" && destFloorIn != "9")
- {
- std::cout << "I'm sorry, but you haven't exactly floored me with your response. Please try again." << std::endl;
- std::cout << output << std::endl;
- std::cin >> destFloorIn;
- }
- if(destFloorIn == "R" || destFloorIn == "r")
- {
- destFloor = 10;
- }
- else
- {
- destFloor = std::stoi(destFloorIn);
- }
- destList[carrying + i] = destFloor;
- carrying++;
- }
- calls--;
- }
- }
- void dropOff()
- {
- for(int i = 0; i < 10; i++)
- {
- if(destList[i] == curFloor)
- {
- destList[i] = 0;
- carrying--;
- }
- }
- for(int i = 0; i < 10; i++)
- {
- if(destList[i] == 0)
- {
- for(int j = i + 1; j < 10; j++)
- {
- if(destList[j] != 0)
- {
- destList[i] = destList[j];
- destList[j] = 0;
- }
- }
- }
- }
- }
- void moveFloor(bool goingUp)
- {
- if(goingUp)
- {
- dir = "U";
- curFloor++;
- if(curFloor == 10)
- {
- dir = "N";
- }
- }
- else
- {
- dir = "D";
- curFloor--;
- if(curFloor == 1)
- {
- dir = "N";
- }
- }
- printElevator(curFloor, dir != "N", dir == "U");
- dropOff();
- pickUp();
- }
- void bringDown()
- {
- for(int i = 0; i < 10; i++)
- {
- if(callList[i].empty())
- {
- for(int j = i + 1; j < 10; j++)
- {
- if(!callList[j].empty())
- {
- callList[i] = callList[j];
- callList[j] = "";
- }
- }
- }
- if(destList[i] == 0)
- {
- for(int j = i + 1; j < 10; j++)
- {
- if(destList[j] != 0)
- {
- destList[i] = destList[j];
- destList[j] = 0;
- }
- }
- }
- }
- }
- int main()
- {
- dir = "N";
- printElevator(1, false, false);
- while(true){
- int rtn = newInput();
- if(rtn == 0)
- {
- std::cout << "Have a nice day!" << std::endl;
- return 0;
- }
- bringDown();
- std::cout << "Carrying: " + std::to_string(carrying) + " Calls: " + std::to_string(calls) << std::endl;
- //elevator procedure
- if(carrying == 0)
- {
- if(calls == 1)
- {
- //go to it
- std::string call = callList[0];
- int callFloor;
- if(call[0] == 'R')
- {
- callFloor = 10;
- }
- else
- {
- callFloor = call[0] - 48;
- }
- if(callFloor > curFloor)
- {
- moveFloor(true);
- }
- else if(callFloor < curFloor)
- {
- moveFloor(false);
- }
- else
- {
- pickUp();
- }
- }
- else if(calls > 0)
- {
- //go to the nearest call in our direction
- int target;
- int targetDist = 10;
- for(int i = 0; i < 10; i++)
- {
- std::string cur = callList[i];
- if(!cur.empty())
- {
- if(cur[0] - 48 > curFloor && cur[1] == 'U' && (cur[0] - 48) - curFloor < targetDist)
- {
- target = cur[0] - 48;
- }
- if(cur[0] - 48 < curFloor && cur[1] == 'D' && curFloor - (cur[0] - 48) < targetDist)
- {
- target = cur[0] - 48;
- }
- }
- }
- if(target == curFloor)
- {
- pickUp();
- }
- else
- {
- moveFloor(target > curFloor);
- }
- }
- else
- {
- if(curFloor > 1)
- {
- moveFloor(false);
- }
- else
- {
- dir = "N";
- }
- }
- }
- else
- {
- //figure out direction
- int target;
- int closeDist = 10;
- bool inSameDir = false;
- for(int i = 0; i < 10; i++)
- {
- int curDest = destList[i];
- if(curDest > curFloor && dir == "U" && curDest - curFloor < closeDist)
- {
- inSameDir = true;
- target = curDest;
- closeDist = curDest - curFloor;
- }
- if(curDest < curFloor && dir == "D" && curFloor - curDest < closeDist)
- {
- inSameDir = true;
- target = curDest;
- closeDist = curFloor - curDest;
- }
- }
- if(!inSameDir)
- {
- for(int i = 0; i < 10; i++)
- {
- int curDest = destList[i];
- if(abs(curDest - curFloor) < closeDist)
- {
- target = curDest;
- }
- }
- }
- if(target < curFloor)
- {
- dir = "D";
- }
- else if(target > curFloor)
- {
- dir = "U";
- }
- //transport them
- moveFloor(dir == "U");
- //pick people up if they're on the way and going the same direction
- for(int i = 0; i < 10; i++)
- {
- std::string cur = callList[i];
- int callFloor;
- if(cur[0] == 'R')
- {
- callFloor = 10;
- }
- else
- {
- callFloor = cur[0] - 48;
- }
- if(callFloor == curFloor)
- {
- if(cur[1] == dir[0])
- {
- pickUp();
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment