自己写的c++大整数计算BigInt类

算法使用vector保存变量,使用一个flug来保存正负。
重载的运算符有 + - * += -= *= 还有string类型转换运算符,<<输出运算符
构造函数有默认构造函数(0),从string构造(支持开头有无限个-号,但不支持正号),从BigInt构造,从long long构造(从而可以支持大多数c++的基本类型)
基本原理和手工列竖式差不多,简单测试了几组数据没问题。
因为个人经验有限,对const的设置把握不太好,不过应该可以应对大多数情况。
采用的是万进制,这样可以比较方便的计算乘法进位,单位数据使用_Uint32_t类型以方便移植(如果系统不支持这个类型,自己定义#define _Uint32_t long long,但注意long long可能属于c++11)
class里面枚举常量max是进制单位,znum是max中0的数量,因为string转换的原因只能使用10的整数倍进制,并且要max平方对于_Uint32_t不溢出。
乘法采用的也是列竖式的方法,从10进制推广到了max进制。
其他编译器出了些问题,测试用的vs,如果提示sprintf找不到请include一下stdio.h。
并未作完全测试,做过简单的测试,应该可以正常使用。

#include <iostream>

#include <vector>
#include <string>
using namespace std;
class BigInt{
private:
    enum{ max = 10000, znum = 4 };
    vector<_Uint32t> datarr;//这里用小端模式
    bool flug;//正负
public:
    /*
    * 构造函数:
    * 默认构造函数 0
    * 从long long来构造一个BigInt
    * 从string来构造一个BigInt
    * 从BigInt来构造一个BigInt
    * */
    BigInt() :datarr(1, 0),flug(true){}
    BigInt(BigInt & bigInt) :datarr(bigInt.datarr), flug(bigInt.flug){}
    BigInt(long long num) :datarr(32 / znum), flug(true){
        int i = 0;
        while (num != 0){
            datarr[i++] = num%max;
            num /= max;
        }
    }
    BigInt(const string & num) :datarr(num.size() / znum + (num.size() % znum == 0 ? 0 : 1), 0), flug(true){
        size_t i = 0, nsize = num.size();
        if (nsize == 0)
            return;
        if (num[0] == '-')
            while (i < nsize&&num[i] == '-'){
            flug = !flug;
            ++i;
            }
        --nsize;
        for (; i <= nsize; ++i)//此处不可以用i>0优化
            datarr[(nsize - i) / znum] = datarr[(nsize - i) / znum] * 10 + num[i] - '0';
    }
    BigInt & operator =(const BigInt & that){
        size_t size = that.GetNumSize();
        datarr.resize(size);
        copy(that.datarr.begin(), that.datarr.begin() + size, datarr.begin());
        flug = that.flug;
        return *this;
    }
    size_t GetNumSize()const{
        size_t size = datarr.size();
        while (--size != 0 && datarr[size] == 0);
        return ++size;
    }
    /*
    * 数学运算
    * 加乘
    * */
    /*rely on operator <*/
    /*-=提前使用了 += 和 <,若编译报错请类外定义*/
    BigInt & operator -=(const BigInt & that){
        if (flug != that.flug){
            BigInt bi(that);
            bi.flug = flug;
            *this +=bi;
            return *this;
        }
        if (*this < that){
            *this = BigInt(that) -= *this;
            flug = !flug;
            return *this;
        }
        //*this > that
        _Uint32t carry = 0;
        size_t a = GetNumSize(), b = that.GetNumSize();
        for (size_t i = 0; i < a; ++i){
            carry += i<b ? that.datarr[i] : 0;
            if (datarr[i]>carry)
                datarr[i] -= carry, carry = 0;
            else
            {
                datarr[i] += max;//注意不能出负数
                datarr[i] -= carry;
                carry = 1;
            }
        }
        return *this;
    }
    BigInt & operator +=(const BigInt & that){
        if (flug != that.flug){
            BigInt bi(that);
            bi.flug = flug;
            *this -= bi;
            return *this;
        }

        //符号相同
        _Uint32t carry = 0;
        //扩展位数
        size_t a = GetNumSize(), b = that.GetNumSize();
        datarr.resize(a = (a > b ? a : b) + 1);
        for (int i = 0; i < a; ++i){
            datarr[i] += (i < b ? that.datarr[i] : 0) + carry;
            carry = datarr[i] / max;
            datarr[i] %= max;
        }
        return *this;
    }
    BigInt operator +(const BigInt & that)const{
        return BigInt(*this) += that;
    }
    BigInt operator -(const BigInt & that)const{
        return BigInt(*this) -= that;
    }
    BigInt operator *(const BigInt & that)const{
        BigInt ret;
        _Uint32t carry = 0;
        size_t a = GetNumSize(), b = that.GetNumSize();
        ret.flug = (flug == that.flug);
        ret.datarr.resize(a + b + 1);
        for (int i = 0; i < b; ++i){//乘法竖式第二行
            //此时carry如果不是0那就不科学了
            for (int j = 0; j <= a; ++j)//乘法竖式第三行,累加
            {
                ret.datarr[i + j] += carry + (j<a ? that.datarr[i] * datarr[j] : 0);
                carry = ret.datarr[i + j] / max;
                ret.datarr[i + j] %= max;
            }
        }
        return ret;
    }
    /*
     *  自乘
     *  *=运算符用*来实现,没有优化,会产生一个中间变量
     * */
    BigInt & operator *=(const BigInt & that){
        return *this = *this * that;
    }
    /*
    * 类型转换
    * 到string
    * */
    operator string()const{
        size_t i, ssize = datarr.size();
        string ret;
        char buff[5];
        while (--ssize != 0 && datarr[ssize] == 0);
        sprintf(buff, flug ? "%u" : "-%u", datarr[ssize]);//最好不要看这个PS:一般电脑的数据内存都是小端模式,这样的话对于小于10000的数字来说%llu和%u其实输出是一样的,这里可以不用考虑类型,但是应该考虑才对
        ret = buff;
        for (i = 1; i <= ssize; ++i){
            sprintf(buff, "%04u", datarr[ssize - i]);//修改了znum后也要改这个
            ret += buff;
        }
        return ret;
    }
    /*
    * 运算符
    * < > == !=
    * */
    bool operator <(const BigInt & that){
        size_t asize = datarr.size(), bsize = that.datarr.size();
        while (--asize != 0 && datarr[asize] == 0);
        while (--bsize != 0 && that.datarr[bsize] == 0);
        if (asize != bsize)
            return asize<bsize;
        while (asize != 0 && datarr[asize] == that.datarr[asize])--asize;
        return datarr[asize]<that.datarr[asize];
    }
    //copy operator <
    //first,chage < to >
    bool operator >(const BigInt & that){
        size_t asize = datarr.size(), bsize = that.datarr.size();
        while (--asize != 0 && datarr[asize] == 0);
        while (--bsize != 0 && that.datarr[bsize] == 0);
        if (asize != bsize)
            return asize>bsize;//second,change < to >
        while (asize != 0 && datarr[asize] == that.datarr[asize])--asize;
        return datarr[asize]>that.datarr[asize];//third,change < to >
    }
    //copy operator <
    //first,chage < to ==
    bool operator ==(const BigInt & that){
        size_t asize = datarr.size(), bsize = that.datarr.size();
        while (--asize != 0 && datarr[asize] == 0);
        while (--bsize != 0 && that.datarr[bsize] == 0);
        if (asize != bsize)
            return false;//second,change to false
        while (asize != 0 && datarr[asize] == that.datarr[asize])--asize;
        return asize == 0 && datarr[0] == that.datarr[0];//third,change to this
    }
    //copy operator <
    //first,change < to !=
    bool operator !=(const BigInt & that){
        size_t asize = datarr.size(), bsize = that.datarr.size();
        while (--asize != 0 && datarr[asize] == 0);
        while (--bsize != 0 && that.datarr[bsize] == 0);
        if (asize != bsize)
            return true;//second,change to true
        while (asize != 0 && datarr[asize] == that.datarr[asize])--asize;
        return asize != 0 || datarr[0] != that.datarr[0];//third,change to this
    }
    /*
    * 输出到输出流
    * (执行string转换)
    * */
    friend ostream & operator <<(ostream & out, const BigInt & the);
};

ostream & operator <<(ostream & out, const BigInt & the){
    return out << (string)the;
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值