题解 | #计算表达式#写了两个钟,太垃圾了

计算表达式

https://www.nowcoder.com/practice/7b18aa6b7cc14f8eaae6b8acdebf890b

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <map>
using namespace std;

map<char, int> mymap = {
    {'+', 0}, {'-', 0}, {'/', 1}, {'*', 1}
};

float jisusan(float x, float y, char oper) {
    float res;
    switch (oper) {
        case '+':
            res = x + y;
            break;
        case '-':
            res = x - y;
            break;
        case '*':
            res = x * y;
            break;
        case '/':
            res = x / y;
            break;
    }
    return res;
}


int main() {
    stack<float> numStack;
    stack<char> operStack; //operator 运算符
    string expr; //expression 表达式
    while (getline(cin, expr)) {
        for (int i = 0; i < expr.size(); i++) {
            if (expr[i] >= '0' && expr[i] <= '9') {
                //表明这是一个数字 可能有很多位
                int j = i;
                while (j < expr.size() && expr[j] >= '0' && expr[j] <= '9') {
                    j++;
                    continue;
                }
                //此时j越界,或者j是操作符 i 到 j - 1为数字
                float num = 0;
                while (i != j) {
                    num *= 10;
                    num += (expr[i] - '0');
                    i++;
                }
                i--;
                numStack.push(num);
            } else {
                if (!operStack.empty()) {
                    while (!operStack.empty() && mymap[operStack.top()] >= mymap[expr[i]]) {
                        //栈顶的优先级 >= 当前的优先级
                        //弹出两个数和操作符计算结果然后放到numstack
                        float a1 =  numStack.top();
                        numStack.pop();
                        float a2 = numStack.top();
                        numStack.pop();
                        char c = operStack.top();
                        operStack.pop();
                        float res = jisusan(a2, a1, c);
                        numStack.push(res);


                    }
                    operStack.push(expr[i]);
                } else {
                    operStack.push(expr[i]);
                }
            }

        }
        while (!operStack.empty()) {
            float a1 =  numStack.top();
            numStack.pop();
            float a2 = numStack.top();
            numStack.pop();
            char c = operStack.top();
            operStack.pop();
            float res = jisusan(a2, a1, c);
            numStack.push(res);
        }
        float a1 =  numStack.top();
        numStack.pop();
        //printf("%f\n", a1); //这里用cout方便点
        cout << a1 << endl;



    }

}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

Peach33:项目的 “完整度”“你的思考深度” 和 “能匹配岗位的基础能力”,远比 “复杂度” 更重要。
点赞 评论 收藏
分享
10-29 16:42
门头沟学院 Java
1.今天什么国标的公司打电话约面试,还得准备ppt,好麻烦,网上查薪资一般,打算拒了,不面了2.字节又复活了,什么安全开发,也不知道怎么样,面一面试试吧,还是挺想去字节的,但好难,随缘吧所以今天没面试
嵌入式的小白:面试前可以好好准备下 1.看看你投递的岗位的岗位描述,分析下是哪个业务线,同使要罗列他们描述中提到的技术点 2.根据1中的两点准备 3.岗位描述中应该还有语言要求,这个刷刷八股,要是对自己语言能力很有把握,那就不用看这点了 4.找下你简历中项目部分,看有没有和岗位描述中技术点重合的,这种在面试提到项目时,是高概率问题 好好准备,祝你面试顺利
我的求职进度条
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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