【题目描述】
从键盘读入一个后缀表达式(字符串),只含有0-9组成的运算数及加(+)、减(—)、乘(*)、除(/)四种运算符。每个运算数之间用一个空格隔开,不需要判断给你的表达式是否合法。以@作为结束标志。
比如,16–9*(4+3)转换成后缀表达式为:16□9□4□3□+*–,在字符数组A中的形式为:
栈中的变化情况:
运行结果:-47
提示:输入字符串长度小于250,参与运算的整数及结果之绝对值均在2^64范围内,如有除法保证能整除。
【输入】
一个后缀表达式。
【输出】
一个后缀表达式的值。
【输入样例】
16 9 4 3 +*-@
【输出样例】
-47
【源程序】
#include<iostream>#include<cstdio>#include<cstdlib>#include<string>#include<cstring>#include<cmath>#include<ctime>#include<algorithm>#include<utility>#include<stack>#include<queue>#include<vector>#include<set>#include<map>#define PI acos(-1.0)#define E 1e-9#define INF 0x3f3f3f3f#define LL long longconst int MOD=1000000007;const int N=10000+5;const int dx[]= {-1,1,0,0};const int dy[]= {0,0,-1,1};using namespace std;char str[N];stack<LL> S;int main(){gets(str);int len=strlen(str)-1;for(int i=0;i<len;i++){switch(str[i]){case '+':{LL y=S.top();S.pop();LL x=S.top();S.pop();S.push(x+y);break;}case '-':{LL y=S.top();S.pop();LL x=S.top();S.pop();S.push(x-y);break;}case '*':{LL y=S.top();S.pop();LL x=S.top();S.pop();S.push(x*y);break;}case '/':{LL y=S.top();S.pop();LL x=S.top();S.pop();S.push(x/y);break;}case '@':i=len;break;default:{LL temp=0;while(str[i]!=' ')temp=temp*10+str[i]-'0',i++;S.push(temp);break;}}}cout<<S.top()<<endl;return 0;}