#include <iostream>
#include <stack>
using namespace std;

stack<int> num;
stack<char> op;

int priority(char c){
    if(c=='+'||c=='-') return 1;
    if(c=='*'||c=='/') return 2;
    return 0;
}

void calc(){
    int b=num.top(); num.pop();
    int a=num.top(); num.pop();

    char c=op.top(); op.pop();

    if(c=='+') num.push(a+b);
    if(c=='-') num.push(a-b);
    if(c=='*') num.push(a*b);
    if(c=='/') num.push(a/b);
}

int main(){
    string s;
    cin>>s;
    
    for(int i=0;i<s.size();){
        if(isdigit(s[i])){
            int x=0;
            while(i<s.size() && isdigit(s[i])){
                x=x*10+s[i]-'0';
                i++;
            }
            num.push(x);
        }
        else if(s[i]=='('){
            op.push('(');
            i++;
        }
        else if(s[i]==')'){
            while(op.top()!='(') calc();
            op.pop();
            i++;
        }
        else{
            while(!op.empty() && op.top()!='(' && priority(op.top())>=priority(s[i]))
                calc();

            op.push(s[i]);
            i++;
        }
    }
    while(!op.empty()) calc();

    cout<<num.top();
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: