luogu 高精度加减类型 【模版+】

4 篇文章 0 订阅
2 篇文章 0 订阅

总结

这次是没有具体的题目了,就是单纯地完善了上次的高精度的模版,支持了正负数加减。


Code

// head files excluded
using namespace std;
const int N = 15000;

struct BigInt{
    int data[N];
    int size; // size域 显式声明
    bool nega;// 记录符号
    BigInt(){
        memset(data,0,sizeof(data));
        size = 1;
        nega=false;
    }

    BigInt(const BigInt &x){
        memset(data,0,sizeof(data));
        size = x.size;
        nega = x.nega;
        for(int i=0;i<size;i++) data[i]=x.data[i];
    }


    BigInt(int x){
        memset(data,0,sizeof(data));
        int i=0;
        if(x<0){
            nega = true;
            x=-x;
        }else nega = false;

        while(x){
            data[i++] = x%10;
            x/=10;
        }
        size = i;
    }

    BigInt(const string &x){
        memset(data,0,sizeof(data));
        size = x.size();
        bool flag;
        if(x[0] == '-'){
            nega = true;
            flag = true;
        }else if(x[0] == '+') {
            nega = false;
            flag =true;
        }else{
            nega =false;
            flag =false;
        }
        if(flag) size--;
        for(int i= flag?1:0, j=0 ;j<size;i++,j++){
            data[size-j-1] = (x[i] - '0') ;
        }
    }

    BigInt inverse() const{
        BigInt ret(*this);
        ret.nega = !nega;
        return ret;
    }

    BigInt abs() const{
        if(!nega) return BigInt(*this);
        else return this->inverse();
    }

    BigInt operator +(const BigInt &x)const{
        if(nega != x.nega){ // a + (-b) equals  a-b
            if(nega == true) { 
                BigInt cp(*this);
                cp.nega = false;
                return (x-cp);
            }else{
                BigInt cp(x);
                cp.nega = false;
                return (*this - cp);
            }
        }
        int msize = (size>x.size ? size : x.size);
        BigInt ret;
        ret.nega = nega;
        for(int i = 0; i<msize;i++){
            ret.data[i] = data[i]+x.data[i];
        }
        int adding = 0;
        for(int i = 0; i<msize;i++){
            ret.data[i]+=adding;
            adding = ret.data[i]/10;
            ret.data[i]%=10;
        }
        if(adding>0) {
            ret.data[msize] = adding;
            ret.size = msize+1;
        }else ret.size = msize;

        return ret;
    }

    BigInt operator -(const BigInt &x)const{
        if(nega != x.nega){ // a - (-b) equals  a+b
            if(nega == true) { 
                BigInt cp(x);
                cp.nega = true;;
                return (*this+cp);
            }else{
                BigInt cp(x);
                cp.nega = false;
                return (*this+cp);
            }
        }
        BigInt bg,sm;
        bool flag;
        if( this->abs() < x.abs() ){
            bg = x;
            sm = *this;
            if(nega==true){
                flag = false;
            }else{
                flag = true;
            }
        }else{
            bg = *this;
            sm = x;
            if(nega==true){
                flag = true;
            }else{
                flag = false;
            }

        }

        int msize = bg.size > sm.size ? bg.size:sm.size;
        int minus=0;
        for(int i=0;i<msize;i++){
            bg.data[i] -= minus;
            if(bg.data[i] < sm.data[i]){
                minus = 1;
                sm.data[i] = bg.data[i]+10 - sm.data[i];
            }else{
                minus = 0;
                sm.data[i] = bg.data[i] - sm.data[i];
            }
        }
        while(sm.data[msize-1]==0 && msize!=1)  msize--;// consider when 0 and size=1

        sm.size = msize;

        sm.nega = flag;
        return sm;

    }

    bool operator <(const BigInt &x)const{
        if(nega!=x.nega){
            return nega==true;
        }

        bool ret;

        int i;
        if(size == x.size){
            for(i=size-1; data[i]==x.data[i] && i>0;i--);// equal until last one
            if(i>=0) ret = data[i] < x.data[i];
            else return false;// equal
        }else ret = size < x.size;

        if(!nega) return ret;
        else return !ret;
    }
};
ostream& operator <<(ostream& out, const BigInt &x){
    if(x.nega) cout<<'-';
    for(int i=x.size-1;i>=0;i--) out<<x.data[i];
    return out;
}
int main(){

    string a,b;
    cin>>a>>b;
    BigInt an(a),bn(b);

    cout<< an-bn <<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值