重载运算符(高精除低精)

#define ll long long
ll p=100000000000000;
struct ac{
    ll x[100];
    friend ac operator + (ac a,ac b){
        ac c;
        memset(c.x,0,sizeof(c.x));
        for(int i=1;i<=a.x[0] || i<=b.x[0];i++){
            c.x[i]+=a.x[i]+b.x[i];
            c.x[i+1]=c.x[i]/p;
            c.x[i]%=p;
        }
        c.x[0]=max(a.x[0],b.x[0]);
        if(c.x[c.x[0]+1]!=0){
            c.x[0]++;
        }
        return c;
    }
    friend ac operator - (ac a,ac b){
        ac c;
        memset(c.x,0,sizeof(c.x));
        for(int i=1;i<=a.x[0];i++){
            if(a.x[i]<b.x[i]){
                a.x[i]+=p;
                a.x[i+1]--;
            }
            c.x[i]=a.x[i]-b.x[i];
            cout<<c.x[i]<<endl;
        }
        c.x[0]=a.x[0];
        while(c.x[c.x[0]]==0){
            c.x[0]--;
        }
        return c;
    }
    friend ac operator * (ac a,ll b){
        ac c;
        memset(c.x,0,sizeof(c.x));
        for(int i=1;i<=a.x[0];i++){
            c.x[i]+=a.x[i]*b;
            c.x[i+1]+=c.x[i]/p;    
            c.x[i]%=p;
        }
        c.x[0]=a.x[0];
        if(c.x[c.x[0]+1]!=0){
            c.x[0]++;
        }
        return c;
    }
    friend ac operator / (ac a,ll b){
        ac c;
        memset(c.x,0,sizeof(c.x));
        for(int i=a.x[0];i>=1;i--){
            c.x[i]=a.x[i]/b;
            a.x[i-1]+=a.x[i]%b*p;
        }
        c.x[0]=a.x[0];
        while(c.x[c.x[0]]==0){
            c.x[0]--;
        }
        return c;
    }
};
void print(ac a){
    printf("%lld",a.x[a.x[0]]);
    for(int i=a.x[0]-1;i>=1;i--){
        printf("%.14lld",a.x[i]);
    }
    printf("\n");
}

 

 

posted @ 2021-06-07 15:46  fengwu2005  阅读(68)  评论(0编辑  收藏  举报