当前位置:网站首页>栈 AcWing 3302. 表达式求值
栈 AcWing 3302. 表达式求值
2022-07-27 10:35:00 【T_Y_F666】
栈 AcWing 3302. 表达式求值
原题链接
算法标签
栈 表达式求值
思路
代码
#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);
// 定义运算符优先级
unordered_map<char, int> ump={
{'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}};
string s;
cin>>s;
rep(i, 0, s.size()){
// 提取字符串中数字
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]=='('){ // ( 入栈
op.push(s[i]);
}else if(s[i]==')'){ // ) 与 ( 之间数字进行运算
while(op.top()!='('){
eval();
}
// ) 出栈
op.pop();
}else{ // + - * / 运算 待入栈运算符优先级低,则先计算 后将计算结果入栈
while(op.size()&&op.top()!='('&&ump[op.top()]>=ump[s[i]]){
eval();
}
// 否则 先入栈 后计算
op.push(s[i]);
}
}
// 将所有未操作运算符 从前至后操作
while(op.size()){
eval();
}
printf("%lld", num.top());
return 0;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- Learning notes - simple server implementation
- Data assets are king. How to analyze the relationship between enterprise digital transformation and data asset management?
- ACM warm-up Exercise 2 in 2022 summer vacation (summary)
- XXX packages are looking for funding run 'NPM fund' for details solutions
- Chengying, kangaroo cloud one-stop fully automated operation and maintenance steward, is officially open source
- The largest square of leetcode (violence solving and dynamic programming solving)
- 洛谷P1441 砝码称重
- Ten year structure five year life-07 young and vigorous transformation
- ethereum rpc
- 神经网络学习笔记
猜你喜欢

Analysis of C language pointer function and function pointer

A verification test of the relationship between iteration number and entropy

The largest square of leetcode (violence solving and dynamic programming solving)

Real time development platform construction practice, in-depth release of real-time data value - 04 live broadcast review

Use of beautifulsoup

Shortest moving distance and entropy of morphological complex

Cancer DDD

图片中非0值的数量对分类的影响

如何组装一个注册中心

MySQL installation (RPM package)
随机推荐
Study notes Minio
IO流_字符流、IO流小结、IO流案例总结
Today's code farmer girl summarized her notes about NPM package management and URL module
Use of parsel
Learning notes - simple server implementation
15 design movie rental system
Review and Prospect of encrypted traffic identification based on deep learning
洛谷 P3052 [USACO12MAR]Cows in a Skyscraper G
What is the issuing price of NFT (Interpretation of NFT and establishment of NFT world outlook)
Wilderness search --- search iterations
迭代次数的差异与信息熵
Neural network learning notes
Based on the open source stream batch integrated data synchronization engine Chunjun data restore DDL parsing module actual combat sharing
Instructions for mock platform
如何组装一个注册中心
背包模型 AcWing 423. 采药
MySQL installation (RPM package)
14 check whether integers and their multiples exist
Sort th in antd table to prevent hovering color change +table hovering row color change +table header color change
FAQs of "relay chain" and "dot" in Poka ecosystem
