Skip to main content
Added ungolfed version
Source Link
breadbox
  • 7.3k
  • 33
  • 45

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; } 

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; } 
Removed leading-whitespace logic.
Source Link
breadbox
  • 7.3k
  • 33
  • 45

C, 250 245 236 193193 185 chars

char*p,b[99],a;fb[99];f(char*s){int t=0;for(;*p-32? *p>47?a=printfprintf(""%d %d"+!a",strtol(p,&p,10)):*p==40?f(p++),++p: t*(s[t]%5==2|*p%5t&&s[t]%5==2|*p%5-2)?printf(""%c %c"",s[t--]):*p>41?s[++t]=*p++:0:++p;);} main(){f(p=gets(b));} 

I can still remove 7 more chars if we're allowed to have a leading space in the output. Just sayin'.

C, 250 245 236 193 chars

char*p,b[99],a;f(char*s){int t=0;for(;*p-32? *p>47?a=printf(" %d"+!a,strtol(p,&p,10)):*p==40?f(p++),++p: t*(s[t]%5==2|*p%5-2)?printf(" %c",s[t--]):*p>41?s[++t]=*p++:0:++p;);} main(){f(p=gets(b));} 

I can still remove 7 more chars if we're allowed to have a leading space in the output. Just sayin'.

C, 250 245 236 193 185 chars

char*p,b[99];f(char*s){int t=0;for(;*p-32? *p>47?printf("%d ",strtol(p,&p,10)):*p==40?f(p++),++p: t&&s[t]%5==2|*p%5-2?printf("%c ",s[t--]):*p>41?s[++t]=*p++:0:++p;);} main(){f(p=gets(b));} 
code revision
Source Link
breadbox
  • 7.3k
  • 33
  • 45

C, 250 245 236236 193 chars

char*p,b[64]b[99],a;f(char*s){int t=0;for(;;++p)if(*p^32&&!(*p>47;*p-32? *p>47?a=printf(" %d"+!a,strtol(p,&p,10)),--p:*p==40&&f*p==40?f(++p))p++){,++p: while(t*(s[t]^43&&s[t]^45||*p^42&&*p^47)s[t]%5==2|*p%5-2)?printf(" %c",s[t--]); if(!*p||*p==41:*p>41?s[++t]=*p++:0:++p;)return 1;s[++t]=*p;};}  main(){f(p=gets(b));} 

I can still remove 7 more chars if we're allowed to have a leading space in the output. Just sayin'.

C, 250 245 236 chars

char*p,b[64],a;f(char*s){int t=0;for(;;++p)if(*p^32&&!(*p>47? a=printf(" %d"+!a,strtol(p,&p,10)),--p:*p==40&&f(++p))){ while(t*(s[t]^43&&s[t]^45||*p^42&&*p^47))printf(" %c",s[t--]); if(!*p||*p==41)return 1;s[++t]=*p;}}main(){f(p=gets(b));} 

I can remove 7 more chars if we're allowed to have a leading space in the output. Just sayin'.

C, 250 245 236 193 chars

char*p,b[99],a;f(char*s){int t=0;for(;*p-32? *p>47?a=printf(" %d"+!a,strtol(p,&p,10)):*p==40?f(p++),++p: t*(s[t]%5==2|*p%5-2)?printf(" %c",s[t--]):*p>41?s[++t]=*p++:0:++p;);}  main(){f(p=gets(b));} 

I can still remove 7 more chars if we're allowed to have a leading space in the output. Just sayin'.

corrected mispaste
Source Link
breadbox
  • 7.3k
  • 33
  • 45
Loading
reduced recursion
Source Link
breadbox
  • 7.3k
  • 33
  • 45
Loading
Small improvement
Source Link
breadbox
  • 7.3k
  • 33
  • 45
Loading
Source Link
breadbox
  • 7.3k
  • 33
  • 45
Loading