Question
Need help with c++ program involving stack, queue and templates, reading in file. You are being placed in charge of running an elevator in an
Need help with c++ program involving stack, queue and templates, reading in file.
You are being placed in charge of running an elevator in an office building. People enter your building on the ground floor to wait in line for the elevator.
Ground floor Rules:
- The first person in line will be the first to get on the elevator.
- There is no cap on how many people can be waiting for the elevator
Elevator Rules:
- The first person to get into the elevator will be last one off
You will read in from file what is happening to the elevator. You can assume the file will be well formatted, but you may not assume that the commands will be given in a logical order or have safe values. For example, the command "DROP_OFF 10" could be in the file when no one is in the elevator. When problems like this arise, you must handle the thrown exception! Do not simply disallow such actions to occur; you need to get practice catching and handling exceptions.
Commands from file:
Note, there is no exit command. You'll have to read until you reach the end of file.
Here's one way of reading until you reach the end of file:
while (inFileObject >> someVar) { //you just successfully read something into someVar //so you can proceed or perhaps even continue reading into other variables }
Example File
WAIT Bart WAIT Homer WAIT Marge WAIT Lisa WAIT Maggie WAIT Fred INSPECTION WAIT Wilma WAIT Betty WAIT Barney WAIT George WAIT Jane PICK_UP 7 INSPECTION DROP_OFF 3 PICK_UP 4 INSPECTION
File overview:
- Six people get in line for the elevator
- Inspection occurs. The following is printed:
Elevator status: The elevator is empty. No one is in the elevator. Bart will be the next person to get on the elevator.
- Five more people get in line for the elevator
- The elevator is filled
- Inspection occurs. The following is printed:
Elevator status: The elevator is not empty. Wilma will be the next person to leave the elevator. Betty will be the next person to get on the elevator.
- Three are dropped off
- The elevator filled
- Inspection occurs. The following is printed:
Elevator status: The elevator is not empty. Jane will be the next person to leave the elevator. No one is in line for the elevator
Notable departures from reality:
- Once people have been dropped off by the elevator, they cannot get back on the elevator (in other words you don't have keep track of all people in the building)
- We don't care what floor the elevator is going to
Updates to your Stack and Node Classes
- Node, Stack, and the new Queue class will all need to be templated
- Please see the Makefile tutorial on tips on what to update
- Your Stack and Queue class will need to have the option of throwing exceptions
Stack Method Updates
Remember to make your Stack class templated. Here, I'm refering to the type variable as T.
- void pop();
- assumes the stack is not empty
- top of the stack is removed
- throws std::runtime_error when a pop is attempted on an empty stack. Does not return a value in this case.
- T peek() const;
- assumes the stack is not empty
- returns the value at the top of the stack
- throws a std::runtime_error is attempted on an empty stack. Does not return a value in this case.
Queue class
- Put the precondition, postcondition, return, and throws comments in this class also
- Queue will have standard destructors, copy constructors, and assignment operator overload
- bool isEmpty() const;
- returns true if the queue is empty, false otherwise
- void enqueue(const T value);
- Entry added to back of queue
- void dequeue();
- assumes the queue is not empty
- the front element is removed
- throws std::runtime_error if attempted on an empty queue. Does not return a value in this case.
- T peekFront() const;
- assumes the queue is not empty
- returns the value at the front of the queue
- throws a std::runtime_error if attempted on an empty queue. Does not return a value in this case.
Messages when exceptions are thrown
Any method that throws an exception needs to provide a meaningful message to the point in code doing the exception handling. For example:
- "Pop attempted on an empty stack"
- "Peek attempted on an empty stack"
Handling exceptions
Here is some sample code to handle an exception that is thrown.
try { something that could throw a std::runtime_error } catch(std::runtime_error& rte) { std::cout Command Description WAITSomeone gets in line for the elevator . The name will be a single word PICK_UP How many people get on the elevator The elevator goes up into the building DROP_OFF . The given number of people get off the elevator Then the elevator returns to the ground floor . The status of the following is printed to the screen Is the elevator empty? Who will be the next to get off the elevator? Who will be the next to get on the elevator? DO NOT alter either data structure INSPECTION Elevator status: The elevator is not empty. Mark will be the next person to leave the elevator. Stacy will be the next person to get on the elevator. Command Description WAIT Someone gets in line for the elevator . The name will be a single word PICK_UP How many people get on the elevator The elevator goes up into the building DROP_OFF . The given number of people get off the elevator Then the elevator returns to the ground floor . The status of the following is printed to the screen Is the elevator empty? Who will be the next to get off the elevator? Who will be the next to get on the elevator? DO NOT alter either data structure INSPECTION Elevator status: The elevator is not empty. Mark will be the next person to leave the elevator. Stacy will be the next person to get on the elevator
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started