大整数类模板

可能还有一些bug没调出来。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <bitset>
using namespace std;
#define reg register
inline int abss(int x) {return x < 0 ? -x : x;}

struct BigInteger {
    int siz, num[10005];
    inline void Esc() {
        while(siz != 1 and num[siz] == 0) siz--;
    }
    BigInteger() {
        siz = 0;
        memset(num, 0, sizeof num);
    }
    BigInteger(int x)
    {
        siz = 0;
        while(x)
        {
            num[++siz] = x % 10;
            x /= 10;
        }
    }
    friend bool lesst (BigInteger A, BigInteger B)
    {
        if (A.siz < B.siz) return 1;
        if (A.siz > B.siz) return 0;
        for (reg int i = A.siz ; i >= 1 ; i --)
        {
            if (A.num[i] > B.num[i]) return 0;
            else if (A.num[i] < B.num[i]) return 1;
        }
    }
      friend bool operator < (BigInteger A, BigInteger B)
    {
        if (A.siz < B.siz) return 1;
        if (A.siz > B.siz) return 0;
        for (reg int i = A.siz ; i >= 1 ; i --)
            if (A.num[i] < B.num[i]) return 1;
            else if (A.num[i] > B.num[i]) return 0;
        return 1;
    }
    friend BigInteger operator + (BigInteger A, BigInteger B) //高精 + 高精
    {
        BigInteger ans;
        int s = max(A.siz, B.siz);
        ans.siz = s;
        for (reg int i = 1 ; i <= s ; i ++) ans.num[i] = A.num[i] + B.num[i];
        for (reg int i = 1 ; i <= s ; i ++)
        {
            if (ans.num[i] >= 10)
            {
                ans.num[i+1] += ans.num[i] / 10;
                ans.num[i] %= 10;
            }
        }
        if(ans.num[s+1] != 0) ans.siz++;
        ans.Esc();
        return ans;
    }
    friend BigInteger operator - (BigInteger A, BigInteger B) //高精 - 高精
    {
        BigInteger ans;
        if (lesst(A, B))
        {
            int i = 1;
            while (i <= A.siz or i <= B.siz)
            {
                if (B.num[i] < A.num[i]) B.num[i] += 10, B.num[i+1]--;
                ans.num[i] = B.num[i] - A.num[i];
                i++;
            }
            ans.siz = i;
            ans.Esc();
            ans.num[ans.siz] = -1 * ans.num[ans.siz];
        }
        else
        {
            int i = 1;
            while (i <= A.siz or i <= B.siz)
            {
                if (A.num[i] < B.num[i]) A.num[i] += 10, A.num[i+1]--;
                ans.num[i] = A.num[i] - B.num[i];
                i++;
            }
            ans.Esc();
            ans.siz = i;
        }
        return ans;
    }
    friend BigInteger operator * (BigInteger A, int x) //高精 * int
    {
        BigInteger ans;
        ans.siz = A.siz;
        for (reg int i = 1 ; i <= A.siz ; i ++) ans.num[i] = A.num[i] * x;
        int s;
        for (reg int i = 1 ; i <= A.siz or ans.num[i] ; i ++)
        {
            ans.num[i+1] += ans.num[i] / 10;
            ans.num[i] %= 10;
            s = i;
        }
        ans.siz = ans.num[s] ? s : s - 1;
        return ans;
    }
    friend BigInteger operator / (BigInteger A, int x) //高精 / int
    {
        BigInteger ans;
        ans.siz = A.siz;
        int rest = 0;
        for (reg int i = A.siz ; i >= 1 ; i --)
        {
            rest = rest * 10 + A.num[i];
            ans.num[i] = rest / x;
            rest %= x;
        }
        while(ans.siz >= 2 and ans.num[ans.siz] == 0) ans.siz--;
        return ans;
    }
    friend ostream & operator << (ostream &os, BigInteger A)
    {
        A.Esc();
        for (reg int i = A.siz ; i >= 1 ; i --)
            os << A.num[i];
        return os;
    }
}a, b;

BigInteger change(string s)
{
    BigInteger A;
    int len = s.length();
    A.siz = len;
    for (reg int i = len ; i >= 1 ; i --) A.num[i] = s[len-i] - '0';
    return A;
}

int main()
{
    string sa, sb;
    cin >> sa >> sb;
    a = change(sa), b = change(sb);
    cout << a - b << endl;
    cout << a + b << endl;
    cout << a * 2 << endl;
    cout << b / 2 << endl;
    return 0;
}

 

posted @ 2018-08-22 16:47  zZhBr  阅读(172)  评论(0编辑  收藏  举报