大數高精度運算(模板)

來源:互聯網
上載者:User

標籤:高精度

前言:高精度運算,是指參與運算的數(加數,減數,因子……)範圍大大超出了標準資料類型(整型,實型)能表示的範圍的運算。

模板:包括大數加減乘除,大數與int數的乘法,模板可以不斷擴充。

代碼:

/*全部親測可用*/const int ten[4]= {1,10,100,1000};const int maxl = 300;struct BigNumber{    int d[maxl];    char s[maxl];    BigNumber(const char s[])    {        int len=strlen(s);        d[0]=(len-1)/4+1;        int i,j,k;        for(int i=1; i<maxl; i++)            d[i]=0;        for(int i=len-1; i>=0; i--)        {            j=(len-i-1)/4+1;            k=(len-i-1)%4;            d[j]+=ten[k]*(s[i]-'0');        }        while(d[0]>1&&d[d[0]]==0)            d[0]--;    }    BigNumber()    {        *this=BigNumber("0");    }    BigNumber(int x)    {        for (int i=0; i<maxl; i++) d[i]=0;        if (!x) d[0]=1;        while(x)        {            d[++d[0]]=x%10000;            x/=10000;        }    }    BigNumber(long long x)    {        for (int i=0; i<maxl; i++) d[i]=0;        if (!x) d[0]=1;        while(x)        {            d[++d[0]]=x%10000;            x/=10000;        }    }    void print()    {        int len=d[0];        printf("%d",d[d[0]]);        for(int i=len-1; i>=1; i--)        {            if(d[i]>=1000)                printf("%d",d[i]);            else if(d[i]>=100)                printf("0%d",d[i]);            else if(d[i]>=10)                printf("00%d",d[i]);            else printf("000%d",d[i]);        }        printf("\n");    }    void toString()    {        int top=0;        int i,j,temp;        for(i=3; i>=1; i--)            if(d[d[0]]>=ten[i])                break;        temp=d[d[0]];        for(j=i; j>=0; j--)        {            s[top++]=(char)(temp/ten[j]+'0');            temp%=ten[j];        }        for(i=d[0]-1; i>0; i--)        {            temp=d[i];            for(j=3; j>=0; j--)            {                s[top++]=(char)(temp/ten[j]+'0');                temp%=ten[j];            }        }    }} zero=BigNumber(),d,temp,mid1[15],a[3005];bool operator < (const BigNumber &a,const BigNumber &b){    if(a.d[0]!=b.d[0])        return a.d[0]<b.d[0];    int i;    for(i=a.d[0]; i>0; i--)        if(a.d[i]!=b.d[i])            return a.d[i]<b.d[i];    return false;}bool operator > (const BigNumber &a,const BigNumber &b){    if(b.d[0]!=a.d[0])        return b.d[0]<a.d[0];    int i;    for(i=b.d[0]; i>0; i--)        if(a.d[i]!=b.d[i])            return b.d[i]<a.d[i];    return false;}bool operator ==(const BigNumber &a,const BigNumber &b){    int i;    if(a.d[0]!=b.d[0])        return false;    for(i=1; i<=a.d[0]; i++)        if(a.d[i]!=b.d[i])            return false;    return true;}bool operator <= (const BigNumber &a,const BigNumber &b){    return a<b||a==b;}bool operator >= (const BigNumber &a,const BigNumber &b){    return a>b||a==b;}BigNumber operator +(const BigNumber &a,const BigNumber &b){    BigNumber c;    c.d[0]=max(a.d[0],b.d[0]);    int i,x=0;    for(i=1; i<=c.d[0]; i++)    {        x=a.d[i]+b.d[i]+x;        c.d[i]=x%10000;        x/=10000;    }    while(x!=0)    {        c.d[++c.d[0]]=x%10000;        x/=10000;    }    return c;}BigNumber operator -(const BigNumber &a,const BigNumber &b){    BigNumber c;    c.d[0]=a.d[0];    int i,x=0;    for(i=1; i<=c.d[0]; i++)    {        x=10000+a.d[i]-b.d[i]+x;        c.d[i]=x%10000;        x=x/10000-1;    }    while((c.d[0]>1)&&(c.d[c.d[0]]==0))        c.d[0]--;    return c;}BigNumber operator *(const BigNumber &a,const BigNumber &b){    BigNumber c;    c.d[0]=a.d[0]+b.d[0];    int i,j,x;    for(i=1; i<=a.d[0]; i++)    {        x=0;        for(int j=1; j<=b.d[0]; j++)        {            x=a.d[i]*b.d[j]+x+c.d[i+j-1];            c.d[i+j-1]=x%10000;            x/=10000;        }        c.d[i+b.d[0]]=x;    }    while((c.d[0]>1)&&(c.d[c.d[0]]==0))        --c.d[0];    return c;}bool smaller(const BigNumber &a,const BigNumber &b,int delta){    if(a.d[0]+delta!=b.d[0])        return a.d[0]+delta<b.d[0];    int i;    for(i=a.d[0]; i>0; i--)        if(a.d[i]!=b.d[i+delta])            return a.d[i]<b.d[i+delta];    return true;}void Minus (BigNumber &a,const BigNumber &b,int delta){    int i,x=0;    for(i=1; i<=a.d[0]-delta; i++)    {        x=10000+a.d[i+delta]-b.d[i]+x;        a.d[i+delta]=x%10000;        x=x/10000-1;    }    while((a.d[0]>1)&&(a.d[a.d[0]]==0))        a.d[0]--;}BigNumber operator *(const BigNumber &a,const int &k){    BigNumber c;    c.d[0]=a.d[0];    int i,x=0;    for(i=1; i<=a.d[0]; i++)    {        x=a.d[i]*k+x;        c.d[i]=x%10000;        x/=10000;    }    while(x>0)    {        c.d[++c.d[0]]=x%10000;        x/=10000;    }    while((c.d[0]>1)&&(c.d[c.d[0]]==0))        c.d[0]--;    return c;}BigNumber operator /(const BigNumber &a,const BigNumber &b){    BigNumber c;    d=a;    int i,j,temp;    mid1[0]=b;    for(int i=1; i<=13; i++)        mid1[i]=mid1[i-1]*2;    for(i=a.d[0]-b.d[0]; i>=0; i--)    {        temp=8192;        for(j=13; j>=0; j--)        {            if(smaller(mid1[j],d,i))            {                Minus(d,mid1[j],i);                c.d[i+1]+=temp;            }            temp/=2;        }    }    c.d[0]=max(1,a.d[0]-b.d[0]+1);    while((c.d[0]>1)&&(c.d[c.d[0]]==0))        c.d[0]--;    return c;}BigNumber operator %(const BigNumber &a,const BigNumber &b){    BigNumber c=a/b;    return a-b*c;}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.