#include #include #include #include using namespace std; // Prioritatea operatorilor int getPrecedence(char op) { if (op == '+' || op == '-') return 1; if (op == '*' || op == '/') return 2; return 0; } // Verifică dacă un caracter este operator bool isOperator(char ch) { return ch == '+' || ch == '-' || ch == '*' || ch == '/'; } // Convertire expresie infix → postfix string infixToPostfix(const string& infix) { stack operators; string postfix; for (size_t i = 0; i < infix.length(); ++i) { char ch = infix[i]; // Dacă este spațiu, îl ignorăm if (ch == ' ') continue; // Dacă este cifră, extragem întregul număr if (isdigit(ch)) { while (i < infix.length() && isdigit(infix[i])) { postfix += infix[i]; i++; } postfix += ' '; // separare între termeni i--; // revenim la poziția de dinainte } else if (ch == '(') { operators.push(ch); } else if (ch == ')') { while (!operators.empty() && operators.top() != '(') { postfix += operators.top(); postfix += ' '; operators.pop(); } if (!operators.empty() && operators.top() == '(') { operators.pop(); // eliminăm '(' } } else if (isOperator(ch)) { while (!operators.empty() && getPrecedence(operators.top()) >= getPrecedence(ch)) { postfix += operators.top(); postfix += ' '; operators.pop(); } operators.push(ch); } } // Scoatem operatorii rămași while (!operators.empty()) { postfix += operators.top(); postfix += ' '; operators.pop(); } return postfix; } int main() { string input; getline(cin, input); // citim expresia întreagă (poate conține spații) string result = infixToPostfix(input); // Eliminăm spațiul de la final (opțional) if (!result.empty() && result.back() == ' ') result.poack(); cout << result << endl; return 0; }