高精度加减乘除

为增进鲁棒代码能力和提高代码效率(卡常)能力,重构高精度模板

#include
using namespace std;
 
struct Bignum
{
    #define LL long long
    #define siz 1005
    #define bit 1
    #define bitsiz 10
    LL s[siz],l,neg;
    Bignum(LL a=0)
    {
        neg = (a < 0);
        for(l=0;a;) s[l++] = a % bitsiz , a /= bitsiz;
        s[l] = 0;
    }
    void cin()
    {
        char ch;neg = l = 0;
        for(;!isdigit(ch=getchar());) if(ch == '-') neg = 1;
        for(s[l++]=ch-'0';isdigit(ch=getchar());s[l++]=ch-'0');
        for(int i=0;i=0;i--) printf("%d",s[i]);
    }
    Bignum sum(const Bignum &B)const
    {
        Bignum ret(0);
        ret.l=max(l,B.l);
        int tmp;
        for(int i=0;i 9)
                ret.s[i+1] += (tmp=ret.s[i] / 10),
                ret.s[i] -= tmp * 10;
        }
        for(;ret.s[ret.l];ret.l++)
        {
            ret.s[ret.l+1]=0;
            if(ret.s[ret.l]>9)
                ret.s[ret.l+1] += (tmp=ret.s[ret.l] / 10),
                ret.s[ret.l] -= tmp * 10;
        }
        return ret;
    }
    Bignum sub(const Bignum &B)const  // require abs(*this) >= abs(B)
    {
        Bignum ret(0);
        ret.l=max(l,B.l);
        int tmp;
        for(int i=0;i=0 && !ret.s[ret.l];ret.l--);
        ret.l++;
        return ret;
    }
    bool absles(const Bignum &B)const
    {
        if(l!=B.l) return l < B.l;
        for(int i=l-1;i>=0;i--) 
            if(s[i]!=B.s[i])
                return s[i] 9)
                ret.s[i+1] += (tmp = ret.s[i] / 10),
                ret.s[i] -= tmp * 10;
        for(;ret.s[ret.l];ret.l++)
            if(ret.s[ret.l] > 9)
                ret.s[ret.l+1] += (tmp = ret.s[ret.l] / 10),
                ret.s[ret.l] -= tmp * 10;
        for(;ret.l>=0 && !ret.s[ret.l];ret.l--);
        ret.l++;
        return ret;
    }
    pair operator /(const int &B)const
    {
        Bignum ret=*this;
        for(int i=ret.l-1;i>0;i--)
        {
            ret.s[i-1] += 10ll * (ret.s[i] % B);
            ret.s[i] /= B;
        }
        int res = ret.s[0] % B;
        ret.s[0]/=B;
        for(;ret.l >= 0 && !ret.s[ret.l];ret.l--);
        ret.l++;
        return make_pair(ret,res);
    }
}A;
int B;
int main()
{
    A.cin(),scanf("%d",&B);
    pairtmp = A/B;
    tmp.first.cout();
    printf("\n");
    printf("%d\n",tmp.second);
}

你可能感兴趣的:(模板)