当前位置:网站首页>栈 AcWing 3302. 表达式求值
栈 AcWing 3302. 表达式求值
2022-07-05 06:16: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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- SQL三种连接:内连接、外连接、交叉连接
- Daily question 2006 Number of pairs whose absolute value of difference is k
- MySQL advanced part 1: triggers
- A reason that is easy to be ignored when the printer is offline
- Niu Mei's math problems
- The difference between CPU core and logical processor
- MySQL advanced part 2: MySQL architecture
- [rust notes] 17 concurrent (Part 2)
- Golang uses context gracefully
- Data visualization chart summary (I)
猜你喜欢

Matrixdb V4.5.0 was launched with a new mars2 storage engine!

Leetcode array operation

Appium基础 — 使用Appium的第一个Demo

MySQL advanced part 2: storage engine

阿里巴巴成立企业数智服务公司“瓴羊”,聚焦企业数字化增长

Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135

Sqlmap tutorial (1)

Quickly use Amazon memorydb and build your own redis memory database

4. Object mapping Mapster

Network security skills competition in Secondary Vocational Schools -- a tutorial article on middleware penetration testing in Guangxi regional competition
随机推荐
QT判断界面当前点击的按钮和当前鼠标坐标
Sum of three terms (construction)
[leetcode] day94 reshape matrix
js快速将json数据转换为url参数
1.13 - RISC/CISC
Appium foundation - use the first demo of appium
Leetcode-9: palindromes
数据可视化图表总结(二)
JS quickly converts JSON data into URL parameters
CPU内核和逻辑处理器的区别
1.14 - assembly line
MySQL advanced part 1: triggers
1040 Longest Symmetric String
C - XOR to all (binary topic)
New title of module a of "PanYun Cup" secondary vocational network security skills competition
leetcode-6111:螺旋矩阵 IV
Data visualization chart summary (II)
liunx启动redis
Sqlmap tutorial (1)
1.14 - 流水线
