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

USING THE CODE BELOW.... Write a program that will use a stack to evaluate a pos

ID: 3555114 • Letter: U

Question

USING THE CODE BELOW.... Write a program that will use a stack to evaluate a postfix equation. The equation will consist of 2 digit numbers and the operations + , *, -, / . The equation will end with a =.

For example
25 03 * 02 + =

The result would be 77

08 04 03 * 05 + 02 + + =

The result would be 27

1) The current code only works for digits 1 to 5. Add the code for the remaining digits and make your program work for 2 digit numbers.

2) Add the addition math operators.

3) add the code for push and pop. The functions are already written, add the code .

4) Make your output look unique.

5) Add any other code needed to make your program work correctly.

Explanation / Answer

#include<iostream>
using namespace std;
#include<conio.h>
#include<string.h>
template<class T>
class evaluation
{
private:
T postfix[20];
int stack[20],top;
public:
evaluation()
{
top=-1;
}
void getpostfixexpr();
void calculate();
void display();
};
template<class T>
void evaluation<T>::getpostfixexpr()
{
cout<<" enter the postfix expression :";
cin>>postfix;
}
template<class T>
void evaluation<T>::display()
{
cout<<" the calculate value is :"<<stack[top];
}
template<class T>
void evaluation<T>::calculate()
{
int l=strlen(postfix);
int k=l;
for(int i=0;i<k;i++)
{
int x;
if((postfix[i]>='a'&&postfix[i]<='z')||(postfix[i]>='A'&&postfix[i]<='Z'))
{
cout<<" enter the value of "<<postfix[i]<<":";
cin>>x;
stack[++top]=x;
}
else
{
int a=stack[top];
int b=stack[top-1];
switch(postfix[i])
{
case '+':
stack[top-1]=b+a;
top--;
break;
case '-':
stack[top-1]=b-a;
top--;
break;
case '*':
stack[top-1]=b*a;
top--;
break;
case '/':
stack[top-1]=b/a;
top--;
break;
}
}
}
}
int main()
{
evaluation<char> e;
e.getpostfixexpr();
e.calculate();
e.display();
_getch();
return 0;
}