当前位置:网站首页>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 
边栏推荐
- Sqlmap tutorial (1)
- International Open Source firmware Foundation (osff) organization
- Paper reading report
- [rust notes] 16 input and output (Part 1)
- 1.手动创建Oracle数据库
- Traversal of leetcode tree
- 11-gorm-v2-02-create data
- Suppose a bank's ATM machine, which allows users to deposit and withdraw money. Now there is 200 yuan in an account, and both user a and user B have the right to deposit and withdraw money from this a
- Leetcode-9: palindromes
- How to understand the definition of sequence limit?
猜你喜欢

Client use of Argo CD installation

高斯消元 AcWing 884. 高斯消元解异或线性方程组

栈 AcWing 3302. 表达式求值

Error ora-28547 or ora-03135 when Navicat connects to Oracle Database

ADG5412FBRUZ-RL7应用 双电源模拟开关和多路复用器IC

Liunx starts redis

1.15 - input and output system

5.Oracle-表空间

How to make water ripple effect? This wave of water ripple effect pulls full of retro feeling

Install opencv -- CONDA to establish a virtual environment and add the kernel of this environment in jupyter
随机推荐
What's wrong with this paragraph that doesn't work? (unresolved)
Leetcode dynamic programming
Leetcode divide and conquer / dichotomy
栈 AcWing 3302. 表达式求值
MySQL怎么运行的系列(八)14张图说明白MySQL事务原子性和undo日志原理
NotImplementedError: Cannot convert a symbolic Tensor (yolo_boxes_0/meshgrid/Size_1:0) to a numpy ar
5.Oracle-錶空間
11-gorm-v2-02-create data
P3265 [jloi2015] equipment purchase
Leetcode-9: palindromes
__ builtin_ Popcount() counts the number of 1s, which are commonly used in bit operations
2021apmcm post game Summary - edge detection
5. Oracle TABLESPACE
4. Object mapping Mapster
In depth analysis of for (VaR I = 0; I < 5; i++) {settimeout (() => console.log (I), 1000)}
MySQL advanced part 2: storage engine
Chart. JS - Format Y axis - chart js - Formatting Y axis
2048 project realization
Single chip computer engineering experience - layered idea
There are three kinds of SQL connections: internal connection, external connection and cross connection
