【HW-OJ 算法题】

HW-OJ 计算器(根据输入表达式计算结果)

#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <string>
#include <unordered_map>
#include <cmath>
class Solution {
private:
    std::stack<int> values;
    std::stack<char> operations;
    std::unordered_map<char,int> oper_to_level;
public:
    void init_map(){
        oper_to_level.insert({'+',1});
        oper_to_level.insert({'-',1});
        oper_to_level.insert({'*',2});
        oper_to_level.insert({'/',2});
        oper_to_level.insert({'%',2});
        oper_to_level.insert({'^',3});
    }

    void back_cal(std::stack<char>& operations, std::stack<int>& values){
        if(operations.empty() || values.size() < 2) return;
        int b = values.top(); values.pop();
        char oper = operations.top(); operations.pop();
        int a = values.top(); values.pop();
        int res = match(a,b,oper);
        values.push(res);
    }

    int match(int a, int b, char oper){
        switch(oper){
            case '+': return a + b;
            case '-': return a - b;
            case '*': return a * b;
            case '/': return a / b;
            case '%': return a % b;
            case '^': return std::pow(a,b);
            default: return 0;
        }
    }
    //删除字符串中的空格
    std::string remove_space(std::string& str){
        std::string result;
        for(char ch : str){
            if(ch != ' '){
                result += ch;
            }
        }
        return result;
    }

    int calculate(std::string s) {
        init_map();
        std::string str = remove_space(s);
        str.pop_back();
        for(int i = 0; i < str.length();){
            if(str[i] == '('){
                operations.push(str[i]);
                i++;
            }else if(str[i] == ')'){ //当出现右括号 需要将括号里面的内容新进行计算
                while(!operations.empty() && operations.top() != '('){
                    back_cal(operations,values);
                }
                operations.pop();
                i++;
            }else if(std::isdigit(str[i])){ //如果是数字 需要计算位数
                int val = 0;
                while(std::isdigit(str[i]) && i < str.length()){
                    val = val * 10 + (str[i] - '0');
                    i++;
                }
                values.push(val);
            }else{
                //当进来的是运算符的时候 只要栈顶的运算符大于等于新进来的优先级 就先进行局部运算
                while(!operations.empty()){
                    if(oper_to_level[operations.top()] >= oper_to_level[str[i]]){
                        back_cal(operations, values);
                    }else{
                        break;
                    }
                }
                operations.push(str[i]);
                i++;
            }
        }
        //对剩余的表达式进行运算
        while(!operations.empty() && values.size() >= 2){
            back_cal(operations, values);
        }
        return values.top();
    }
};

int main(){
    std::string str;
    std::getline(std::cin, str);
    Solution so;
    int res = so.calculate(str);
    std::cout << res << std::endl;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值