Skip to content

Instantly share code, notes, and snippets.

@ashutoshmehra
Created May 16, 2009 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashutoshmehra/112787 to your computer and use it in GitHub Desktop.
Save ashutoshmehra/112787 to your computer and use it in GitHub Desktop.
/* Solves the puzzle: Puzzle: recursive postfix evaluation.
Ref: http://rgrig.blogspot.com/2009/03/postfix-expressions.html
*/
#include <stdio.h>
#define F 0x40
char eval(int * pr) {
char ch;
int val;
switch((ch = getchar())) {
case '+': case '-': case '*': case '|': return ch;
default:
if(ch >= '0' && ch <= '9') {
*pr = ch - '0';
while(1) {
switch((ch = eval(&val))) {
case '+': case '-': case '*': return ch^F;
case '|': return '|';
case '+'^F: *pr += val; break;
case '*'^F: *pr *= val; break;
case '-'^F: *pr -= val; break;
}
}
}
}
return ' ';
}
int main() {
int r;
eval(&r);
printf("%d\n", r);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment