压位高精度模板

不走程序,直接上板子。
第一个板子压四位,支持带符号的加减乘。
第二个板子压九位,支持不带符号的四则运算和取模。
都封装了。


#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
struct intX
{
    static const int M=600;
    int xb[M+5];
    intX& operator=(const char* c)
    {
        memset(xb,0,sizeof(xb));
        int n=strlen(c),j=1,k=1;
        for(int i=1;i<n;i++,k*=10)
        {
            if(k==10000) j++,k=1;
            xb[j]+=k*(c[n-i]-'0');
        }
        xb[0]=j,xb[M]=c[0]-'0';
        return *this;
    }
    intX& operator=(int a)
    {
        char s[25];
        sprintf(s+1,"%d",a);
        if(a<0) s[0]='1'; else s[0]='0';
        return *this=s;
    }
    intX() {memset(xb,0,sizeof(xb)); xb[0]=1;}
    intX(int n) {*this=n;}
    bool operator>(const intX &b) const
    {
        if(xb[M]!=b.xb[M]) return xb[M]<b.xb[M];
        if(xb[0]!=b.xb[0]) return xb[M]?xb[0]<b.xb[0]:xb[0]>b.xb[0];
        for(int i=xb[0];i>0;i--)
            if(xb[i]!=b.xb[i]) return xb[M]?xb[i]<b.xb[i]:xb[i]>b.xb[i];
        return false;
    }
    bool operator<(const intX &b) const {return b>*this;}
    bool operator<=(const intX &b) const {return !(*this>b);}
    bool operator>=(const intX &b) const {return !(b>*this);}
    bool operator!=(const intX &b) const {return b>*this||*this>b;}
    bool operator==(const intX &b) const {return !(b>*this)&&!(*this>b);}
    intX operator+(const intX &b) const
    {
        intX c,d=b,e=*this;
        if(xb[M]^b.xb[M])
        {
            e.xb[M]=d.xb[M]=0;
            c=e>d?e-d:d-e;
            if((e>d&&xb[M]) || (e<d&&b.xb[M])) c.xb[M]=1;
            return c;
        }
        c.xb[0]=max(xb[0],b.xb[0]), c.xb[M]=xb[M];
        for(int i=1;i<=c.xb[0];i++)
        {
            c.xb[i]+=xb[i]+b.xb[i];
            if(c.xb[i]>9999) c.xb[i]-=10000, c.xb[i+1]++;
        }
        if(c.xb[c.xb[0]+1]>0) c.xb[0]++;
        return c;
    }
    intX operator-(const intX &b) const
    {
        intX c,d=b,e=*this;
        if(e<0 && d<0) {d.xb[M]=e.xb[M]=0; return d-e;}
        else if(d<0) {d.xb[M]=0; return d+e;}
        else if(e<0) {d.xb[M]=1; return d+e;}
        else if(e<d) {c=d-e; c.xb[M]=1; return c;}
        c.xb[0]=xb[0];
        for(int i=1;i<=c.xb[0];i++)
        {
            c.xb[i]+=xb[i]-b.xb[i];
            if(c.xb[i]<0) c.xb[i]+=10000, c.xb[i+1]--;
        }
        while(!c.xb[c.xb[0]]&&c.xb[0]>1) c.xb[0]--;
        return c;
    }
    intX& operator+=(const intX &b) {return *this=*this+b;}
    intX& operator-=(const intX &b) {return *this=*this-b;}
    intX operator*(const intX &b) const
    {
        intX c;
        c.xb[0]=xb[0]+b.xb[0]+1;
        for(int i=1;i<=xb[0];i++)
            for(int j=1;j<=b.xb[0];j++)
                c.xb[i+j-1]+=xb[i]*b.xb[j],
                c.xb[i+j]+=c.xb[i+j-1]/10000,
                c.xb[i+j-1]%=10000;
        while(!c.xb[c.xb[0]] && c.xb[0]>1) c.xb[0]--;
        if(*this!=0 && b!=0) c.xb[M]=xb[M]^b.xb[M];
        return c;
    }
    intX operator*=(const intX &b) {return *this=*this*b;}
    void readX(intX &b)
    {
        char s[2*M]; scanf("%s",s+1);
        if(s[1]=='-') s[1]='0',s[0]='1'; else s[0]='0';
        b=s;
    }
    void writeX()
    {
        if(xb[M]) printf("-");
        printf("%d",xb[xb[0]]);
        for(int i=xb[0]-1;i>0;i--)
            printf("%04d",xb[i]);
    }
};
int main()
{
    intX a,b;
    a.readX(a),b.readX(b);
    (a+b).writeX();
    return 0;
}

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int LEN=10000+1,BIT=9;
struct intX 
{
    static const int MOD=1000000000;
    long long s[LEN];
    bool flag;
    intX() {memset(s,0,sizeof(s)); s[0]=flag=1;}
    intX& operator=(const char *num)
    {
        int l=strlen(num);
        memset(s,0,sizeof(s));
        for(int i=l-1;i>=0;i-=BIT)
        {
            ++s[0];
            long long w=1;
            for(int j=i;j>i-BIT && j>=0;j--) 
                s[s[0]]+=(num[j]^48)*w, w=(w<<1)+(w<<3);
        }
        return *this;
    }
    intX& operator=(int num) 
    {
        char a[LEN];
        sprintf(a,"%d",num);
        return *this=a;
    }
    intX(int n) {*this = n;}
    intX(const char *n) {*this = n;}
    bool operator>(const intX &a) const
    {
        if(s[0]!=a.s[0]) return s[0]>a.s[0];
        int up=max(s[0],a.s[0]);
        for(int i=0;i<up;i++)
            if(s[up-i]!=a.s[up-i]) return s[up-i]>a.s[up-i];
        return false;
    }
    bool operator<(const intX &a) const {return a>*this;}
    bool operator<=(const intX &a) const {return !(*this>a);}
    bool operator>=(const intX &a) const {return !(a>*this);}
    bool operator!=(const intX &a) const {return a>*this||*this>a;}
    bool operator==(const intX &a) const {return !(a>*this)&&!(*this>a);}
    intX operator+(const intX &a) const
    {
        intX c;
        int x=0;
        c.s[0]=max(a.s[0],s[0])+1;
        for(int i=1;i<=c.s[0];i++) 
            c.s[i]=a.s[i]+s[i]+x,
            x=c.s[i]/MOD,
            c.s[i]%=MOD;
        while(c.s[c.s[0]]==0 && c.s[0]>1) c.s[0]--;
        return c;
    }
    intX& operator+=(const intX &a) {return *this=*this+a;}
    intX operator-(const intX &a) const
    {
        intX c;
        c.s[0]=max(a.s[0],s[0])+1;
        if(*this<a) c.flag=false;
        for(int i=1;i<=c.s[0];i++) 
        {
            if(c.flag) c.s[i]+=s[i]-a.s[i];
            else c.s[i]+=a.s[i]-s[i];
            if(c.s[i]<0) c.s[i]+=MOD, c.s[i+1]--;
        }
        while(c.s[c.s[0]]==0 && c.s[0]>1) c.s[0]--;
        return c;
    }
    intX& operator-=(const intX &a) {return *this=*this-a;}
    intX operator*(const intX &a) const
    {
        intX c;
        c.s[0]=s[0]+a.s[0];
        for(int i=1;i<=s[0];i++)
        {
            int x=0;
            for(int j=1;j<=a.s[0];j++) 
                c.s[i+j-1]+=s[i]*a.s[j]+x,
                x=c.s[i+j-1]/MOD,
                c.s[i+j-1]%=MOD;
            c.s[i+a.s[0]]=x;
        }
        while(c.s[c.s[0]]>0) c.s[0]++;
        while(c.s[c.s[0]]==0 && c.s[0]>1) c.s[0]--;
        return c;
    }
    intX& operator*=(const intX &a) {return *this=*this*a;}
    intX operator<<(const int &num)
    {
        s[0]++;
        for(int i=1;i<=s[0];i++) 
        {
            s[i]<<=num;
            if(s[i-1]>=MOD) s[i-1]-=MOD, ++s[i];
        }
        while(s[s[0]]==0 && s[0]>1) s[0]--;
        return *this;
    }
    intX operator>>(const int &num) 
    {
        for(int i=s[0];i>=1;i--) 
        {
            if((s[i]&1) && i>1) s[i-1]+=MOD;
            s[i]>>=num;
        }
        while(s[s[0]]==0 && s[0]>1) s[0]--;
        return *this;
    }
    intX operator/(const intX &k) const
    {
        intX c=*this,tmp,lt,a;
        a=k;
        tmp.s[1]=1;
        while(c>=a) a=a<<1,tmp=tmp<<1;
        while(tmp.s[0]>1 || tmp.s[1]) 
        {
            if(c>=a) c-=a, lt+=tmp;
            a=a>>1, tmp=tmp>>1;
        }
        c=lt;
        while(c.s[c.s[0]]==0 && c.s[0]>1) c.s[0]--;
        if(c.s[0]<1) c.s[c.s[0]=1]=0;
        return c;
    }
    intX& operator/=(const intX &a) {return *this=*this/a;}
    intX operator%(const intX &a) const {return *this-*this/a*a;}
    intX& operator%=(const intX &a) {return *this=*this%a;}
};
ostream& operator<<(ostream &out,const intX &a) 
{
    if(!a.flag) putchar('-');
    printf("%lld",a.s[a.s[0]]);
    for(int i=a.s[0]-1;i>=1;i--)
        printf("%0*lld",BIT,a.s[i]);
    return out;
}
istream& operator>>(istream &in,intX &a) 
{
    char str[LEN];
    scanf("%s",str);
    a=str;
    return in;
}

转载于:https://www.cnblogs.com/wzzyr24/p/11423505.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值