高精度(终极版)

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define _FOR(i, a, b) for (int i = a; i >= b; i--)

struct BigInt
{
    static const int M = 1000;
    int num[M + 10], len;

    BigInt() { clean(); }

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

    void read()
    {
        char str[M + 10];
        scanf("%s", str);
        len = strlen(str);
        FOR(i, 1, len)
        num[i] = str[len - i] - '0';
    }

    void write()
    {
        _FOR(i, len, 1)
        printf("%d", num[i]);
        puts("");
    }

    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(i, len, 1)
        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(i, 1, S.len)
        {
            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(i, 1, S.len)
        {
            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(i, 1, len)
        FOR(j, 1, A.len)
        {
            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(i, len, 1)
        {
            N.itoBig(10);
            R = R * N;
            N.itoBig(num[i]);
            R = R + N;
            int flag = -1;
            FOR(j, 1, 10)
            {
                N.itoBig(j);
                if (N * A > R)
                {
                    flag = j - 1;
                    break;
                }
            }
            S.num[++S.len] = flag;
            N.itoBig(flag);
            R = R - N * A;
        }
        FOR(i, 1, S.len / 2)
        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;
    }
};

int main()
{
    BigInt a;
    BigInt b;
    a.read();
    b.read();
    BigInt c = a * b;
    c.write();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值