Last active
December 16, 2015 15:59
-
-
Save mopp/5459706 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
// http://judge.u-aizu.ac.jp/onlinejudge/lesson.jsp?id=ALDS1# | |
// 問題:3-B | |
#include<stdio.h> | |
#include<stdlib.h> | |
#define MAX_SIZE 100 | |
int stack[MAX_SIZE]; | |
int stack_pointer = 0; | |
void push(int data){ | |
stack[stack_pointer++] = data; | |
} | |
int pop(){ | |
return stack[--stack_pointer]; | |
} | |
int main(void){ | |
char s[MAX_SIZE]; | |
int a, b; | |
while( scanf("%s", s) != EOF ){ | |
switch (s[0]) { | |
case '+': | |
push(pop() + pop()); | |
break; | |
case '-': | |
/* | |
* "3 4 -"などを与えると | |
* -1ではなく1となってしまう | |
* push(-pop() + pop()); | |
* | |
*/ | |
a = pop(); | |
b = pop(); | |
push(b - a); | |
break; | |
case '*': | |
push(pop() * pop()); | |
break; | |
default: | |
push(atoi(s)); | |
break; | |
} | |
} | |
printf("%d\n", pop()); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment