试题描述
|
后缀表达式是指运算符放在两个运算对象之后,所有计算按运算符出现的顺序,严格地从左到右进行。注意每个操作数之后都紧跟着一个“.”。
|
输入
|
一行,一个后缀表达式。
|
输出
|
一行,一个整数,表示后缀表达式的值。
|
输入示例
|
3.5.2.-*7.+
|
输出示例
|
16
|
其他说明
|
表达式长度不超过50,所有操作数的值以及所有中间运算结果以及最终结果的绝对值均不超过10^6。
|
C程序:
#include<cstdio> #include<cstdlib> #include<cmath> #include<iostream> using namespace std; int st[51]; int top; void push(int ch){top++;st[top]=ch;} void pop(){top--;} void clear(){top=0;} int main() { char s[51]; cin>>s; int ch=0; for(int i=0;i<strlen(s);i++) { if(s[i]=='.') { push(ch); ch=0; } else if(s[i]=='+') { int temp=st[top]+st[top-1]; pop();pop(); push(temp); } else if(s[i]=='-') { int temp=st[top-1]-st[top]; pop();pop(); push(temp); } else if(s[i]=='*') { int temp=st[top]*st[top-1]; pop();pop(); push(temp); } else ch=ch*10+(s[i]-'0'); } printf("%d",st[top]); return 0; }