I am trying the Infix to Prefix conversion and evaluation in C++. Someone here guided me and my converstion was resolved easily. But now I am having problems while evaluating it. The problem is that cout is not printing the return value from my function. Following is my code:
int PrefixEvaluation(string s)
{
stack<int> st;
for(int i=s.length()-1; i>=0; i--)
{
if(s[i] >= '0' && s[i] <= '9')
{
st.push(s[i] - '0');
} // if
else
{
int op1 = st.top();
st.pop();
int op2 = st.top();
st.pop();
switch (s[i])
{
case '+':
st.push(op1+op2);
break;
case '-':
st.push(op1-op2);
break;
case '*':
st.push(op1*op2);
break;
case '/':
st.push(op1/op2);
break;
case '^':
st.push(pow(op1,op2));
break;
default:
cout<<"Invalid"<<endl;
break;
} // switch
} // else
} // for loop
cout<<st.top()<<endl;
return st.top();
} // PrefixEvaluation()
int main()
{
cout<<"Prefix Evaluation: "<<PrefixEvaluation("+*^55/9/9*974")<<endl;
} // main
There is no error displayed. The console simply displays a blank screen. Please help. Thank you.