write a program in C++ that reads a text file containing a number of words and d
ID: 3919103 • Letter: W
Question
write a program in C++ that reads a text file containing a number of words and displays the words on the screen in reverse order using a stack. A pseudo?code outline for the program is given below:Begin main display a prompt for the file name read in the file name try to open the file if the file fails to open print an error message on the screen and exit fi do read in a word from the file if the file read fails terminate (break) the loop fi Push the word onto the stack od close the file while the stack is not empty display the top stack word on the screen followed by a space pop the top value from the stack elihw End main
(Backwards words are the statement being closed, e.g if statements with a fi)
The stack must be implemented using a fixed size array of words and an index integer for indicating the top of the stack.
Thank you in advance for your help! write a program in C++ that reads a text file containing a number of words and displays the words on the screen in reverse order using a stack. A pseudo?code outline for the program is given below:
Begin main display a prompt for the file name read in the file name try to open the file if the file fails to open print an error message on the screen and exit fi do read in a word from the file if the file read fails terminate (break) the loop fi Push the word onto the stack od close the file while the stack is not empty display the top stack word on the screen followed by a space pop the top value from the stack elihw End main
(Backwards words are the statement being closed, e.g if statements with a fi)
The stack must be implemented using a fixed size array of words and an index integer for indicating the top of the stack.
Thank you in advance for your help! write a program in C++ that reads a text file containing a number of words and displays the words on the screen in reverse order using a stack. A pseudo?code outline for the program is given below:
Begin main display a prompt for the file name read in the file name try to open the file if the file fails to open print an error message on the screen and exit fi do read in a word from the file if the file read fails terminate (break) the loop fi Push the word onto the stack od close the file while the stack is not empty display the top stack word on the screen followed by a space pop the top value from the stack elihw End main
(Backwards words are the statement being closed, e.g if statements with a fi)
The stack must be implemented using a fixed size array of words and an index integer for indicating the top of the stack.
Thank you in advance for your help!
Explanation / Answer
Please find the code below.
CODE
===============
#include<bits/stdc++.h>
#include <fstream>
using namespace std;
#define MAX 1000
class Stack
{
int top;
public:
string a[MAX]; //Maximum size of Stack
Stack() { top = -1; }
bool push(string x);
string pop();
bool isEmpty();
};
bool Stack::push(string x)
{
if (top >= (MAX-1))
{
cout << "Stack Overflow";
return false;
}
else
{
a[++top] = x;
return true;
}
}
string Stack::pop()
{
if (top < 0)
{
cout << "Stack Underflow";
return "";
}
else
{
string x = a[top--];
return x;
}
}
bool Stack::isEmpty()
{
return (top < 0);
}
int main() {
ifstream input("input.txt");
Stack st;
if(!input) {
cout << "Cannot open input file. ";
return 1;
}
string line;
while (getline(input, line)) {
st.push(line);
}
while(st.isEmpty() == false) {
cout << st.pop() << " ";
}
input.close();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.