SDUT 2133 数据结构实验之栈三:后缀式求值
后缀式求值的方法参见我的另一篇文章 把运算符变成表达式
1 #include<stdio.h>
2 #include<string.h>
3 int main()
4 {
5 int top = 0,i,k,s,num[50];
6 char str;
7 while(scanf("%c", &str),str!='#')
8 {
9 if(str>='0'&&str<='9')
10 num[++top] = str-48;
11 else
12 {
13 switch(str)
14 {
15 case '+':s = (num[top-1])+(num[top]);break;
16 case '-':s = (num[top-1])-(num[top]);break;
17 case '*':s = (num[top-1])*(num[top]);break;
18 case '/':s = (num[top-1])/(num[top]);break;
19 }
20 top--;
21 num[top] = s;
22 }
23 }
24 printf("%d\n",num[top]);
25 return 0 ;
26 }