Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to implement the algorithm for evaluating postfix expressions th

ID: 3621759 • 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

#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
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

Hope this helps. Let me know if you have any questions. Please rate. :) #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' && token
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote