c++ program with code
#include<iostream>
#include<cctype>
using namespace std;
class evalPostFix
{
public:
int s[50];
int top;
char str[50];
evalPostFix() {
top=-1;
}
void push(int val) {
top++;
s[top]=val;
}
int pop() {
int val=s[top];
top--;
return val;
}
int oper(int x,int y,char op) {
switch(op) {
case '+':return x+y;
case '-':return x-y;
case '*':return x*y;
case '/':return x/y;
default: return 0;
}
}
int calc();
};
int evalPostFix::calc()
{
int i=0; int j = 0;
while(str[j]!='$')
{
if (isalpha(str[j]))
{
cout<<"Enter the value of "<<str[j];
cin>> str[j];
}
j++;
}
while(str[i]!='$') {
if(isdigit(str[i])) {
push(str[i]-'0');
}
else {
int x=pop();
int y=pop();
int r=oper(x,y,str[i]);
push(r);
}
i++;
}
return pop();
}
int main()
{
char answer;
evalPostFix eval;
do
{
cout<<"Enter a postfix expression with a $ at the end:" << endl;
cin>>eval.str;
int r=eval.calc();
cout<<"Final Value : "<<r <<"\n";
cout << "Continue (Y or N)?" << endl;
cin >> answer;
}
while(answer!='N');
system("pause");
}
Here is the output, but I do not want it to be like this.
I want the output to be like this