This is a small town and it has five restaurants, identified by single letters: A, B, C,
Question:
This is a small town and it has five restaurants, identified by single letters: A, B, C, D, and E.
The input text file is a series of proposed pickup and drop-off sequences which should be independently verified. Each sequence starts with an empty stack and is formatted in the file as follows:
- At the beginning of a new sequence, the number of lines in that sequence is given (a single integer by itself, this will be at least 1 but not more than 100).
- A pickup - labeled with a P followed by a letter (A, B, C, D or E)
- A drop-off - labeled with a D followed by a letter
- A 'clear stack' - labeled with a 'C' by itself on a line. This causes all of the deliveries in the car to be made at once, leaving an empty stack.
For each drop-off operation, the letter in the file is the "expected" restaurant item that is at the top of the stack. If it differs from the actual top of the stack at that point in time, the series is invalid.
Example:
This input file has 5 separate series which should be verified independently of each other.
output:
Your program should output, for each series of pickups and drop-offs, whether this is a valid series, which is true if:
- The series is a valid series of operations, given the constraints, and
- All of the food is delivered (the car is empty) at the end of the series
If both of these are true, the series is valid, otherwise it is not valid.
You program should output only 'valid' or 'not valid', one line per test case. Be sure to turn off any extra cout or debugging statements. Code that has extra output (printing the file to the screen, etc.) will have points deducted.
A 'clear stack' operation on an empty stack is acceptable (the series can still be valid).
For the input file above (which has 5 separate series) here is the correct output. Use this file to help verify your code.
Please using this code.....
#include
#include
#include
#include
using namespace std;
#define FILENAME "test.txt"
int main() {
ifstream f(FILENAME);
stack s;
string tmp;
bool failed = false;
char item;
if (if.good()){
cout << "Invalid file." << endl;
exit(-1);
}
while (true) {
getline(f,tmp);
if (f.eof())
return (0);
}
}
Operating Systems Internals and Design Principles
ISBN: 978-0133805918
8th edition
Authors: William Stallings