#include <iostream>
#include <stack>
#include <string>
#include <sstream>
#include <cctype>
#include <vector>
#include <map>

using namespace std;

// Пріоритети операторів
map<char, int> precedence = {
    {'+', 1}, {'-', 1},
    {'*', 2}, {'/', 2},
    {'(', 0}  // Дужки мають найнижчий пріоритет
};

// Функція перевірки, чи символ є оператором
bool isOperator(char c) {
    return precedence.count(c) > 0;
}

// Перетворення інфіксного виразу в постфіксний
string infixToPostfix(const string& infix) {
    stack<char> operators;
    stringstream output;

    for (size_t i = 0; i < infix.length(); i++) {
        char token = infix[i];

        // Якщо символ — число або літера, додаємо його у вихідний рядок
        if (isalnum(token)) {
            output << token;
        }
        // Якщо символ — оператор
        else if (isOperator(token)) {
            while (!operators.empty() && precedence[operators.top()] >= precedence[token]) {
                output << ' ' << operators.top();
                operators.pop();
            }
            output << ' ';
            operators.push(token);
        }
        // Якщо символ — відкрита дужка, додаємо в стек
        else if (token == '(') {
            operators.push(token);
        }
        // Якщо символ — закрита дужка, обробляємо стек
        else if (token == ')') {
            while (!operators.empty() && operators.top() != '(') {
                output << ' ' << operators.top();
                operators.pop();
            }
            operators.pop(); // Видаляємо '('
        }
    }

    // Витягуємо залишки операторів зі стеку
    while (!operators.empty()) {
        output << ' ' << operators.top();
        operators.pop();
    }

    return output.str();
}

int main() {
    string infix = "3+4*2/(1-5)";
    cout << "Інфіксний запис: " << infix << endl;
    cout << "Постфіксний запис: " << infixToPostfix(infix) << endl;
    return 0;
}
