这一鸽,就到了2024年4月19日22:26:06,先上题!!
题目描述
所谓后缀表达式是指这样的一个表达式:式中不再引用括号,运算符号放在两个运算对象之后,所有计算按运算符号出现的顺序,严格地从左向右进行(不用考虑运算符的优先级)。
如:3*(5–2)+7对应的后缀表达式为:3.5.2.-*7.+@。’@’为表达式的结束符号。‘.’为操作数的结束符号。
输入描述
输入:后缀表达式
输出描述
输出:表达式的值
输入输出样例
-
输入
3.5.2.-*7.+@
输出
16
提示
字符串长度,1000内。
#include<bits/stdc++.h>
using namespace std;
char stk[5200];
int top=0;
int main(){
char c;
int now=0;
cin>>c;
while(c!='@'){
if('0'<=c&&c<='9'){
now=now*10+c-'0';
}
if(c=='.'){
stk[++top]=now;
now=0;
}
if(c=='+'){
int x=stk[top--];
x+=stk[top--];//简化了
stk[++top]=x;
now=0;
}
if(c=='-'){
int x=stk[top--];
int y=stk[top--];
stk[++top]=y-x;
now=0;
}
if(c=='*'){
int x=stk[top--];
x*=stk[top--];;//简化了
stk[++top]=x;
now=0;
}
if(c=='/'){
int x=stk[top--];//--top,top--是一样的,为了更容易接受,用top--
int y=stk[top--];
stk[++top]=y/x;
now=0;
}
cin>>c;
}
cout<<(int)stk[top];
return 0;//0
}