Write a program to implement the algorithm for evaluating po

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 evaluation, display each token as it is encountered, and display the action of each stack operation.

1. Each digit or operator should be separated by a space, and end with :

2. You should apply stack ADT in the program. You can use the Standard Template Library (STL) stack instead of Stack class defined in the text book.

Here is an example how to use the STL stack push/pop/top and empty functions which will be used in this project.

3. You may need to use function isdigit(int c) in standard header file #include to check if the token character is digit or not.

4. To convert the single digit char c to the represented integer i, a simple method is to assign i by c -\'0\' (for example):

5. Paste your test output as the comments at the end of your source file proj5.cpp.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The sample run of the output is ---

Please enter the RPN expression to be evaluated:

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
type \'Y\' or \'y\' to continue or type any other letter to quit: y
(Note: Each digit or operator should be separated by a space, and end with :)
Please enter the RPN expression to be evaluated:

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 Pop 2 Push 14
Token = Pop 14

type \'Y\' or \'y\' to continue or type any other letter to quit: q
Press any key to continue

Solution

/*
* Postfix Evaluation
* Language: C++
*/

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <stack>
#include <string.h>

using namespace std;

bool isOperator(char ch)
{
if (ch==\'+\' || ch==\'-\' || ch==\'*\' || ch==\'/\')
return true;
else
return false;
}


int performOperation(int op1, int op2, char op)
{
int ans;
switch(op){
case \'+\':
ans = op2 + op1;
break;
case \'-\':
ans = op2 - op1;
break;
case \'*\':
ans = op2 * op1;
break;
case \'/\':
ans = op2 / op1;
break;
}
return ans;
}


int main()
{
char exp[1000], buffer[15];
int i,op1, op2, len, j, x;
stack<int> s;
char c=\'y\';
while(1)
{

printf(\"Enter a Postfix Expression: ( e.g. 23 34 * :)\ \");
gets(exp);
len = strlen(exp);
j = 0;
for(i=0; i<len-1;i++){

if(exp[i]>=\'0\' && exp[i]<=\'9\'){
buffer[j++] = exp[i];
}
else if(exp[i]==\' \'){
if(j>0){
buffer[j] = \'\\0\';
x = atoi(buffer);
cout<<\"Token = \"<<x<<\" push \"<<x<<endl;
s.push(x);
j = 0;
}
}

else if(isOperator(exp[i])){
  
op1 = s.top();
s.pop();
op2 = s.top();
s.pop();
s.push(performOperation(op1, op2, exp[i]));
cout<<\"token = \"<<exp[i]<<\" pop \"<<op1<<\" pop \"<<op2<<\" push \"<<s.top()<<endl;
}
}

printf(\"Answer is %d\ \", s.top());
cout<<\"type \'Y\' or \'y\' to continue or type any other letter to quit:\";
gets(exp);
if(exp[0]!=\'y\')
break;
  
}

return 0;
}


Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site