当前位置:网站首页>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
边栏推荐
- 博弈论 AcWing 893. 集合-Nim游戏
- 11-gorm-v2-03-basic query
- Leetcode array operation
- In depth analysis of for (VaR I = 0; I < 5; i++) {settimeout (() => console.log (I), 1000)}
- [2020]GRAF: Generative Radiance Fields for 3D-Aware Image Synthesis
- 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
- TCP's understanding of three handshakes and four waves
- Redis publish subscribe command line implementation
- Leetcode-3: Longest substring without repeated characters
- International Open Source firmware Foundation (osff) organization
猜你喜欢
Single chip computer engineering experience - layered idea
栈 AcWing 3302. 表达式求值
[2021]IBRNet: Learning Multi-View Image-Based Rendering Qianqian
MySQL advanced part 2: MySQL architecture
Quickly use Amazon memorydb and build your own redis memory database
博弈论 AcWing 893. 集合-Nim游戏
LeetCode 0108. Convert an ordered array into a binary search tree - the median of the array is the root, and the left and right of the median are the left and right subtrees respectively
What is socket? Basic introduction to socket
Paper reading report
Gauss Cancellation acwing 884. Solution d'un système d'équations Xor linéaires par élimination gaussienne
随机推荐
P2575 master fight
C job interview - casting and comparing - C job interview - casting and comparing
Traditional databases are gradually "difficult to adapt", and cloud native databases stand out
Open source storage is so popular, why do we insist on self-development?
[leetcode] day94 reshape matrix
Operator priority, one catch, no doubt
容斥原理 AcWing 890. 能被整除的数
【LeetCode】Easy | 20. Valid parentheses
Gaussian elimination acwing 884 Gauss elimination for solving XOR linear equations
Quickly use Amazon memorydb and build your own redis memory database
时间很快,请多做有意义的事情
什么是套接字?Socket基本介绍
Simple selection sort of selection sort
Sqlmap tutorial (1)
[rust notes] 17 concurrent (Part 2)
Leetcode-6110: number of incremental paths in the grid graph
4. Oracle redo log file management
Series of how MySQL works (VIII) 14 figures explain the atomicity of MySQL transactions and the principle of undo logging
[rust notes] 16 input and output (Part 1)
Golang uses context gracefully