当前位置:网站首页>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;
}
边栏推荐
- Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
- go笔记(1)go语言介绍以及特点
- 电脑怎么保存网页到桌面上使用
- 针对深度学习的“失忆症”,科学家提出基于相似性加权交错学习,登上PNAS
- hash 表的概念及应用
- 强化学习-学习笔记2 | 价值学习
- Managed service network: application architecture evolution in the cloud native Era
- Hash哈希竞猜游戏系统开发如何开发丨哈希竞猜游戏系统开发(多套案例)
- Talking about cookies of client storage technology
- Six stones programming: about code, there are six triumphs
猜你喜欢

字节测试工程师十年经验直击UI 自动化测试痛点

WinCC7.5 SP1如何通过交叉索引来寻找变量及其位置?

What if the win11 shared file cannot be opened? The solution of win11 shared file cannot be opened

剑指 Offer II 80-100(持续更新)

See how Tencent does interface automation testing
![[today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born](/img/0b/73f0d98a6db813e54074abe199ed98.png)
[today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born

What if the WiFi of win11 system always drops? Solution of WiFi total drop in win11 system

Alibaba testers use UI automated testing to achieve element positioning

Win11共享文件打不开怎么办?Win11共享文件打不开的解决方法

MySQL --- 数据库查询 - 聚合函数的使用、聚合查询、分组查询
随机推荐
How to solve the problem that win11 cannot write the value to the registry key?
What if the brightness of win11 is locked? Solution to win11 brightness locking
Record the online bug solving list (unfinished to be continued 7/4)
NetCore3.1 Json web token 中间件
浏览器渲染页面过程
Win11共享文件打不开怎么办?Win11共享文件打不开的解决方法
Four traversal methods of binary tree, as well as the creation of binary tree from middle order to post order, pre order to middle order, pre order to post order, and sequence [specially created for t
See how Tencent does interface automation testing
go笔记(1)go语言介绍以及特点
【申博攻略】六.如何联系心仪的博导
E-week finance | Q1 the number of active people in the insurance industry was 86.8867 million, and the licenses of 19 Payment institutions were cancelled
[Beijing Xunwei] i.mx6ull development board porting Debian file system
Win11系统wifi总掉线怎么办?Win11系统wifi总掉线的解决方法
Alibaba testers use UI automated testing to achieve element positioning
idea恢复默认快捷键
Length of the longest integrable subarray
哈希(Hash)竞猜游戏系统开发功能分析及源码
剑指 Offer II 80-100(持续更新)
Talking about cookies of client storage technology
Integretee integrates into Moonriver through xcm, bringing enterprise class privacy solutions to its ecosystem