Write a program to implement the algorithm for evaluating postfix expressions th
ID: 3621743 • Letter: W
Question
Write a program to implement the algorithm for evaluating postfix expressions that involve only single-digit integers and the integer operations +, -, *, and /. To trace the action of postfix ecaluation, display each token as it is encountered, and display the action of each stack operation. For example, the output of your program might resemble the following for the postfix experession:
9 2 1 + / 4 *:
Token = 9 Push 9
Token = 2 Push 2
Token = 1 Push 1
Token = + Pop 1 Pop 2 Push 3
Token = / Pop 3 Pop 9 Push 3
Token = 4 Push 4
Token = * Pop 4 Pop 3 Push 12
Token = Pop 12
Answer is 12
Then modify the postfix evaluation to detect and report postfix expresstions that are notwell formed.
Another example run is:
Enter a postfix expression
2 3 4 + =
Token: 2 Push 2
Token: 3 Push 3
Token: 4 Push 4
Token: + Pop 4 Pop 3 Push 7
Token: = Pop 7
The expression has an error.
For this problem, you must use the static array based Stack given. I will list the Stack implementation below:
-------------------------------------------------------------------------------
Stack.h
#include <iostream>
#ifndef STACK
#define STACK
const int STACK_CAPACITY = 128;
typedef int StackElement;
class Stack
{
public:
Stack();
bool empty() const;
void push(const StackElement & value);
void display(ostream & out) const;
StackElement top() const;
void pop();
private:
StackElement myArray[STACK_CAPACITY];
int myTop;
}; // end of class declaration
#endif
---------------------------------------------------------------------------------
Stack.cpp
#include <iostream>
using namespace std;
#include "Stack.h"
//--- Definition of Stack constructor
Stack::Stack()
: myTop(-1)
{}
//--- Definition of empty()
bool Stack::empty() const
{
return (myTop == -1);
}
//--- Definition of push()
void Stack::push(const StackElement & value)
{
if (myTop < STACK_CAPACITY - 1) //Preserve stack invariant
{
++myTop;
myArray[myTop] = value;
}
else
{
cerr << "*** Stack full -- can't add new value *** "
"Must increase value of STACK_CAPACITY in Stack.h ";
exit(1);
}
}
//--- Definition of display()
void Stack::display(ostream & out) const
{
for (int i = myTop; i >= 0; i--)
out << myArray[i] << endl;
}
//--- Definition of top()
StackElement Stack::top() const
{
if ( !empty() )
return (myArray[myTop]);
else
{
cerr << "*** Stack is empty -- returning garbage value *** ";
StackElement garbage;
return garbage;
}
}
//--- Definition of pop()
void Stack::pop()
{
if ( !empty() )
myTop--;
else
cerr << "*** Stack is empty -- can't remove a value *** ";
}
Explanation / Answer
Did this not work? Let me know if there are any problems - either through a comment here or a private message. #include "Stack.h" #include #include using namespace std; int main() { Stack stack; string str; char token; getline(cin, str); int len = str.length(); for (int i = 0; i = '0' && tokenRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.