首页 > 其他 > 详细

Big int

时间:2016-03-05 16:05:59      阅读:344      评论:0      收藏:0      [点我收藏+]
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#define ull unsigned long long
#define INTe18 1000000000000000000
#define INT2_e18 2000000000000000000

using namespace std;

class bigint{
    friend ostream& operator<<(ostream& out, const bigint x);
    public:
        bigint operator =(const bigint b_int){
            for(unsigned i=16;i;) num[--i]=b_int.num[i];
            return *this;
        }
        bigint operator =(const ull _ull_int){
            num[0]=0;num[1]=0;num[2]=0;num[3]=0;num[4]=0;num[5]=0;num[6]=0;num[7]=0;
            num[8]=0;num[9]=0;num[10]=0;num[11]=0;num[12]=0;num[13]=0;num[14]=0;
            num[15]=_ull_int;return *this;
        }
        
        bigint operator <<=(unsigned step){
            unsigned _st_=16-step;
            bigint copy1=*this;
            for(unsigned i=0;i!=15;){
                num[i]<<=step;num[++i]|=(copy1.num[i]>>=_st_);
            }
            num[15]<<=step;
            return *this;
        }
        bigint l_move64(unsigned step){
            unsigned _st_=16-step;
            for(unsigned i=0;i!=step){
                ;
            }
        }
        
        bigint operator +=(const bigint b_int){
            unsigned j;
            for(unsigned i=16;i;){
                num[--i]+=b_int.num[i];
                if(num[i]<b_int.num[i]) for(j=i;!++num[--j];);
            }
            return *this;
        }
        bigint operator +=(const ull _ullint){
            if(num[15]+=_ullint<_ullint){
                for(unsigned i=15;!++num[--i];);
            }
            return *this;
        }
        bigint operator +(const bigint b_int){
            bigint r=*this;
            r+=b_int;
            return r;
        }
        bigint operator +(const ull _ullint){
            bigint r=*this;
            r+=_ullint;
            return r;
        }
        bigint operator ++() {for(unsigned i=16;!++num[--i];);return *this;}
        bigint operator ++(int){
            bigint old=*this;
            for(unsigned i=16;!++num[--i];);
            return old;
        }
        
        bigint operator -=(const bigint b_int){
            unsigned j;
            for(unsigned i=16;i;){
                if(num[--i]<(num[i]-=b_int.num[i])) for(j=i;~--num[--j];);
            }
            return *this;
        }
        bigint operator -=(const ull _ullint){
            if(num[15]<num[15]-_ullint) for(unsigned i=15;~--num[--i];);
            return *this;
        }
        bigint operator -(const bigint b_int){
            bigint r=*this;
            r-=b_int;
            return r;
        }
        bigint operator -(const ull _ullint){
            bigint r=*this;
            r-=_ullint;
            return r;
        }
        bigint operator --() {for(unsigned i=16;~--num[--i];);return *this;}
        bigint operator --(int){
            bigint old=*this;
            for(unsigned i=16;~--num[--i];);
            return old;
        }
        
        bigint operator *=(const ull that){
            for(unsigned i=16;i;) num[--i]*=that;
        }
        
        ull num[16];
};

ostream& operator<<(ostream& out,bigint x){
    ull outint[18]={};bool c;unsigned i,j,n;bigint cx=x;
    for(i=0;i!=16;++i){
        for(j=65;--j;){
            c=false;
            for(n=18;--n;){
                if(outint[n]==INTe18) {outint[n]=0;++outint[n-1];c=false;}
                outint[n]<<=1;
                if(c) ++outint[n];
                if(c=(INTe18<=outint[n])) outint[n]-=INTe18;
            }
            outint[0]<<=1;if(c) ++outint[0];
            if(cx.num[i]&0x8000000000000000) ++outint[17];
            cx.num[i]<<=1;
        }
    }
    out<<setfill(0);j=17;
    for(i=0;i!=18;i++){
        if(outint[i]) {j=i;break;}
    }
    out<<outint[j]<<setw(18);
    while(++j!=18) out<<outint[j];
    out<<setw(0);
    return out;
}

int main(){
    bigint a;a=1;
    a<<=63;
    cout<<a;
}

 

Big int

原文:http://www.cnblogs.com/TheName/p/5244912.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!