表达式求值(中缀)

Hello,大家好!

我是蒟亦先生!

温馨提示:请各位奆奆们洁身自好,请勿COPY!

相关题目:

表达式求值

后缀表达式求值


说实话,这道题连本人都觉得非常恶心......


思路

简单来说,这道题就是输入一个中缀表达式,让你求它的值

没别的,先将中缀转后缀,再将后缀表达式计算并输出即可

(详情参考后缀表达式求值)


代码实现

#include <bits/stdc++.h>
#define d double
using namespace std;
bool jj(char c) { return c == '+' || c == '-'; }
bool cc(char c) { return c == '*' || c == '/'; }
string shorten(string m) {
    stack<char> s;
    string b;
    int i;
    char w;
    for (i = 0; i < m.size(); i++) {
        if (isdigit(m[i]) || m[i] == '.') {
            while (isdigit(m[i]) || m[i] == '.') {
                b += m[i++];
            }
            i--;
            b += '#';
        } else if (jj(m[i])) {
            while (s.size() && (jj(s.top()) || cc(s.top()))) {
                b += s.top();
                s.pop();
            }
            s.push(m[i]);
        } else if (m[i] == ')') {
            while (s.top() != '(') {
                b += s.top();
                s.pop();
            }
            s.pop();
        } else if (cc(m[i])) {
            while (s.size() && cc(s.top())) {
                b += s.top();
                s.pop();
            }
            s.push(m[i]);
        } else {
            s.push(m[i]);
        }
    }
    while (s.size()) {
        b += s.top();
        s.pop();
    }
    return b;
}
d sj(int n) {
    d res = 1;
    for (int i = 0; i < n; i++) {
        res *= 10;
    }
    return res;
}
d sb(string s) {
    d res = 0;
    char c;
    int dec = 0;
    for (int i = 1; i <= s.size(); i++) {
        c = s[i - 1];
        if (c == '.') {
            dec = i;
        } else if (!dec) {
            res = res * 10 + c - '0';
        } else {
            res += (c - '0') / sj(i - dec);
        }
    }
    return res;
}
d nb(string s) {
    d res, t;
    stack<d> num;
    string temp;
    int i;
    for (i = 0; i < s.size(); i++) {
        temp = "";
        if (isdigit(s[i]) || s[i] == '.') {
            while (isdigit(s[i]) || s[i] == '.') {
                temp += s[i++];
            }
            num.push(sb(temp));
        } else {
            switch (s[i]) {
                case '+':
                    t = num.top();
                    num.pop();
                    t += num.top();
                    num.pop();
                    num.push(t);
                    break;
                case '-':
                    t = num.top();
                    num.pop();
                    t = num.top() - t;
                    num.pop();
                    num.push(t);
                    break;
                case '*':
                    t = num.top();
                    num.pop();
                    t *= num.top();
                    num.pop();
                    num.push(t);
                    break;
                case '/':
                    t = num.top();
                    num.pop();
                    t = num.top() / t;
                    num.pop();
                    num.push(t);
                    break;
            }
        }
    }
    res = num.top();
    return res;
}
int main() {
    string a, b;
    cin >> a;
    b = shorten(a);
    cout << nb(b);
    return 0;
}

谢谢各位奆奆!

题解制作不易,望能用心思考

如有疑问,欢迎提出问题一起讨论!

本章完

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值