题解 | Powerful Calculator

#include <bits/stdc++.h>
using namespace std;

const int LEN = 100000;

struct BigInteger {
    int digit[LEN];
    int length;
    bool isNegative;

    BigInteger(string s) {
        memset(digit, 0, sizeof(digit));
        isNegative = false;
        if (s[0] == '-') {
            isNegative = true;
            s = s.substr(1);
        }
        length = s.size();
        for (int i = 0; i < length; i++) {
            digit[i] = s[length - i - 1] - '0';
        }
    }

    BigInteger() { 
        memset(digit, 0, sizeof(digit));
        length = 0;
        isNegative = false;
    }

    BigInteger operator+(BigInteger b) {
        if (isNegative == b.isNegative) {
            BigInteger ans;
            ans.isNegative = isNegative;
            int carry = 0;
            for (int i = 0; i < length || i < b.length || carry; i++) {
                int cur = carry + digit[i] + b.digit[i];
                carry = cur / 10;
                ans.digit[ans.length++] = cur % 10;
            }
            return ans;
        }
        //符号处理
        if (isNegative) {
            BigInteger temp = *this;
            temp.isNegative = false;
            return b - temp;
        } else {
            BigInteger temp = b;
            temp.isNegative = false;
            return *this - temp;
        }
    }

    BigInteger operator-(BigInteger b) {
        if (isNegative != b.isNegative) {
            BigInteger temp = b;
            temp.isNegative = !b.isNegative;
            return *this + temp;
        }
        // 长度大小去判断同符号,因为可以转换为加,上面改写了
        bool resultIsNegative = false;
        BigInteger a = *this, bb = b;
        if (a < bb) {
            swap(a, bb);
            resultIsNegative = true;
        }

        BigInteger ans;
        int carry = 0;
        for (int i = 0; i < a.length; i++) {
            int cur = a.digit[i] - bb.digit[i] - carry;
            if (cur < 0) {
                cur += 10;
                carry = 1;
            } else {
                carry = 0;
            }
            ans.digit[ans.length++] = cur;
        }
        while (ans.length > 1 && ans.digit[ans.length - 1] == 0) {
            ans.length--;
        }
        ans.isNegative = resultIsNegative;
        return ans;
    }

    BigInteger operator*(BigInteger b) {
        BigInteger ans;
        ans.length = length + b.length;
        for (int i = 0; i < length; i++) {
            for (int j = 0; j < b.length; j++) {
                ans.digit[i + j] += digit[i] * b.digit[j];
            }
        }
        for (int i = 0; i < ans.length; i++) {
            ans.digit[i + 1] += ans.digit[i] / 10;
            ans.digit[i] %= 10;
        }
        while (ans.length > 1 && ans.digit[ans.length - 1] == 0) {
            ans.length--;
        }
	  	//乘法的,简单判断即可
        ans.isNegative = isNegative != b.isNegative;
        return ans;
    }
	//必须实现,因为前面要用
    bool operator<(const BigInteger& b) const {
        if (length != b.length) {
            return length < b.length;
        }
        for (int i = length - 1; i >= 0; i--) {
            if (digit[i] != b.digit[i]) {
                return digit[i] < b.digit[i];
            }
        }
        return false;
    }

    void print() const {
        if (isNegative && !(length == 1 && digit[0] == 0)) {
            cout << '-';
        }
        for (int i = length - 1; i >= 0; i--) {
            cout << digit[i];
        }
    }
};

int main() {
    string a, b;
    while (cin >> a >> b) {
        BigInteger x(a), y(b);
        BigInteger a1 = x + y, a2 = x - y, a3 = x * y;
        a1.print();
        cout << endl;
        a2.print();
        cout << endl;
        a3.print();
        cout << endl;
    }
}

首先这道题是包含正负号的,因此这个问题就需要特殊处理了,对于乘法直接逻辑算出来就好了,对于加减法都需要考虑正负号的处理,主要采用比较的方式,注意减法可以转换为加法,这题数据不知道有没有这种,反正我是写了,这题是入门是真的离谱,说实话不是很好写。

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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