高精度模板【高精度加减乘除模带负数判定】

#include<iostream>
#include<cstdio>
#include<cstring>
const int maxn=500;
using namespace std;
struct BN
{
    int e[maxn],len;
    int syb;
    BN(){memset(e,0,sizeof e);len=1;syb=1;}
    BN operator =(const char *a)
    {
        int j=0;
        if(!isdigit(a[0])&&a[0]=='-')
            syb=-1,len=strlen(a)-1,j++;
        else syb=1,len=strlen(a);
        for(int i=1;i<=len;i++,j++)
            e[len-i+1]=a[j]-'0';
    }
    BN operator =(const int& num)
    {
        char a[maxn];
        sprintf(a,"%d",num);
        *this=a;
        return *this;
    }
    BN(const int& num){*this = num;}
    BN(const char *num){*this = num;}
    string str()
    {
        string res="";
        for(int i=1;i<=len;i++)
            res=char(e[i]+'0')+res;
        if(res=="")res="0";
        else if(syb==-1)res="-"+res;
        return res;
    }
     
    BN operator -()const{BN res=*this;res.syb=0-syb;return res;}
     
    void clean(){while(len>1&&!e[len])len--;}
     
    BN operator +(const BN &a)const 
    {
        BN res;
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>if(syb==1&&a.syb==1);
        else if(syb==-1&&a.syb==-1){res=-((-*this)+(-a));return res;}
        else if(syb==1&&a.syb==-1){res=*this-(-a);return res;}
        else if(syb==-1&&a.syb==1){res=a-(-*this);return res;}
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>res.len=0;
        int ub=max(len,a.len);
        for(int i=1,g=0;g||i<=ub;i++)
        {
            int x=g;
            if(i<=len)x+=e[i];
            if(i<=a.len)x+=a.e[i];
            res.e[++res.len]=x%10;
            g=x/10;
        }
        return res;
    }
    BN operator -(const BN &a)const
    {
        BN res;
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>if(syb==1&&a.syb==1&&*this>a);
        else if(syb==-1&&a.syb==1){res=-( (-*this) +a);return res;}
        else if(syb==1&&a.syb==-1){res=*this+(-a);return res;}
        else if(syb==-1&&a.syb==-1){res=-a-(-*this);return res;}
        else if(syb==1&&a.syb==1&&*this<a){res=-(a-*this);return res;}
<span style="white-space:pre">		</span>
        res.len=len;
        for(int i=1,g=0;i<=len;i++)
        {
            int x=e[i];
            x-=g;
            if(i<=a.len)x-=a.e[i];
            if(x>=0)g=0;
            else x+=10,g=1;
            res.e[i]=x;
        }
        res.clean();
        return res;
    }
    BN operator *(const BN &a)const
    {
        BN res;
        res.len=len+a.len;
        for(int i=1;i<=len;i++)
            for(int j=1;j<=a.len;j++)
                res.e[i+j-1]+=e[i]*a.e[j];
        for(int i=1;i<=res.len;i++)
        {
            res.e[i+1]+=res.e[i]/10;
            res.e[i]%=10;
        }
        res.clean();
        if(syb==a.syb)res.syb=1;
        else res.syb=-1;
        return res;
    }
    BN operator /(const BN &a)const
    {
        BN res,f=0;
        res.len=len;
        for(int i=len;i>=1;i--)
        {
            f=f*10+e[i];
            while(f>=a)
            {
                f=f-a;
                res.e[i]++;
            }
        }
        res.clean();
        if(syb==a.syb)res.syb=1;
        else res.syb=-1;
        return res;
    }
    BN operator %(const BN &a)const
    {
        BN res=*this/a;
        res=*this-res*a;
        return res;
    }
     
    bool operator <(const BN &a)const
    {
        if(syb!=a.syb)return syb<a.syb;
<span style="white-space:pre">		</span>else if(syb==-1) return -*this>(-a);
<span style="white-space:pre">		</span>if(len!=a.len)return len<a.len;
        for(int i=len;i>=1;i--)
            if(e[i]!=a.e[i])return e[i]<a.e[i];
        return 0;
    }
    bool operator >(const BN &a)const 
    {
        return a < *this;
    }
    bool operator >=(const BN &a)const
    {
        return !(*this<a);
    }
    bool operator <=(const BN &a)const
    {
        return !(*this>a);
    }
};
istream& operator >>(istream &in,BN &a)
{
    string s;
    in>>s;
    a=s.c_str();
    return in;
}
ostream& operator <<(ostream &out,BN &a)
{
    out<<a.str();
    return out;
}
int main()
{
    BN a,b;
    cin>>a>>b;
    BN c=a*b;
    cout<<c;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值