帮忙调一下负数(高精度 重载运算符)
  • 板块学术版
  • 楼主ElmPoplar
  • 当前回复0
  • 已保存回复0
  • 发布时间2022/7/11 08:14
  • 上次更新2023/10/27 21:09:37
查看原帖
帮忙调一下负数(高精度 重载运算符)
371524
ElmPoplar楼主2022/7/11 08:14
struct BigInt { 
    static const int MAXN = 1010;
    int num[MAXN], len;

    // 初始化 
    BigInt() {clean();}

    void clean() {
        memset(num, 0, sizeof num);
        len = 1;
    }

    // 读入输出 
    void read() {
        char str[MAXN];
        scanf("%s", str);

        len = strlen(str);
        for (int i = 1; i <= len; i ++) 
            num[i] = str[len - i] - '0';
    }

    void write() {
        for (int i = len; i >= 1; i --)
            printf("%d",num[i]);
    }

    void itoBig(int x) {
        clean();
        while(x != 0) {
            num[len ++] = x % 10;
            x /= 10;
        }
        if(len != 1) 
            len --;
    }

    // 重载运算符 
    bool operator < (const BigInt &cmp) const {
        if(len != cmp.len) 
            return len < cmp.len;
        for (int i = len; i >= 1; i --)
            if(num[i]!=cmp.num[i]) 
                return num[i]<cmp.num[i];
        return false;
    }

    bool operator > (const BigInt &cmp) const { 
        return cmp < *this; 
    }

    bool operator <= (const BigInt &cmp) const { 
        return ! (cmp < *this); 
    }

    bool operator != (const BigInt &cmp) const { 
        return cmp < *this || *this < cmp; 
    }

    bool operator == (const BigInt &cmp) const { 
        return ! (cmp < *this || *this < cmp); 
    }

    BigInt operator + (const BigInt &A) const {
        BigInt S;
        S.len = max(len, A.len);

        for (int i = 1; i <= S.len; i ++) {
            S.num[i] += num[i] + A.num[i];

            if(S.num[i] >= 10)  {
                S.num[i] -= 10;
                S.num[i + 1] ++;
            }
        }
        while(S.num[S.len + 1]) 
            S.len ++;

        return S;
    }

    BigInt operator - (const BigInt &A) const {
        BigInt S;
        S.len = max(len, A.len);

        for (int i = 1; i <= S.len; i ++) {
            S.num[i] += num[i] - A.num[i];

            if(S.num[i] < 0) {
                S.num[i] += 10;
                S.num[i + 1] --;
            }
        }
        while(! S.num[S.len] && S.len > 1) 
            S.len --;

        return S;
    }

    BigInt operator * (const BigInt &A) const {
        BigInt S;

        if((A.len == 1 && A.num[1] == 0) || (len == 1 && num[1] == 0)) 
            return S;

        S.len = A.len + len - 1;
        for (int i = 1; i <= len; i ++)
            for (int j = 1;j <= A.len; j ++) {
            S.num[i + j - 1] += num[i] * A.num[j];
            S.num[i + j] += S.num[i + j - 1] / 10;
            S.num[i + j - 1] %= 10;
        }
        while(S.num[S.len + 1]) 
            S.len ++;

        return S;
    }
    BigInt operator / (const BigInt &A) const {
        BigInt S;
        if((A.len == 1 && A.num[1] == 0) || (len == 1 && num[1] == 0)) 
            return S;

        BigInt R, N;
        S.len = 0;
        for (int i = len; i >= 1; i --) {
            N.itoBig(10);
            R = R * N;
            N.itoBig(num[i]);
            R = R + N;

            int flag = -1;
            for (int j = 1; j <= 10; j ++) {
                N.itoBig(j);
                if(N * A > R) {
                    flag = j - 1;
                    break;
                }
            }

            S.num[++ S.len] = flag;
            N.itoBig(flag);
            R = R - N * A;
        }

        for (int i = 1; i <= S.len / 2; i ++) 
            swap(S.num[i], S.num[len - i + 1]);
        while(! S.num[S.len] && S.len > 1) 
            S.len --;

        return S;
    }

    BigInt operator % (const BigInt &A) const {
        BigInt S;
        BigInt P = *this / A;
        S = *this - P * A;

        return S;
    }
};
2022/7/11 08:14
加载中...