#include <iostream>
#include <stack> // don't have time to implement a stack by myself
using namespace std;

// function to check precedence
int pre(char op);

int main()
{
    string infix;
    cout << "Expression: ";
    cin >> infix;

    stack<char> st;
    string postfix = "";
    for (int i = 0; i < infix.length(); i++)
    {
        char c = infix[i];
        if (c >= '0' && c <= '9') // To make sure we are adding any number we meet
            postfix += c;

        else if (c == '(') // open prenthesess are added to our cup
            st.push(c);

        else if (c == ')')
        {
            // making sure we are poping from not empty stack
            while (!st.empty() && st.top() != '(')
            {
                postfix += st.top();
                st.pop();
            }
            st.pop(); // to remove one '(', aka remove one open prentheesss
        }
        else if (c == '+' || c == '-' || c == '*' || c == '/' || c == '^')
        {
            while (!st.empty() && pre(st.top()) >= pre(c))
            {
                postfix += st.top();
                st.pop();
            }
            st.push(c);
        }
    }
    while (!st.empty())
    {
        postfix += st.top();
        st.pop();
    }

    cout << "After Converting To Postfix: " << postfix;

    return 0;
}

// What comes before the other ..uuuh
int pre(char op)
{
    if (op == '^')
        return 3;
    if (op == '*' || op == '/')
        return 2;
    if (op == '+' || op == '-')
        return 1;
    return 0;
}