Here's a readable version of the ungolfed source, that still reflects the basic logic. It's actually a rather straightforward program. The only real work it has to do is to push a low-associativity operator onto a stack when a high-associativity operator is encountered, and then pop it back off at the "end" of that subexpression.
#include <stdio.h> #include <stdlib.h> static char buf[256], stack[256]; static char *p = buf; static char *fix(char *ops) { int sp = 0; for ( ; *p && *p != '\n' && *p != ')' ; ++p) { if (*p == ' ') { continue; } else if (*p >= '0') { printf("%ld ", strtol(p, &p, 10)); --p; } else if (*p == '(') { ++p; fix(ops + sp); } else { while (sp) { if ((ops[sp] == '+' || ops[sp] == '-') && (*p == '*' || *p == '/')) { break; } else { printf("%c ", ops[sp--]); } } ops[++sp] = *p; } } while (sp) printf("%c ", ops[sp--]); return p; } int main(void) { fgets(buf, sizeof buf, stdin); fix(stack); return 0; }