芜湖有哪些招聘网站,做海报的素材哪个网站,中国电信的视频播放器,前端开发招聘要求3302. 表达式求值 - AcWing题库
给定一个表达式#xff0c;其中运算符仅包含 ,-,*,/#xff08;加 减 乘 整除#xff09;#xff0c;可能包含括号#xff0c;请你求出表达式的最终值。
注意#xff1a;
数据保证给定的表达式合法。题目保证符号 - 只作为减号出现其中运算符仅包含 ,-,*,/加 减 乘 整除可能包含括号请你求出表达式的最终值。
注意
数据保证给定的表达式合法。题目保证符号 - 只作为减号出现不会作为负号出现例如-12,(22)*(-(11)2) 之类表达式均不会出现。题目保证表达式中所有数字均为正整数。题目保证表达式在中间计算过程以及结果中均不超过 231−1。题目中的整除是指向 0 取整也就是说对于大于 0 的结果向下取整例如 5/31对于小于 0 的结果向上取整例如 5/(1−4)−1。C和Java中的整除默认是向零取整Python中的整除//默认向下取整因此Python的eval()函数中的整除也是向下取整在本题中不能直接使用。
输入格式
共一行为给定表达式。
输出格式
共一行为表达式的结果。
数据范围
表达式的长度不超过 105。
输入样例
(22)*(11)输出样例
8
解析
AcWing 3302. 表达式求值每日一题·春季 - AcWing
#includeiostream
#includestring
#includecstring
#includecmath
#includectime
#includealgorithm
#includeutility
#includestack
#includequeue
#includevector
#includeset
#includemath.h
#includemap
#includeunordered_map
using namespace std;
typedef long long LL;
const int N 1e5 5, M 3e5 5;
stackintnum;
stackcharop;
string s;void eval() {int a num.top(); num.pop();int b num.top(); num.pop();char c op.top(); op.pop();int x 0;if (c ) {x a b;}else if (c -)x b - a;else if (c *)x a * b;elsex b / a;num.push(x);
}int main() {unordered_mapchar, intmp { {,1},{-,1},{*,2},{/,2} };cin s;for (int i 0; i s.size(); i) {auto c s[i];if (isdigit(c)) {int x 0, j i;while (j s.size() isdigit(s[j])) {x x * 10 s[j] - 0;}i j - 1;num.push(x);}else if (c ()op.push(c);else if (c )) {while (op.top() ! ()eval();op.pop();}else {while (op.size() mp[op.top()] mp[c])eval();op.push(c);}}while (op.size())eval();cout num.top() endl;return 0;
}