当前位置:网站首页>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;
}
边栏推荐
- 《动手学深度学习》(三) -- 卷积神经网络 CNN
- What ppt writing skills does the classic "pyramid principle" teach us?
- 【深度学习】一文看尽Pytorch之十九种损失函数
- Practice examples to understand JS strong cache negotiation cache
- Every time I look at the interface documents of my colleagues, I get confused and have a lot of problems...
- 电脑共享打印机拒绝访问要怎么办
- Fleet tutorial 08 introduction to AppBar toolbar Basics (tutorial includes source code)
- hash 表的概念及应用
- [ismb2022 tutorial] the picture shows the precision medicine of learning. Marinka zitnik, Harvard University, keynote speaker, with 87 ppt
- What if win11u disk refuses access? An effective solution to win11u disk access denial
猜你喜欢

电脑页面不能全屏怎么办?Win11页面不能全屏的解决方法

黄金k线图中的三角形有几种?
node强缓存和协商缓存实战示例

Flet tutorial 05 outlinedbutton basic introduction (tutorial includes source code)

面对同样复杂的测试任务为什么大老很快能梳理解决方案,阿里十年测试工程师道出其中的技巧

哈希表、哈希函数、布隆过滤器、一致性哈希

How does win11 search for wireless displays? Win11 method of finding wireless display device

LeetCode+ 81 - 85 单调栈专题

Flet教程之 05 OutlinedButton基础入门(教程含源码)

Win11共享文件打不开怎么办?Win11共享文件打不开的解决方法
随机推荐
How to adapt your games to different sizes of mobile screen
左右最值最大差问题
word中插入圖片後,圖片上方有一空行,且删除後布局變亂
LeetCode 7. 整数反转
What is the development of block hash quiz game system? Hash quiz game system development (case mature)
Is it safe for Great Wall Securities to open an account? Stock account opening process online account opening
Play the music of youth
电脑页面不能全屏怎么办?Win11页面不能全屏的解决方法
Six stones programming: about code, there are six triumphs
《动手学深度学习》(三) -- 卷积神经网络 CNN
Après l'insertion de l'image dans le mot, il y a une ligne vide au - dessus de l'image, et la disposition est désordonnée après la suppression
Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
九齐NY8B062D MCU规格书/datasheet
GVM使用
Selected review | machine learning technology for Cataract Classification / classification
NLP、视觉、芯片...AI重点方向发展几何?青源会展望报告发布[附下载]
AP8022开关电源小家电ACDC芯片离线式开关电源IC
栈:如何实现有效括号的判断?
奏响青春的乐章
阿里测试师用UI自动化测试实现元素定位