当前位置:网站首页>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 
边栏推荐
- 3.Oracle-控制文件的管理
- 将webApp或者H5页面打包成App
- Leetcode backtracking method
- Doing SQL performance optimization is really eye-catching
- 时间很快,请多做有意义的事情
- One question per day 1020 Number of enclaves
- 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)
- 求组合数 AcWing 889. 满足条件的01序列
- P2575 master fight
猜你喜欢

There are three kinds of SQL connections: internal connection, external connection and cross connection

1.15 - input and output system

Redis publish subscribe command line implementation

MySQL advanced part 1: View

求组合数 AcWing 889. 满足条件的01序列

Install opencv -- CONDA to establish a virtual environment and add the kernel of this environment in jupyter

Leetcode stack related

【高德地图POI踩坑】AMap.PlaceSearch无法使用

Erreur de connexion Navicat à la base de données Oracle Ora - 28547 ou Ora - 03135

3.Oracle-控制文件的管理
随机推荐
Is it impossible for lamda to wake up?
Simple selection sort of selection sort
C Primer Plus Chapter 15 (bit operation)
How to make water ripple effect? This wave of water ripple effect pulls full of retro feeling
Leetcode-9: palindromes
P2575 master fight
MySQL怎么运行的系列(八)14张图说明白MySQL事务原子性和undo日志原理
Single chip computer engineering experience - layered idea
MySQL advanced part 1: stored procedures and functions
Network security skills competition in Secondary Vocational Schools -- a tutorial article on middleware penetration testing in Guangxi regional competition
Ffmpeg build download (including old version)
Redis-02.Redis命令
Redis publish subscribe command line implementation
Currently clicked button and current mouse coordinates in QT judgment interface
栈 AcWing 3302. 表达式求值
TCP's understanding of three handshakes and four waves
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
4. Object mapping Mapster
MySQL advanced part 1: triggers
MySQL advanced part 2: storage engine
