// editor1
#include <stdio.h>
#include <ctype.h>
#define MAX 100

char stack[MAX];
int top = -1;

void push(char c) { if (top < MAX - 1) stack[++top] = c; }
char pop() { return top >= 0 ? stack[top--] : '\0'; }
char peek() { return top >= 0 ? stack[top] : '\0'; }
int isEmpty() { return top == -1; }
int precedence(char c) { return (c == '+' || c == '-') ? 1 : (c == '*' || c == '/') ? 2 : (c == '^') ? 3 : 0; }

int main() {
    char infix[MAX], postfix[MAX];
    int j = 0, valid = 1;

    printf("Enter infix: ");
    fgets(infix, sizeof(infix), stdin);
    infix[strcspn(infix, "\n")] = 0;

    for (char *c = infix; *c; c++) {
        if (isalnum(*c)) postfix[j++] = *c;
        else if (*c == '(') push(*c);
        else if (*c == ')') {
            while (peek() != '(' && !isEmpty()) postfix[j++] = pop();
            if (peek() == '(') pop();
        } else if (c == '+' || *c == '-' || *c == '' || *c == '/' || *c == '^') {
            while (!isEmpty() && peek() != '(' && precedence(*c) <= precedence(peek())) postfix[j++] = pop();
            push(*c);
        } else valid = 0;
    }

    while (!isEmpty()) postfix[j++] = pop();
    postfix[j] = '\0';

    if (valid) printf("Postfix: %s\n", postfix);
    else printf("Invalid input\n");

    return 0;
}