自己写的C++高精度模板(带运算符重载),与别人的对比

自己的:

const int LEN=100;
typedef struct HighPrecision
{
    short data[LEN],h;
    HighPrecision()
    {
        CLEAR(data);
        h=0;
    }
    HighPrecision(int n)
    {
        CLEAR(data);
        h=0;
        while(n>0) data[h++]=n%10,n/=10;
        if(!data[h]) h--;
    }
    /*HighPrecision(const HighPrecision &a)
    {
        memcpy(data,a.data,sizeof(data));
        h=a.h;
    }*/
    const HighPrecision &operator= (int n)
    {
        CLEAR(data);
        h=0;
        while(n>0) data[h++]=n%10,n/=10;
        if(!data[h]) h--;
        return *this;
    }
    const HighPrecision &operator= (const HighPrecision &a)
    {
        memcpy(data,a.data,sizeof(data));
        h=a.h;
        return *this;
    }
    bool odd()
    {
        return data[0]&1;
    }
    void output()
    {
        for(int i=h;i>=0;i--) printf("%d",data[i]);
    }
} BigInteger;

bool operator< (const BigInteger &a,const BigInteger &b)
{
    if (a.h<b.h) return 1;
    else if (a.h>b.h) return 0;
    else
    {
        int h=b.h,k=0;
        for(int i=h;i>=0;i--)
            if (a.data[i]!=b.data[i]) {k=i;break;}
        return a.data[k]<b.data[k];
    }
}

bool operator< (const BigInteger &a,int n)
{
    return a<BigInteger(n);
}

bool operator> (const BigInteger &a,const BigInteger &b)
{
    if (a.h<b.h) return 0;
    else if (a.h>b.h) return 1;
    else
    {
        int h=b.h,k=0;
        for(int i=h;i>=0;i--)
            if (a.data[i]!=b.data[i]) {k=i;break;}
        return a.data[k]>b.data[k];
    }
}

bool operator> (const BigInteger &a,int n)
{
    return a>BigInteger(n);
}

bool operator== (const BigInteger &a,const BigInteger &b)
{
    if (a.h!=b.h) return 0;
    else
    {
        int h=b.h,k=0;
        for(int i=h;i>=0;i--)
            if (a.data[i]!=b.data[i]) {k=i;break;}
        return a.data[k]==b.data[k];
    }
}

bool operator== (const BigInteger &a,int n)
{
    return a==BigInteger(n);
}

bool operator>= (const BigInteger &a,const BigInteger &b)
{
    return (a>b)||(a==b);
}

BigInteger operator+(const BigInteger &a,const BigInteger &b)
{
    BigInteger c;
    int h=max(a.h,b.h);
    for(int i=0;i<=h;i++)
    {
        c.data[i]+=a.data[i]+b.data[i];
        c.data[i+1]=c.data[i]/10;
        c.data[i]%=10;
    }
    while(c.data[c.h+1]>0) c.h++;
    return c;
}

BigInteger operator+(const BigInteger &a,int n)
{
    return a+BigInteger(n);
}

BigInteger operator+= (const BigInteger &a,const BigInteger &b)
{
    return a+b;
}

BigInteger operator-(const BigInteger &a,const BigInteger &b)
{
    BigInteger c(a);
    int h=c.h;
    for(int i=0;i<=c.h;i++)
    {
        c.data[i]-=b.data[i];
        if (c.data[i]<0)
        {
            c.data[i]+=10;
            c.data[i+1]--;
        }
    }
    while(c.data[c.h]<1) c.h--;
    return c;
}

BigInteger operator-(const BigInteger &a,int n)
{
    return a-BigInteger(n);
}

BigInteger operator-= (const BigInteger &a,const BigInteger &b)
{
    return a-b;
}

BigInteger operator* (const BigInteger &a,const BigInteger &b)
{
    BigInteger c;
    for(int i=0;i<=a.h;i++)
        for(int j=0;j<=b.h;j++)
            c.data[i+j-1]+=a.data[i]*b.data[j];
    int k=0;
    while(c.data[k++]>0)
    {
        c.data[k+1]+=c.data[k]/10;
        c.data[k]%=10;
    }
    c.h=(k-1>=0)?k-1:0;
    return c;
}

BigInteger operator*= (const BigInteger &a,const BigInteger &b)
{
    return a*b;
}

BigInteger operator/ (const BigInteger &a,const BigInteger &b)
{
    BigInteger c,tmp;
    int h=max(a.h,b.h);
    for( int i = h; i >= 0; i-- ){
        tmp*= 10;
        tmp+= a.data[i];
        while( tmp >= b ) { tmp -= b; c.data[i]++; }
    }
    c.h=a.h;
    while(c.data[c.h]<1) c.h--;
    return c;
}

BigInteger operator/ (const BigInteger &a,int n)
{
    return a/BigInteger(n);
}

BigInteger operator/= (const BigInteger &a,const BigInteger &b)
{
    return a/b;
}

BigInteger operator% (const BigInteger &a,const BigInteger &b)
{
    BigInteger c,tmp;
    int h=max(a.h,b.h);
    for( int i = h; i >= 0; i-- ){
        tmp*= 10;
        tmp+= a.data[i];
        while( tmp >= b ) { tmp -= b; c.data[i]++; }
    }
    tmp.h=a.h;
    while(tmp.data[tmp.h]<1) tmp.h--;
    return tmp;
}

BigInteger operator>>(const BigInteger &a,int n)
{
    BigInteger c(a);
    for (int i=1;i<=n;i++) c=c/2;
    return c;
}



别人的:

http://blog.csdn.net/wall_f/article/details/8373395

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值