重载高精度的所有运算

重载高精度的所有运算

这个东西的码风现在虽然看起来很奇怪,
但是一旦拖到编辑器里把括号一缩就爽的一比了

#include<bits/stdc++.h>
#define rg register int
#define maxn 1005
#define base 10000//压位用基底 
using namespace std;
struct bignum{
    int len,s[maxn];
//————————结构体初始化————————
    bignum(){
        len=1;
        memset(s,0,sizeof s);
    }
//————————高精度读、写、去零操作————————
    inline void write(char a[]){
        int lena=strlen(a);
        int k=1;
        for(rg i=0;i<lena;++i)
        {
            s[len]+=(a[lena-i-1]-'0')*k;//从字符串中取出高精数 
            k*=10;
            if(k==base)++len,k=1;
        }
    }
    inline void zero(){
        while(len>1&&s[len]==0)//若首位为零且整体不为零,进行去零 
            --len;
    }
    inline void read(){
        char a[4*maxn];
        scanf("%s",a);//以字符串形式输入 
        write(a);//进行读入操作 
    }
    inline void print(){
        zero();//先去零 
        printf("%d",s[len]);//首位单独处理 
        for(rg i=len-1;i>=1;--i)
            printf("%04d",s[i]);//其他位要用零补齐 
    }
//————————重载整形对高精赋值————————
    inline bignum operator = (const int b){
        char a[maxn*4];
        sprintf(a,"%d",b);//以字符串形式读入 
        write(a);
        return *this;
    }
//————————高精对高精的计算————————
    inline bignum operator + (bignum b)const{
        bignum c;
        c.len=max(len,b.len)+1;
        for(rg i=1;i<c.len;++i)
        {
            c.s[i]+=s[i]+b.s[i]; 
            c.s[i+1]=c.s[i]/base;
            c.s[i]%=base;
        }
        c.zero();
        return c;
    }
    inline bignum operator - (bignum b){
        bignum c;
        c.len=len;
        for(rg i=1;i<=c.len;++i)
        {
            c.s[i]=s[i]-b.s[i];
            if(c.s[i]<0)
            {
                --s[i+1];
                c.s[i]+=base;
            }
        }
        c.zero();
        return c;
    }
    inline bignum operator * (bignum b)const{
        bignum c;
        c.len=len+b.len+1;
        for(rg i=1;i<=len;++i)
        {
            rg k=0;
            for(rg j=1;j<=b.len;j++)
            {
                c.s[i+j-1]+=s[i]*b.s[j]+k;
                k=c.s[i+j-1]/base;
                c.s[i+j-1]%=base;
            }
            c.s[i+b.len]=k;
        }
        c.zero();
        return c;
    }
    inline bignum operator / (bignum b)const{
        bignum c;
        bignum temp;
        c.len=len;
        for(rg i=len;i>=1;--i)
        {
            temp=temp*base+s[i];
            int l=0,r=base,ans=0;
            while(l<=r)
            {
                int mid=(l+r)/2;
                if(b*mid<=temp)
                {
                    ans=mid;
                    l=mid+1;
                }
                else
                    r=mid-1;
            }
            temp=temp-b*ans;
            c.s[i]=ans;
        }
        c.zero();
        return c;
    }
    inline bignum operator % (bignum b){
        return *this-*this/b*b;
    }
//————————高精对整形的计算————————
    inline bignum operator + (int b)const{
        bignum c;
        c=b;
        return *this+c;
    }
    inline bignum operator - (int b){
        bignum c;
        c=b;
        return *this-c;
    }
    inline bignum operator * (int b)const{
        bignum c;
        c=b;
        return *this*c;
    }
    inline bignum operator / (int b)const{
        bignum c;
        c=b;
        return *this/c;
    }
    inline bignum operator % (int b){
        bignum c;
        c=b;
        return *this%c;
    }
//————————高精对高精的比较————————
    inline bool operator <  (bignum b)const{
        if(len<b.len) return true;
        if(len>b.len) return false;
        for(rg i=len;i>=1;--i)
        {
            if(s[i]<b.s[i]) return true;
            else if(s[i]>b.s[i]) return false;
        }
        return false;
    }
    inline bool operator >  (bignum b)const{
        return b<*this;
    }
    inline bool operator <= (bignum b)const{
        return !(*this>b);
    }
    inline bool operator >= (bignum b)const{
        return !(*this<b);
    }
    inline bool operator == (bignum b)const{
        return (!(b>*this)&&!(b<*this));
    }
    inline bool operator != (bignum b)const{
        return !(b==*this);
    }
//————————高精对整形的比较————————
    inline bool operator <  (int b)const{
        bignum c;
        c=b;
        return *this<c;
    }
    inline bool operator >  (int b)const{
        bignum c;
        c=b;
        return *this>c;
    }
    inline bool operator <= (int b)const{
        bignum c;
        c=b;
        return *this<=c;
    }
    inline bool operator >= (int b)const{
        bignum c;
        c=b;
        return *this>=c;
    }
    inline bool operator == (int b)const{
        bignum c;
        c=b;
        return *this==c;
    }
    inline bool operator != (int b)const{
        bignum c;
        c=b;
        return *this!=c;
    }
}a,h,sum;
int main()
{
//————————test————————  
    a.read();
    h.read();
    sum=a/h;
    sum.print();
    puts("");
//————————test———————— 
//  if(a>h) cout<<">"<<endl;
//  if(a==h) cout<<"=="<<endl;
//  if(a<h) cout<<"<"<<endl;
//  if(a>=h) cout<<">="<<endl;
//  if(a!=h) cout<<"!="<<endl;
//  if(a<=h) cout<<"<="<<endl;
//————————test————————
}

转载于:https://www.cnblogs.com/superMB/p/10311397.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值