BM49 表达式求值

描述

请写一个整数计算器,支持加减乘三种运算和括号。

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     * 返回表达式的值
     * @param s string字符串 待计算的表达式
     * @return int整型
     */
    int solve(string s) {
        // write code here
        stack<char> sign;
        stack<int> num;
        for(int i = 0;i<s.length();i++){
            if(s[i] >= '0' && s[i] <= '9'){
                int rear = i;
                while((rear+1)<s.length() && s[rear+1] >= '0' && s[rear+1] <= '9'){
                    rear++;
                }
                int n = 0;
                for(int j=rear;j>=i;j--){
                    n += (s[j]-'0')*pow(10,(rear-j));
                }
                i = rear;
                num.push(n);
            }
            else if(sign.size() == 0 || s[i] == '*'|| s[i] == '('){
                sign.push(s[i]);
            }
            else if(s[i] == '+' || s[i] == '-'){
                while(1){
                    if(sign.size() == 0){
                        break;
                    }
                    if(sign.top() == '*'){
                        sign.pop();
                        int n2 = num.top();
                        num.pop();
                        int n1 = num.top();
                        num.pop();
                        num.push(n1*n2);
                    }
                    else if(sign.top() == '+'){
                        sign.pop();
                        int n2 = num.top();
                        num.pop();
                        int n1 = num.top();
                        num.pop();
                        num.push(n1+n2);
                    }
                    else if(sign.top() == '-'){
                        sign.pop();
                        int n2 = num.top();
                        num.pop();
                        int n1 = num.top();
                        num.pop();
                        num.push(n1-n2);
                    }
                    else{
                        break;
                    }
                }
                sign.push(s[i]);
            }
            else if(s[i] == ')'){
                while(1){
                    if(sign.size() == 0){
                        break;
                    }
                    else if(sign.top() == '('){
                        sign.pop();
                        break;
                    }
                    else if(sign.top() == '+'){
                        sign.pop();
                        int n2 = num.top();
                        num.pop();
                        int n1 = num.top();
                        num.pop();
                        num.push(n1+n2);
                    }
                    else if(sign.top() == '-'){
                        sign.pop();
                        int n2 = num.top();
                        num.pop();
                        int n1 = num.top();
                        num.pop();
                        num.push(n1-n2);
                    }
                    else if(sign.top() == '*'){
                        sign.pop();
                        int n2 = num.top();
                        num.pop();
                        int n1 = num.top();
                        num.pop();
                        num.push(n1*n2);
                    }
                }
            }
        }
        while(sign.size()!=0){
            if(sign.top() == '+'){
                sign.pop();
                int n2 = num.top();
                num.pop();
                int n1 = num.top();
                num.pop();
                num.push(n1+n2);
            }
            else if(sign.top() == '-'){
                sign.pop();
                int n2 = num.top();
                num.pop();
                int n1 = num.top();
                num.pop();
                num.push(n1-n2);
            }
            else if(sign.top() == '*'){
                sign.pop();
                int n2 = num.top();
                num.pop();
                int n1 = num.top();
                num.pop();
                num.push(n1*n2);
            }
        }
        return num.top();
    }
};

参考:Java数据结构:栈与综合计算器的实现(图解+完整代码)_java先进后出图解_兴趣使然黄小黄的博客-CSDN博客 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值