Created
March 30, 2014 13:23
-
-
Save ibigbug/9872750 to your computer and use it in GitHub Desktop.
Calculate Reverse Polish notation Demo
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
#include <stdio.h> | |
#include <stdlib.h> | |
int main (int argc, char * argv[]) | |
{ | |
int a = 0, b = 0; | |
int * stack = malloc(sizeof(int) * 20); | |
if (stack == NULL) | |
return -1; | |
char * cur; | |
argc--; | |
argv++; // goodbye filename | |
while (argc-- > 0) { | |
cur = *argv++; | |
if (cur[0] == '+') { | |
a = *--stack; | |
b = *--stack; | |
*stack++ = a + b; | |
} else if (cur[0] == '*') { | |
a = *--stack; | |
b = *--stack; | |
*stack++ = a * b; | |
} else { | |
*stack++ = atoi(cur); | |
} | |
} | |
printf("result=%d\n", *--stack); | |
free(stack); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
感觉写了这个之后对指针和数组就理解差不多了。