输入一个中缀表达式(由 0-9 组成的运算数、加+减—乘*除/四种运算符、左右小括号组成。注意“—”也可作为负数的标志,表达式以“@”作为结束符),判断表达式是否合法,如果不合法,请输出“NO”;否则请把表达式转换成后缀形式,再求出后缀表达式的值并输出。
注意:必须用栈操作,不能直接输出表达式的值。
输入
输入一行为一个以@结束的字符串。
输出
如果表达式不合法,请输出“NO”,要求大写。
如果表达式合法,请输出计算结果。
样例
输入
1+2*8-9@
输出
8
提示
注意其中的加减乘除符号!!
[参考代码]
#include<bits/stdc++.h>
using namespace std;
char str[10000];
int n;
stack<int>s1;//数字
stack<char>s2;//运算符
void calc() {
int y=s1.top();
s1.pop();
int x=s1.top();
s1.pop();
char z=s2.top();
s2.pop();
if(z=='+')
s1.push(x+y);
else if(z=='-')
s1.push(x-y);
else if(z=='*')
s1.push(x*y);
else
s1.push(x/y);
}
i