LeetCode/计算器

1. 基本计算器

实现加减和括号优先级,忽视空格

class Solution {
public:
    int calculate(string s) {
        stack<int> ops;//栈存储上一层
        ops.push(1);
        int sign = 1;
        int ret = 0;
        int n = s.length();
        int i = 0;
        while (i < n) {
            switch (s[i]){
            case ' ':   i++; break;
            case '+': sign = ops.top(); i++;break;
            case '-': sign = -ops.top();i++;break;
            case '(' : ops.push(sign); i++;break;
            case ')' : ops.pop();  i++;break;
            default:
                long num = 0;
                while (i < n && s[i] >= '0' && s[i] <= '9') {
                    num = num * 10 + s[i] - '0';
                    i++;
                }
                ret += sign * num;
            }
        }
        return ret;
    }
};

2. 基本计算器II

实现加减乘除

class Solution {
public:
    int calculate(string s) {
        int n = s.size(),dgt=0;
        char preSign='+';//第一个数是正的
        vector<int> v;
        for(int i=0; i<n; i++){
            if(s[i]==' ') continue;
            if(!isdigit(s[i])) preSign=s[i];
            else{
                while(isdigit(s[i])) dgt=dgt*10+int(s[i]-'0'),i++;
                if(preSign == '+') v.push_back(dgt);
                if(preSign == '-') v.push_back(-dgt);
                if(preSign == '*') v.back()*=dgt;
                if(preSign == '/') v.back()/=dgt;
                dgt=0,i--;
            }
        }
        return accumulate(v.begin(), v.end(), 0);
    }
};

3. 分数加减运算

class Solution {
public:
    string fractionAddition(string expression) {
        long denominator = 0, numerator = 1; // 分子,分母
        int index = 0, n = expression.size();
        while (index < n) {
            // 读取分子
            long denominator1 = 0, sign = 1;
            if (expression[index] == '-' || expression[index] == '+') {
                sign = expression[index] == '-' ? -1 : 1;
                index++;
            }
            while (index < n && isdigit(expression[index])) {
                denominator1 = denominator1 * 10 + expression[index] - '0';
                index++;
            }
            denominator1 = sign * denominator1;
            index++;

            // 读取分母
            long numerator1 = 0;
            while (index < n && isdigit(expression[index])) {
                numerator1 = numerator1 * 10 + expression[index] - '0';
                index++;
            }

            denominator = denominator * numerator1 + denominator1 * numerator;
            numerator *= numerator1;
            //这里也可以化简
        }
        if (denominator == 0) {
            return "0/1";
        }
         long g = gcd(abs(denominator), numerator); // 获取最大公约数
        return to_string(denominator / g) + "/" + to_string(numerator / g);
    }
};

4. 求解方程

class Solution {
public:
    string solveEquation(string equation) {
        int factor = 0, val = 0;
        int index = 0, n = equation.size(), sign1 = 1; // 等式左边默认系数为正
        //从左往右顺序遍历,index内部加,因为要数字不止一位
        while (index < n) {
            //处理等号
            if (equation[index] == '=') {
                sign1 = -1; // 等式右边默认系数为负
                index++;}
            //修正后的符号,以及待定系数(数字)
            int sign2 = sign1, number = 0;
            bool valid = false; // 记录 number 是否有效
            if (equation[index] == '-' || equation[index] == '+') { //修正符号 
                sign2 = (equation[index] == '-') ? -sign1 : sign1;
                index++;
            }//继续处理
            //读取数字(系数)
            while (index < n && isdigit(equation[index])) {
                number = number * 10 + (equation[index] - '0');
                index++;
                valid = true;//有系数
            }
            //合并同类项
            if (index < n && equation[index] == 'x') { // 变量
                factor += valid ? sign2 * number : sign2;
                index++;
                } 
            else  val += sign2 * number;//数字求和
        }

        if (factor == 0) //系数为0,有值无解,无值无穷解
        return val == 0 ? "Infinite solutions" : "No solution";
        if (val % factor != 0) return "No solution";//保证整数
        return string("x=") + to_string(-val / factor);
    }
};
posted @ 2022-07-27 20:07  失控D大白兔  阅读(39)  评论(0编辑  收藏  举报