Created
May 16, 2009 20:07
-
-
Save ashutoshmehra/112787 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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