Skip to content

Instantly share code, notes, and snippets.

@ibigbug
Created March 30, 2014 13:23
Show Gist options
  • Save ibigbug/9872750 to your computer and use it in GitHub Desktop.
Save ibigbug/9872750 to your computer and use it in GitHub Desktop.
Calculate Reverse Polish notation Demo
#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;
}
@ibigbug
Copy link
Author

ibigbug commented Mar 30, 2014

感觉写了这个之后对指针和数组就理解差不多了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment