Algorithms Quiz | SP Contest 3 | Question 10
Given below is a C++ function to evaluate a postfix expression represented as a string. The below code contains proper comments and some statements are marked specially. Find the statements which will lead to incorrect output.
int evaluatePostfix( char * exp )
{
stack< int > st;
int i;
for (i = 0; exp [i]; ++i)
{
if ( isdigit ( exp [i]))
st.push( exp [i]);
else
{
int val1 = st.top();
st.pop();
int val2 = st.top();
st.pop();
switch ( exp [i])
{
case '+' : st.push(val2 + val1); break ;
case '-' : st.push(val2 - val1); break ;
case '*' : st.push(val2 * val1); break ;
case '/' : st.push(val2/val1); break ;
}
}
}
return st.top();
}
|
(A) Statement 2
(B) Statement 1
(C) Statement 3 & Statement 1
(D) None of the above
Answer: (B)
Explanation: https://www.geeksforgeeks.org/stack-set-4-evaluation-postfix-expression/
Quiz of this Question
Last Updated :
25 Jul, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...