当前位置:网站首页>acwing 3302. Expression evaluation
acwing 3302. Expression evaluation
2022-07-04 20:55:00 【_ Liu Xiaoyu】
Given an expression , The operator only contains +,-,*,/( Add reduce ride to be divisible by ), May contain parentheses , Please find the final value of the expression .
Be careful :
The data guarantees that the given expression is legal .
Title Guarantee symbol - Appears only as a minus sign , Will not appear as a minus sign , for example ,-1+2,(2+2)*(-(1+1)+2) Such expressions will not appear .
Ensure that all numbers in the expression are positive integers .
The title ensures that the expression is in the intermediate calculation process and the result , Not more than 231−1.
The division in the title points to 0 integer , That is, for greater than 0 The result is rounded down , for example 5/3=1, For less than 0 The result is rounded up , for example 5/(1−4)=−1.
C++ and Java The default division in is rounding to zero ;Python Integer division in // Round down by default , therefore Python Of eval() The integer division in the function is also rounded down , You can't use... Directly in this question .
Input format
All in one line , For the given expression .
Output format
All in one line , Is the result of the expression .
Data range
The length of the expression does not exceed 105.
sample input :
(2+2)*(1+1)
sample output :
8
The key :
Two stacks are required , A stack is used to store numbers , Another is to save the operation operator
#include <iostream>
#include <cstring>
#include <algorithm>
#include <stack>
#include <unordered_map>
using namespace std;
stack<int> num; // Save digital stack
stack<char> op; // Save the operator stack
void eval()
{
auto b = num.top(); num.pop(); // Take the number
auto a = num.top(); num.pop(); // Take the number
auto c = op.top(); op.pop(); // Take operator
int x;
// Calculation
if (c == '+') x = a + b;
else if (c == '-') x = a - b;
else if (c == '*') x = a * b;
else x = a / b;
num.push(x);
}
int main()
{
unordered_map<char, int> pr{
{
'+', 1}, {
'-', 1}, {
'*', 2}, {
'/', 2}}; // If there is ^, You can add {'^',3}
string str;
cin >> str;
for (int i = 0; i < str.size(); i ++ )
{
auto c = str[i];
if (isdigit(c)) // Digital stack
{
int x = 0, j = i;
while (j < str.size() && isdigit(str[j]))
x = x * 10 + str[j ++ ] - '0';
i = j - 1;
num.push(x);
}
else if (c == '(') op.push(c);
else if (c == ')')
{
while (op.top() != '(') eval(); // Always need to calculate to the left bracket
op.pop();
}
else
{
while (op.size() && op.top() != '(' && pr[op.top()] >= pr[c]) eval(); // Priority saved in the stack >= Current priority
op.push(c);
}
}
while (op.size()) eval();
cout << num.top() << endl;
return 0;
}
边栏推荐
- go笔记(1)go语言介绍以及特点
- Record the online bug solving list (unfinished to be continued 7/4)
- 最长的可整合子数组的长度
- Taishan Office Technology Lecture: about the order of background (shading and highlighting)
- Flet tutorial 04 basic introduction to filledtonalbutton (tutorial includes source code)
- 【ISMB2022教程】图表示学习的精准医疗,哈佛大学Marinka Zitnik主讲,附87页ppt
- go语言笔记(2)go一些简单运用
- FS4061A升压8.4V充电IC芯片和FS4061B升压12.6V充电IC芯片规格书datasheet
- In operation (i.e. included in) usage of SSRs filter
- What should I do if my computer sharing printer refuses access
猜你喜欢
ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声
哈希表、哈希函数、布隆过滤器、一致性哈希
Flet教程之 06 TextButton基础入门(教程含源码)
AP8022开关电源小家电ACDC芯片离线式开关电源IC
《动手学深度学习》(三) -- 卷积神经网络 CNN
Flet教程之 05 OutlinedButton基础入门(教程含源码)
Win11怎么搜索无线显示器?Win11查找无线显示器设备的方法
面对同样复杂的测试任务为什么大老很快能梳理解决方案,阿里十年测试工程师道出其中的技巧
What if the computer page cannot be full screen? The solution of win11 page cannot be full screen
实践示例理解js强缓存协商缓存
随机推荐
九齐NY8B062D MCU规格书/datasheet
Alibaba testers use UI automated testing to achieve element positioning
Flet教程之 06 TextButton基础入门(教程含源码)
CDGA|数据治理不得不坚持的六个原则
word中插入圖片後,圖片上方有一空行,且删除後布局變亂
How does win11 search for wireless displays? Win11 method of finding wireless display device
Stack: how to realize the judgment of valid brackets?
Qt五子棋人机对战画棋子之QPainter的使用误区总结
NetCore3.1 Json web token 中间件
电脑页面不能全屏怎么办?Win11页面不能全屏的解决方法
Write it down once Net analysis of thread burst height of an industrial control data acquisition platform
idea配置标准注释
Practical examples of node strong cache and negotiation cache
Flet tutorial 07 basic introduction to popupmenubutton (tutorial includes source code)
What is involution?
Fleet tutorial 08 introduction to AppBar toolbar Basics (tutorial includes source code)
ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
看腾讯大老如何做接口自动化测试
NLP, vision, chip What is the development direction of AI? Release of the outlook report of Qingyuan Association [download attached]
Automatic generation of interface automatic test cases by actual operation