Question: this code you guys gave me doesn't work: #include Imagine there is an unknown data structure that holds a collection of strings. It has two
this code you guys gave me doesn't work: #include Imagine there is an "unknown" data structure that holds a collection of strings. It has two operations defined as follows:
How this data structure works is mysterious. Given a sequence of operations, it may operate like a LIFO stack, or it may not.
Your program must determine whether or not it is operating as a stack given a series of operations and the corresponding returned strings.
Program Input and Output
Input File Format
length of The input is terminated by reaching endoffile EOF
program. You can also use the Stack.h template in this module.
Program Output
For each test case, output one of these lines:
stack
not stack
which will indicate whether or not the series of operations are behaving like a stack in LIFO order
Sample Input
Create a text file with the following lines and use it as input into your program.
i cat
i dog
r dog
rmathrm cat
sunshine
i apple
i street
r apple
r street
rmathrm sunshine
red
red
i blue
i green
r green Sample Output
When your program reads the above input file, it should output these four lines only:
stack
not stack
stack
not stack
Explanation of the Sample Output
output:
st
most recent string inserted At that point we know that this is not a stack, so program outputs:
no
At this point your code can skip the remaining lines in that test case and proceed to the next one in the file.
The fourth case inserts two strings, then removes one in the correct order green comes out first The test case ends and the stack is not empty, so the program outputs:
hot stack Imagine there is an "unknown" data structure that holds a collection of strings. It has two operations defined as follows:
How this data structure works is mysterious. Given a sequence of operations, it may operate like a LIFO stack, or it may not.
Your program must determine whether or not it is operating as a stack given a series of operations and the corresponding returned strings.
Program Input and Output
Input File Format
length of The input is terminated by reaching endoffile EOF
program. You can also use the Stack.h template in this module.
Program Output
For each test case, output one of these lines:
stack
not stack
which will indicate whether or not the series of operations are behaving like a stack in LIFO order
Sample Input
Create a text file with the following lines and use it as input into your program.
i cat
i dog
r dog
rmathrm cat
sunshine
i apple
i street
r apple
r street
rmathrm sunshine
red
red
i blue
i green
r green
#include
#include
#include
using namespace std;
#define INPUTFILE "test.txt
int main
ifstream fINPUTFILE;
string tmp;
bool failed false;
if fgood
cout "Invalid file." endl;
exit;
while true
getlinef tmp;
if feof
return ;
failed false;
int n atoitmpcstr;
stack st;
while n
getlinef tmp;
char op tmp;
string word tmpsubstr;
if op i
stpushword;
else if op r
if stempty sttop word
failed true;
else
stpop;
if stempty
failed true;
if failed
cout "not stack" endl;
else
cout "stack" endl;
return ;
the output when I run the code is: Invalid file.
Program ended with exit code:
can you help me fix it because it is not letting me input this:
i cat
i dog
r dog
r cat
i sunshine
i apple
i street
r apple
r street
r sunshine
i red
r red
i blue
i green
r green
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
