对栈
1331【例1-2】后綴表達式的值
#include<bits/stdc++.h>
using namespace std;
int sta[101];
char s[256];
int comp(char s[256])
{
int i=0,top=0,x;
while(i<=strlen(s)-2)
{
switch(s[i])
{
case'+':sta[--top]+=sta[top+1];break;
case'-':sta[--top]-=sta[top+1];break;
case'*':sta[--top]*=sta[top+1];break;
case'/':sta[--top]/=sta[top+1];break;
default: x=0;
while(s[i]!=' ')x=x*10+s[i++]-'0';
sta[++top]=x;
break;
}
i++;
}
return sta[top];
}
int main()
{
gets(s);
cout<<comp(s);
return 0;
}
?
轉載于:https://www.cnblogs.com/jiazizheng/p/9374132.html
總結
- 上一篇: 145元的祖传5W充电器终结 苹果要从i
- 下一篇: 造车新势力又一起“断轴”事件 Rivia