c++高精度

2021-01-18 17:17

阅读:519

标签:case   cas   etc   color   switch   can   end   数据   ring   

#include
#includestring.h>
#define yw 10000
//yw表示压4位数 
struct node{
    int s[10001];//s存储数据 
    int len,zf;//len存储位数,zf存储正负(1为正-1为负) 
};
char dr[1000005],k1;
void print(const struct node& a){
    if(a.zf==-1)printf("-");
    printf("%d",a.s[a.len]);
    for(int i=a.len-1;i>0;i--)printf("%04d",a.s[i]);
    printf("\n");
//    printf("%d %d\n",a.zf,a.len);
    return;
}
void print1(const struct node& a){
    if(a.zf==-1)printf("-");
    printf("%d",a.s[a.len]);
    for(int i=a.len-1;i>0;i--)printf("%04d",a.s[i]);
    return;
}
bool operator const struct node& a,const struct node& b){
    if(a.zf*b.zf0)return a.zfb.zf;
    if(a.len!=b.len)return a.lenb.len;
    for(int i=a.len;i>0;i--){
        if(a.s[i]!=b.s[i])return a.s[i]b.s[i];
    }
    return 0;
}
bool operator const struct node& a,const struct node& b){
    if(a.zf*b.zf0)return a.zfb.zf;
    if(a.len!=b.len)return a.lenb.len;
    for(int i=a.len;i>0;i--){
        if(a.s[i]!=b.s[i])return a.s[i]b.s[i];
    }
    return 1;
}
bool operator > (const struct node& a,const struct node& b){
    if(a.zf*b.zf0)return a.zf>b.zf;
    if(a.len!=b.len)return a.len>b.len;
    for(int i=a.len;i>0;i--){
        if(a.s[i]!=b.s[i])return a.s[i]>b.s[i];
    }
    return 0;
}
bool operator >= (const struct node& a,const struct node& b){
    if(a.zf*b.zf0)return a.zf>b.zf;
    if(a.len!=b.len)return a.len>b.len;
    for(int i=a.len;i>0;i--){
        if(a.s[i]!=b.s[i])return a.s[i]>b.s[i];
    }
    return 1;
}
struct node fu(struct node a){
    a.zf=-a.zf;
    return a;
}
struct node operator + (struct node a,struct node b){
    struct node c;int x;
    if(a.zf*b.zf>0){
        memset(&c,0,sizeof(c));
        c.len=1;x=0;
        if(a.zf==-1&&b.zf==-1)c.zf=-1;else c.zf=1;
        while(c.lenb.len){
            c.s[c.len]=a.s[c.len]+b.s[c.len]+x;
            x=c.s[c.len]/yw;
            c.s[c.len]%=yw;
            c.len++;
        }
        c.s[c.len]=x;
        if(c.s[c.len]==0)c.len--;
        return c;
    }else{
        if(a.zf==-1){c=a;a=b;b=c;}
        if(b.zf==-1){b=fu(b);}
        if(a0,sizeof(c));c.zf=-1;}
        else{memset(&c,0,sizeof(c));c.zf=1;}
        int i=1;
        while(ib.len){
            if(a.s[i]b.s[i]){
                a.s[i]+=yw;
                a.s[i+1]--;
            }
            c.s[i]=a.s[i]-b.s[i];
            i++;
        }
        c.len=i;
        while(c.s[c.len]==0&&c.len>1)c.len--;
        if(c.len==1&&c.s[1]==0)c.zf=1;//默认0为正 
        return c;
    }
}
struct node operator -(const struct node& a,const struct node& b){
    return a+fu(b);
}
struct node read(){
    struct node c;
    memset(&c,0,sizeof(c));
    scanf("%s",dr);
    int lendr=strlen(dr);
    if(dr[0]!=-){
        c.zf=1;
        c.len=lendr/4;
        for(int i=1;i)
            c.s[i]=(dr[lendr-4*i]^48)*1000+(dr[lendr-4*i+1]^48)*100+(dr[lendr-4*i+2]^48)*10+(dr[lendr-4*i+3]^48);
        if(lendr%4!=0){
            c.len++;
            int lendr1=lendr%4;
            c.s[c.len]=0;
            for(int i=0;i10+(dr[i]^48);
            //while((--lendr1)>=0)c.s[c.len]=c.s[c.len]*10+(dr[lendr1]^48);
        }
    }else{
        c.zf=-1; 
        c.len=(lendr-1)/4;
        for(int i=1;i)
            c.s[i]=(dr[lendr-4*i]^48)*1000+(dr[lendr-4*i+1]^48)*100+(dr[lendr-4*i+2]^48)*10+(dr[lendr-4*i+3]^48);
        if((lendr-1)%4!=0){
            c.len++;
            int lendr1=(lendr-1)%4+1;
            c.s[c.len]=0;
            for(int i=1;i10+(dr[i]^48);
            //-while((--lendr1)>0)c.s[c.len]=c.s[c.len]*10+(dr[lendr1]^48);
        }
    }
    return c;
}
struct node operator * (const struct node& a,const struct node& b){
    struct node c;int x;
    memset(&c,0,sizeof(c));
    c.zf=a.zf*b.zf;
    for(int i=1;i){
        x=0;
        for(int j=1;j){
            c.s[i+j-1]=a.s[i]*b.s[j]+x+c.s[i+j-1];
            x=c.s[i+j-1]/yw;
            c.s[i+j-1]%=yw;
        }
        c.s[i+b.len]=x;
    }
    c.len=a.len+b.len;
    while(c.s[c.len]==0&&c.len>1)c.len--;
    if(c.len==1&&c.s[1]==0)c.zf=1;
    return c;
}
struct node operator / (const struct node& a,const int& b){
    struct node c;
    if(b==0)throw(-1);
    if(a.zf*b>=0)c.zf=1;else c.zf=-1;
    int x=0;
    for(int i=a.len;i>0;i--){
        c.s[i]=(x*yw+a.s[i])/b;
        x=(x*yw+a.s[i])%b;
    }
    c.len=a.len;
    while(c.s[c.len]==0&&c.len>1)c.len--;
    if(c.len==1&&c.s[1]==0)c.zf=1;
    return c;
}
struct node chu(struct node a,int b,int *d){//返回d为模数 
    struct node c;
    if(b==0)throw(-1);
    if(a.zf*b>=0)c.zf=1;else c.zf=-1;
    if(b0)b=-b; 
    int x=0;
    for(int i=a.len;i>0;i--){
        c.s[i]=(x*yw+a.s[i])/b;
        x=(x*yw+a.s[i])%b;
    }
    c.len=a.len;
    while(c.s[c.len]==0&&c.len>1)c.len--;
    if(c.len==1&&c.s[1]==0)c.zf=1;
    *d=x;
    return c;
}
struct node numcpy(struct node p,int det){//高精除高精要用 
    struct node q;
    memset(&q,0,sizeof(q));
    for(int i=1;i1]=p.s[i];
    q.zf=1;
    q.len=p.len+det-1;
    return q;
} 
struct node operator / (struct node a,struct node b){
    int i;
    struct node tmp,c;
    if(a.zf*b.zf>=0)c.zf=1;else c.zf=-1;
    c.len=a.len-b.len+1;
    for(i=c.len;i>0;i--){
        
        tmp=numcpy(b,i);
        while(a>=tmp){c.s[i]++;a=a-tmp;}
    }
    while(c.len>0&&c.s[c.len]==0)c.len--;
    return c;
}
struct node ksm(struct node m,struct node n){
    struct node ans={{0,1},1,1};
    while(n.len>1||n.s[1]!=0){
        if(n.s[1]&1)ans=ans*m;
        m=m*m;
        n=n/2;
    }
    return ans;
}
int main(){
    struct node k,s,ans;
    k=read();
    char k1;
    getchar();
    scanf("%c",&k1);
    s=read();
//    print(k);printf("%c\n",k1);print(s);
    switch(k1){
        case +:ans=k+s;break;
        case -:ans=k-s;break;
        case *:ans=k*s;break;
        case /:ans=k/s;break;
        case ^:ans=ksm(k,s);break;
        default:break;
    }
    print(ans);
    return 0;
}

 

c++高精度

标签:case   cas   etc   color   switch   can   end   数据   ring   

原文地址:https://www.cnblogs.com/sy666/p/12902244.html


评论


亲,登录后才可以留言!