fork download
#include<bits/stdc++.h>
using namespace std ;

int precedence(char c){
    if(c == '*' || c == '/') return 5 ;
    if(c == '+' || c == '-') return 4 ;
    return 0 ;
}

void solve(string s){
    stack<char> st ;
    string res = "";
    for(int i = 0 ; i < s.size();i++){
        if(isalpha(s[i])) res += s[i];
        else if(s[i] == '(')   st.push(s[i]);
        else if(s[i] == ')'){
            while(!st.empty() && st.top() != '('){
                res += st.top();
                st.pop();
            }
            st.pop();
        }
        else{
            while(!st.empty() && precedence(st.top()) >= precedence(s[i])){
                res += st.top();
                st.pop();
            }
            st.push(s[i]);
        }
    }
    while(!st.empty()){
        res += st.top();
        st.pop();
    }
    cout << res ;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    string s ; cin >> s; 
    solve(s);
}
Success #stdin #stdout 0s 5304KB
stdin
(A-(B+C))*(a+b*c)
stdout
ABC+-abc*+*