当前位置:网站首页>acwing 3302. 表达式求值
acwing 3302. 表达式求值
2022-07-04 19:13:00 【_刘小雨】
给定一个表达式,其中运算符仅包含 +,-,*,/(加 减 乘 整除),可能包含括号,请你求出表达式的最终值。
注意:
数据保证给定的表达式合法。
题目保证符号 - 只作为减号出现,不会作为负号出现,例如,-1+2,(2+2)*(-(1+1)+2) 之类表达式均不会出现。
题目保证表达式中所有数字均为正整数。
题目保证表达式在中间计算过程以及结果中,均不超过 231−1。
题目中的整除是指向 0 取整,也就是说对于大于 0 的结果向下取整,例如 5/3=1,对于小于 0 的结果向上取整,例如 5/(1−4)=−1。
C++和Java中的整除默认是向零取整;Python中的整除//默认向下取整,因此Python的eval()函数中的整除也是向下取整,在本题中不能直接使用。
输入格式
共一行,为给定表达式。
输出格式
共一行,为表达式的结果。
数据范围
表达式的长度不超过 105。
输入样例:
(2+2)*(1+1)
输出样例:
8
关键:
需要用两个栈, 一个栈用于保存数字,另一个需要保存操作运算符
#include <iostream>
#include <cstring>
#include <algorithm>
#include <stack>
#include <unordered_map>
using namespace std;
stack<int> num; // 保存数字栈
stack<char> op; // 保存运算符栈
void eval()
{
auto b = num.top(); num.pop(); // 取数字
auto a = num.top(); num.pop(); // 取数字
auto c = op.top(); op.pop(); // 取运算符
int x;
// 计算
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}}; // 如果有^, 可以直接在后面加 {'^',3}
string str;
cin >> str;
for (int i = 0; i < str.size(); i ++ )
{
auto c = str[i];
if (isdigit(c)) // 数字入栈
{
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(); // 一直需要计算到左括号
op.pop();
}
else
{
while (op.size() && op.top() != '(' && pr[op.top()] >= pr[c]) eval(); // 栈里面保存的优先级 >= 当前的优先级
op.push(c);
}
}
while (op.size()) eval();
cout << num.top() << endl;
return 0;
}
边栏推荐
- 记录线上bug解决list(未完待续7/4)
- PermissionError: [Errno 13] Permission denied: ‘data.csv‘
- Automatic generation of interface automatic test cases by actual operation
- What if the win11 shared file cannot be opened? The solution of win11 shared file cannot be opened
- 15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
- 如何让你的小游戏适配不同尺寸的手机屏幕
- What is the development of block hash quiz game system? Hash quiz game system development (case mature)
- C # better operation mongodb database
- [in-depth learning] review pytoch's 19 loss functions
- 最长的可整合子数组的长度
猜你喜欢

阿里测试师用UI自动化测试实现元素定位

Flet教程之 04 FilledTonalButton基础入门(教程含源码)

Qt五子棋人机对战画棋子之QPainter的使用误区总结

针对深度学习的“失忆症”,科学家提出基于相似性加权交错学习,登上PNAS

What if the computer page cannot be full screen? The solution of win11 page cannot be full screen

What if win11u disk refuses access? An effective solution to win11u disk access denial

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

太方便了,钉钉上就可完成代码发布审批啦!

复杂因子计算优化案例:深度不平衡、买卖压力指标、波动率计算

托管式服务网络:云原生时代的应用体系架构进化
随机推荐
【ISMB2022教程】图表示学习的精准医疗,哈佛大学Marinka Zitnik主讲,附87页ppt
BFC面试简述
Qt编写物联网管理平台38-多种数据库支持
最长的可整合子数组的长度
剑指 Offer II 80-100(持续更新)
软件客户端数字签名一定要申请代码签名证书吗?
Summary of the mistakes in the use of qpainter in QT gobang man-machine game
Understand the reading, writing and creation of files in go language
栈:如何实现有效括号的判断?
哈希(Hash)竞猜游戏系统开发功能分析及源码
Function analysis and source code of hash guessing game system development
Optimization cases of complex factor calculation: deep imbalance, buying and selling pressure index, volatility calculation
Is it safe for Great Wall Securities to open an account? Stock account opening process online account opening
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
GVM使用
go笔记(3)Go语言fmt包的用法
Huawei cloud store homepage banner resource bit application
#夏日挑战赛#带你玩转HarmonyOS多端钢琴演奏
In operation (i.e. included in) usage of SSRs filter
2022 version of stronger jsonpath compatibility and performance test (snack3, fastjson2, jayway.jsonpath)