【AcWing】3302. 表达式求值

题目

在这里插入图片描述

题解

#include<iostream>
#include<unordered_map>
#include<stack>

using namespace std;

//存储运算数 运算符
stack<int> num;
stack<char> op;
//建立映射来判断运算优先级
unordered_map<char, int> cmp = {
    {'+', 1}, {'-', 1} , {'*', 2}, {'/', 2}
};
//模拟一次算术操作
void eval(void){
    int b = num.top();  num.pop();
    int a = num.top();  num.pop();
    char opr = op.top();    op.pop();

    int x;
    if(opr == '+')  x = a + b;
    else if(opr == '-') x = a - b;
    else if(opr == '*') x= a * b;
    else    x = a / b;
    num.push(x);
}

int main(){
    string str;
    cin >> str;

    for(int i = 0; i < str.size(); i++){
        char c = str[i];
        //读入运算数
        if(isdigit(c)){
            int j = i, x = 0;
            while( j < str.size() && isdigit(str[j])){
                //j++ 迭代不能忘 
                x = x *  10 + str[j ++] - '0';
            }
            num.push(x);
            //由于每轮循环有i++,我们需要倒指向最后一个数字
            i = j - 1;
        }else if( c == '(' ){
            //标记一下,括号内数据
            op.push(c);
        }else if( c == ')' ){
            //括号的优先级,先算括号
            while( op.size() && op.top() != '(' )   eval();
            //左括号可以弹出
            op.pop();
        }else{
            //得先把乘除法算了再算加减
            //这里必须得带等于号 我们这题都是正整数计算
            // 0 - 5 + 3 
            //如果不算,上式会被错误计算成 -8
            while( op.size() && cmp[op.top()] >= cmp[c])    eval();
            //压入新运算符
            op.push(c);
        }
    }
    //清理低优先级操作
    while(op.size())    eval();
    cout << num.top() << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值