Untitled

 avatar
DanielGarciaMendez
plain_text
a month ago
622 B
29
Indexable
#include <iostream>
#include <stack>
using namespace std;

int evaluatePostfix(string expr) {
    stack<int> st;
    for (char ch : expr) {
        if (isdigit(ch)) st.push(ch - '0');
        else {
            int val2 = st.top(); st.pop();
            int val1 = st.top(); st.pop();
            switch (ch) {
                case '+': st.push(val1 + val2); break;
                case '-': st.push(val1 - val2); break;
                case '*': st.push(val1 * val2); break;
            }
        }
    }
    return st.top();
}

int main() {
    cout << evaluatePostfix("53+62/*");
    return 0;
}
Editor is loading...
Leave a Comment