当前位置:网站首页>Stack acwing 3302. Expression evaluation
Stack acwing 3302. Expression evaluation
2022-07-27 11:19: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 1015. 摘花生
- 最长上升子序列模型 AcWing 272. 最长公共上升子序列
- 49 letter ectopic grouping and 242 effective letter ectopic words
- 记忆化搜索 AcWing 901. 滑雪
- Object array de duplication
- Wenzhou University X kangaroo cloud: how to "know well" in the construction of higher talent education
- 最长上升子序列模型 AcWing 1017. 怪盗基德的滑翔翼
- Remember not to copy your group work, students. Fortunately, you only passed two questions. Don't have an accident
- BeautifulSoup的使用
- 基于FPGA的ECG信号采集,存储以及传输系统verilog实现
猜你喜欢

记忆化搜索 AcWing 901. 滑雪

Yonbuilder enables innovation, and the "golden keyboard Award" of the fourth UFIDA developer competition is open!

Shortest moving distance and entropy of morphological complex

Use of parsel

力扣——10. 正则表达式匹配

数字三角形模型 AcWing 1018. 最低通行费

ACM warm-up Exercise 2 in 2022 summer vacation (summary)

博弈论 AcWing 893. 集合-Nim游戏

Longest ascending subsequence model acwing 1012. Sister Cities

Internal and external troubles of digital collection NFT "boring ape" bayc
随机推荐
What is the mystery of the gate of the meta universe?
Error: image clipToBoundsAndScale, argument 'input'
An article reveals the NFT strategy of traditional game manufacturers such as Ubisoft
IO流_字符流、IO流小结、IO流案例总结
[FPGA tutorial case 40] communication case 10 -- Verilog implementation of a simple OFDM system based on FPGA
FAQs of "relay chain" and "dot" in Poka ecosystem
【FPGA教程案例40】通信案例10——基于FPGA的简易OFDM系统verilog实现
Cancer DDD
The article will not keep VIP charges all the time. It will be open for a period of time
Verilog implementation of ECG signal acquisition, storage and transmission system based on FPGA
洛谷P1896 互不侵犯
Opengauss kernel analysis - statistics and row count estimation
基于FPGA的ECG信号采集,存储以及传输系统verilog实现
Use of parsel
Redis high availability principle
BeautifulSoup的使用
6 find the smallest letter larger than the target letter
Shock simulation of engine mounting system transient modal dynamic analysis and response spectrum analysis
博弈论 AcWing 892. 台阶-Nim游戏
Why is the data service API the standard configuration of the data midrange when we take the last mile of the data midrange?
