Evaluation of Postfix Expression

Tags : stack, geeksforgeeks, cpp, easy

Given string S representing a postfix expression, the task is to evaluate the expression and find the final value. Operators will only include the basic arithmetic operators like *, /, + and -.

Your Task: You do not need to read input or print anything. Complete the function evaluatePostfixExpression() that takes the string S denoting the expression as input parameter and returns the evaluated value.

Expected Time Complexity: O(N) Expected Auxiliary Space: O(N)

Examples #

Example 1:

Input: S = "231*+9-"
Output: -4
Explanation:
After solving the given expression, 
we have -4 as result.

Example 2:

Input: S = "123+*8-"
Output: -3
Explanation:
After solving the given postfix 
expression, we have -3 as result.

Constraints #

Solutions #

class Solution {
    public:
    //Function to evaluate a postfix expression.
    int evaluatePostfix(string str)
    {
        // Your code here
        stack<int> s;
        for(char c: str){
            if(isdigit(c)) s.push(c-'0');
            else {
                int b = s.top(); s.pop();
                int a = s.top(); s.pop();
                switch(c){
                    case '+': s.push(a+b); break;
                    case '-': s.push(a-b); break;
                    case '*': s.push(a*b); break;
                    case '/': s.push(a/b); break;
                    default : break;
                }
            }
        }
        return s.top();
    }
};