#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
int x;
char s[100];
int stk[100];
int stk_ptr = 0;
int a, b;
while( scanf("%s", s) != EOF ){
if ( s[0] == '+' ){
b = stk[--stk_ptr];
a = stk[--stk_ptr];
stk[stk_ptr++] = a + b;
} else if ( s[0] == '-' ){
b = stk[--stk_ptr];
a = stk[--stk_ptr];
stk[stk_ptr++] = a - b;
} else if ( s[0] == '*' ){
b = stk[--stk_ptr];
a = stk[--stk_ptr];
stk[stk_ptr++] = a * b;
} else {
x = atoi(s);
stk[stk_ptr++] = x;
}
}
printf("%d\n", stk[0]);
return 0;
}
1#include<stdio.h>2#include<stdlib.h>3#include<string.h>4#include<ctype.h>56voidshow_stack(int* stack,int size){7for(int i =0; i < size; i++){8printf("%d ", stack[i]);9}10printf("\n");11}1213intmain(){14int x;15char s[100];16int stk[100];17int stk_ptr =0;18int a, b;1920while(scanf("%s", s)!=EOF){21if(isdigit(s[0])){22 x =atoi(s);23 stk[stk_ptr++]= x;24}else{25 b = stk[--stk_ptr];26 a = stk[--stk_ptr];27if(s[0]=='+'){28 stk[stk_ptr++]= a + b;29}elseif(s[0]=='-'){30 stk[stk_ptr++]= a - b;31}elseif(s[0]=='*'){32 stk[stk_ptr++]= a * b;33}elseif(s[0]=='='){34}else{35// エラー36}37}38show_stack(stk, stk_ptr);39}40return0;41}42