题解 | #表达式求值#

表达式求值

http://www.nowcoder.com/practice/9566499a2e1546c0a257e885dfdbf30d

思路:

  1. 将表达式转为逆波兰式;
  2. 计算逆波兰式的值。
    PS:默认表达式中不含空格。
#include <iostream>
#include <string>
#include <stack>
using namespace std;

// 通过一个单调栈(存储操作符)将表达式转为逆波兰式
string trans(string exp) {
    stack<char> opt_stk;
    int idx = 0, len = exp.length();
    string ans;
    char num_sign = '#';
    while (idx < len) {
        switch (exp[idx]) {
            case '(':
                opt_stk.push('(');
                idx++;
                break;
            case ')':
                while (opt_stk.top() != '(') {
                    ans += opt_stk.top();
                    opt_stk.pop();
                }
                opt_stk.pop();
                idx++;
                break;
            case '+':
            case '-':
                // 以下两种情况将负号视作符号位,不将其入栈
                // 1. 负号在第一位
                // 2. 负号前一位字符不是数字,也不是反括号
                if (exp[idx] == '-' && (idx == 0 || (idx > 0 && exp[idx-1] != ')' && !isdigit(exp[idx-1])))) {
                    num_sign = '@';
                    idx++;    
                } else {
                    while (!opt_stk.empty() && opt_stk.top() != '(') {
                    ans += opt_stk.top();
                    opt_stk.pop();
                    }
                    opt_stk.push(exp[idx++]);
                }
                break;
            case '*':
            case '/':
                while (!opt_stk.empty()) {
                    char c = opt_stk.top();
                    if (c == '*' || c == '/') {
                        ans += c;
                        opt_stk.pop();
                    } else 
                        break;
                }
                opt_stk.push(exp[idx++]);
                break;
            default:
                while (idx < len && exp[idx] <= '9' && exp[idx] >= '0') 
                    ans += exp[idx++];
                ans += num_sign;
                if (num_sign == '@') num_sign = '#';
        }
    }
    while (!opt_stk.empty()) {
        ans += opt_stk.top();
        opt_stk.pop();
    }
    return ans;
}

// 也只用一个栈,栈中存放的是操作数
double compute_value(string exp) {
    stack<double> stk;
    int idx = 0, len = exp.length();
    while (idx < len) {
        char c = exp[idx];
        if (c == '+') {
            double a = stk.top(); 
            stk.pop();
            double b = stk.top();
            stk.pop();
            stk.push(a + b);
        } else if (c == '-') {
            double a = stk.top(); 
            stk.pop();
            double b = stk.top();
            stk.pop();
            stk.push(b - a);
        } else if (c == '*') {
            double a = stk.top(); 
            stk.pop();
            double b = stk.top();
            stk.pop();
            stk.push(a * b);
        } else if (c == '/') {
            double a = stk.top(); 
            stk.pop();
            double b = stk.top();
            stk.pop();
            stk.push(b / a);
        } else {
            double num = 0;
            while (exp[idx] <= '9' && exp[idx] >= '0') 
                num = num * 10 + exp[idx++] - '0';
            if (exp[idx] == '@') num = -num;
            stk.push(num);
        }
        idx++;
    }
    return stk.top();
}

int main() {
    string exp;
    cin >> exp;
//     cout << trans(exp) << endl;
    cout << compute_value(trans(exp));

    return 0;
}
全部评论

相关推荐

2025-12-02 02:15
门头沟学院
最近菊厂陆续开了,极力劝退那些拿13级的985硕士,就13级那么点儿薪资,一线城市每个月到手1.8/7/6w,租房2k还是破烂,吃饭2k还是预制菜,买个1k衣服都是聚酯纤维破塑料,稍微出去浪一浪,能留1w就是万岁,要是再有个啥都想买的对象,一线工作一年难存10w。隔壁工地混泥土,钳工,焊工一天800+,还包吃包住。读书18年到985硕士出来就为了进厂螺丝工?还不如从8岁童工开始干活,别人读书完了你工龄18+,混不上领导也是个小头头了。当然专科进来正式工,od都行,一般本科进来13级也OK,毕竟22岁年纪摆在那个地方还不需要太花钱。读硕博的基本26岁,工作两年就要结婚的,兜里没几个崽,连彩礼都要信用贷。菊厂离职的不少,毕竟正常没人受得了9116(梗:再来一次911刷6)。为啥这时候劝?因为刚下班,因为国考刚完,省考下周,就是可惜选调只有当年应届能报。现在回想能拍断大腿。应届生真实好身份,错过这一次,选调,考公,考编,当老师,进医院,研究所,高校,央国企,基本都无缘了,就连报名资格都被剥夺了,可谓是被党和国家遗弃的废材,统称“社会上的”,扔到社会去流浪,被用坏了就扔医院,长期超负载使用,零件修不好基本可以扔火里回炉重造了。体制内奉行找体制内的,都是党和国家选的人才,智力不差,样貌不丑,身材端正,收入稳定,安居房政策福利待遇也OK。因公出行都是报销,周末顺带“游山玩水“,这种体制内单身资源但凡想找对象,去社会上随便吆喝一声都排队。观察一下,基本没什么公务员在相亲,因为早就被邻里邻居抢光了。
哈哈哈,你是老六:就这不去的人大把人干呢,现在不缺人干活,你不干大把干呢,还有那个说农民工赚钱的,那个800+我估计肯定也就那一段时间,哪有这么赚钱,还是一句话,要想存下钱必须花销极低,能省的就不花钱,工资要高点
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务