对栈

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;
}

 

posted @ 2018-07-26 20:20  无言丶  阅读(158)  评论(0编辑  收藏  举报