【HW-OJ算法题】

HW-OJ 算法题 计算器

只有加减乘除 包含小数与括号

#include <iostream>
#include <stack>
#include <unordered_map>
#include <cmath>
#include <iomanip>

class Solution{
private:
    std::stack<double> vals;
    std::stack<char> opers;
    std::unordered_map<char,int> oper_to_level;
public:
    double calculate(const std::string& expression){
        vals.push(0);
        init_map();
        for(int i = 0; i < expression.size();){
            char ch = expression[i];
            if(ch == '('){
                opers.push(ch);
                i++;
            }else if(ch == ')'){
                while(!opers.empty() && opers.top() != '('){
                    back_calculate();
                }
                opers.pop();
                i++;
            }else if(std::isdigit(ch)){
                double val = 0.0;
                while(std::isdigit(expression[i]) && i < expression.size()){
                    val = val*10 + expression[i] - '0';
                    i++;
                }
                if(expression[i] == '.'){ //小数的判断逻辑
                    i += 1;
                    int index = 1;
                    while(std::isdigit(expression[i]) && i < expression.size()){
                        val = val + static_cast<double>(expression[i] - '0') / (index * 10.00);
                        i++;
                        index *= 10;
                    }
                }
                vals.push(val);
            }else{
                //判断是不是需要在vals中压入0 尤其针对(-num)这种情况
                if(ch == '-' && !opers.empty() && opers.top() == '(') {
                    vals.push(0);
                }
                while(!opers.empty()){
                    if(oper_to_level[opers.top()] >= oper_to_level[ch]){
                        back_calculate();
                    }else{
                        break;
                    }
                }
                opers.push(ch);
                i++;
            }
        }
        while(!opers.empty() && vals.size() >= 2){
            back_calculate();
        }
        return vals.top();
    }
    void back_calculate(){
        if(opers.empty() || vals.size() < 2) return;
        double b = vals.top(); vals.pop();
        double a = vals.top(); vals.pop();
        char oper = opers.top(); opers.pop();
        double res = match(a, b, oper);
        vals.push(res);
    }
    double match(double a, double b, char oper){
        switch(oper){
            case '+': return a + b;
            case '-': return a - b;
            case '*': return a * b;
            case '/': return a / b;
            default: return 0;
        }
    }
    void init_map(){
        oper_to_level.insert({'+',1});
        oper_to_level.insert({'-',1});
        oper_to_level.insert({'*',2});
        oper_to_level.insert({'/',2});
    }
};

int main()
{
    Solution solution;
    while(true){
        std::string expression;
        std::getline(std::cin, expression);
        if(expression == "0") break;
        double res = solution.calculate(expression);
        std::cout << std::fixed << std::setprecision(2) << res << std::endl;
    }
}


测试用例:
输入:
-2
5.6*(-2*(1+(-3)))
2*((4+2)*5)-7/11
1+2+3
0
输出:
-2.00
22.40
59.36
6.00

只有加减 没有括号 只有整数

#include <iostream>
#include <stack>
#include <string>

class Solution{
private:
    std::stack<int> vals;
    std::stack<char> opers;
public:
void back_cal(){
    if(opers.empty() || vals.size() < 2) return;
    int b = vals.top(); vals.pop();
    int a = vals.top(); vals.pop();
    char oper = opers.top(); opers.pop();
    int res = oper == '+' ? a + b : a - b;
    vals.push(res);
}

int calculate(std::string expression){
    for(int i = 0; i < expression.size();){
        if(std::isdigit(expression[i])){
            int val = 0;
            while(std::isdigit(expression[i]) && i < expression.size()){
                val = val * 10 + (expression[i] - '0');
                i++;
            }
            vals.push(val);
        }else{
            while(!opers.empty() && vals.size() >= 2){
                back_cal();
            }
            opers.push(expression[i]);
            i++;
        }
    }
     while(!opers.empty() && vals.size() >= 2){
        back_cal();
     }
    return vals.top();
}

std::string delSpace(std::string str){
    std::string res;
    for(char ch : str){
        if(ch != ' '){
            res += ch;
        }
    }
    return res;
}

};

int main()
{
    std::string input;
    std::getline(std::cin, input);
    Solution solution;
    std::string expression = solution.delSpace(input);
    std::cout << solution.calculate(expression) << std::endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值