当前位置:网站首页>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
边栏推荐
- How to set the drop-down arrow in the spinner- How to set dropdown arrow in spinner?
- 2022/6/29-日报
- 4.Oracle-重做日志文件管理
- TCP's understanding of three handshakes and four waves
- Nested method, calculation attribute is not applicable, use methods
- Leetcode dynamic programming
- Leetcode array operation
- 1.手动创建Oracle数据库
- Leetcode-6109: number of people who know secrets
- 高斯消元 AcWing 884. 高斯消元解异或线性方程组
猜你喜欢
Redis publish subscribe command line implementation
Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135
MPLS experiment
Alibaba established the enterprise digital intelligence service company "Lingyang" to focus on enterprise digital growth
NotImplementedError: Cannot convert a symbolic Tensor (yolo_boxes_0/meshgrid/Size_1:0) to a numpy ar
7.Oracle-表结构
栈 AcWing 3302. 表达式求值
Open source storage is so popular, why do we insist on self-development?
MySQL advanced part 2: the use of indexes
Bit of MySQL_ OR、BIT_ Count function
随机推荐
C Primer Plus Chapter 15 (bit operation)
C job interview - casting and comparing - C job interview - casting and comparing
Redis-01.初识Redis
Find the combination number acwing 887 Find combination number III
MySQL advanced part 1: View
什么是套接字?Socket基本介绍
Doing SQL performance optimization is really eye-catching
3. Oracle control file management
LeetCode 1200. Minimum absolute difference
Leetcode-22: bracket generation
Leetcode-6108: decrypt messages
Chapter 6 relational database theory
How to set the drop-down arrow in the spinner- How to set dropdown arrow in spinner?
Open source storage is so popular, why do we insist on self-development?
阿里新成员「瓴羊」正式亮相,由阿里副总裁朋新宇带队,集结多个核心部门技术团队
MySQL advanced part 2: MySQL architecture
__ builtin_ Popcount() counts the number of 1s, which are commonly used in bit operations
How to make water ripple effect? This wave of water ripple effect pulls full of retro feeling
One question per day 1020 Number of enclaves
1.14 - assembly line