当前位置:网站首页>Stack acwing 3302 Expression evaluation
Stack acwing 3302 Expression evaluation
2022-07-05 06:24:00 【T_ Y_ F666】
Stack AcWing 3302. Expression evaluation
Original link
AcWing 3302. Expression evaluation
Algorithm tags
Stack Expression evaluation
Ideas
Code
#include<bits/stdc++.h>
#define int long long
#define rep(i, a, b) for(int i=a;i<b;++i)
#define Rep(i, a, b) for(int i=a;i>b;--i)
using namespace std;
const int N = 10005;
stack<int> num;
stack<int> op;
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void put(int x) {
if(x<0) putchar('-'),x=-x;
if(x>=10) put(x/10);
putchar(x%10^48);
}
void eval(){
int b=num.top();
num.pop();
int a=num.top();
num.pop();
char 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);
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// Define operator priority
unordered_map<char, int> ump={
{'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}};
string s;
cin>>s;
rep(i, 0, s.size()){
// Extract the number in the string
if(isdigit(s[i])){
int j=i, x=0;
while(j<s.size()&&isdigit(s[j])){
x=x*10+s[j++]-'0';
}
i=j-1;
num.push(x);
}else if(s[i]=='('){ // ( Push
op.push(s[i]);
}else if(s[i]==')'){ // ) And ( Between numbers
while(op.top()!='('){
eval();
}
// ) Out of the stack
op.pop();
}else{ // + - * / operation The operator to be stacked has low priority , Then calculate first Then put the calculation results on the stack
while(op.size()&&op.top()!='('&&ump[op.top()]>=ump[s[i]]){
eval();
}
// otherwise First in stack Post calculation
op.push(s[i]);
}
}
// Put all non operational operators Front to back operation
while(op.size()){
eval();
}
printf("%lld", num.top());
return 0;
}
Originality is not easy.
Reprint please indicate the source
If it helps you Don't forget to praise and support
边栏推荐
- June 29, 2022 daily
- MySQL advanced part 2: the use of indexes
- 2048项目实现
- Sorting out the latest Android interview points in 2022 to help you easily win the offer - attached is the summary of Android intermediate and advanced interview questions in 2022
- 1.13 - RISC/CISC
- [wustctf2020] plain_ WP
- Install opencv -- CONDA to establish a virtual environment and add the kernel of this environment in jupyter
- Doing SQL performance optimization is really eye-catching
- 5. Oracle tablespace
- C Primer Plus Chapter 15 (bit operation)
猜你喜欢
栈 AcWing 3302. 表达式求值
[2021]IBRNet: Learning Multi-View Image-Based Rendering Qianqian
容斥原理 AcWing 890. 能被整除的数
SQL三种连接:内连接、外连接、交叉连接
LeetCode-54
Chapter 6 relational database theory
International Open Source firmware Foundation (osff) organization
博弈论 AcWing 892. 台阶-Nim游戏
NotImplementedError: Cannot convert a symbolic Tensor (yolo_boxes_0/meshgrid/Size_1:0) to a numpy ar
Matrixdb V4.5.0 was launched with a new mars2 storage engine!
随机推荐
Nested method, calculation attribute is not applicable, use methods
[moviepy] unable to find a solution for exe
MySQL advanced part 2: SQL optimization
Navicat连接Oracle数据库报错ORA-28547或ORA-03135
Single chip computer engineering experience - layered idea
2021apmcm post game Summary - edge detection
安装OpenCV--conda建立虚拟环境并在jupyter中添加此环境的kernel
[learning] database: MySQL query conditions have functions that lead to index failure. Establish functional indexes
Leetcode heap correlation
Leetcode-9: palindromes
MySQL advanced part 2: optimizing SQL steps
How to make water ripple effect? This wave of water ripple effect pulls full of retro feeling
2022-5-第四周日报
2048项目实现
Doing SQL performance optimization is really eye-catching
Client use of Argo CD installation
Error ora-28547 or ora-03135 when Navicat connects to Oracle Database
Chapter 6 relational database theory
Alibaba established the enterprise digital intelligence service company "Lingyang" to focus on enterprise digital growth
[learning] database: several cases of index failure