cpp 大整数类示例,加减乘除

注:练手写的,写的比较简单,只考虑正数。没考虑运算速度。0做除数时不抛出异常或终止程序,而是返回0。取模、逻辑运算符、自增自减、位运算和一些其他的运算符没有处理。
加注释是有部分修改,可能无法运行
BigInt.h

#ifndef H_BIGINT
#define H_BIGINT

#include<vector>
#include<string>
#include<algorithm>
#include<iostream>

class BigInt{
private:
    std::vector<int> digits;  //储存每一位
public:
    BigInt();//空白构造函数
    BigInt(long long);//正数转大整数
    BigInt(const BigInt &);//拷贝构造函数
    BigInt(const BigInt & b, int s1, int s2);// 取b的第s1位到第s2位
    BigInt(const std::string&);//字符串装大整数
    //重载赋值运算符
    BigInt & operator=(long long);
    BigInt & operator=(const std::string&);
    BigInt & operator+=(const BigInt b);
    BigInt & operator-=(const BigInt b);
    BigInt & operator*=(const BigInt b);
    BigInt & operator/=(const BigInt b);
    BigInt & operator>>=(unsigned int n);
    BigInt & operator<<=(unsigned int n);

    friend std::istream & operator>>(std::istream &,BigInt &);//重载输入运算符>>
    friend std::ostream & operator<<(std::ostream &,const BigInt &);//重载输出运算符<<

    // 重载比较运算符
    bool operator==(const BigInt & b) const;
    bool operator!=(const BigInt & b) const;
    bool operator>=(const BigInt & b) const;
    bool operator<=(const BigInt & b) const;
    bool operator>(const BigInt & b) const;
    bool operator<(const BigInt & b) const;

    bool isZero()const;  // 判断自身是否为0
    int size()const;  // 输出数字位数
    int length()const;  // =size()
    
    int at(unsigned int n)const;  // 获取从高位到低位的第n+1位数,从0开始,超范围返回-1
    int operator[](unsigned int n)const;/  / 获取从高位到低位的第n+1位数,从0开始,不检查是否超范围,可能出现编译正常,运行出错的情况


    // 重载算数运算符
    BigInt addOne(int n) const;
    BigInt operator+(const BigInt & b) const;
    BigInt operator-(const BigInt & b) const;
    BigInt mulOne(int n) const;  // BigInt 与 一位数 相乘
    BigInt operator*(const BigInt & b) const;  // BigInt 与 BigInt 相乘
    BigInt operator/(const BigInt & b) const;
    BigInt operator>>(unsigned int n) const;
    BigInt operator<<(unsigned int n) const;
    
    friend BigInt operator+(unsigned long long a, const BigInt & b);
    friend BigInt operator-(unsigned long long a, const BigInt & b);
    friend BigInt operator*(unsigned long long a, const BigInt & b);
    friend BigInt operator/(unsigned long long a, const BigInt & b);

};
class SignBigInt{
private:
    BigInt num;
    bool posi;
public:
    // 留空
};
#endif

BigInt.cpp


#include"BigInt.h"

BigInt::BigInt(){}
BigInt::BigInt(long long l){*this = l;}
BigInt::BigInt(const BigInt & a){digits.assign(a.digits.begin(),a.digits.end());}
BigInt::BigInt(const BigInt & a, int s1, int s2){
    digits.assign(a.digits.begin()+s1,a.digits.begin()+s1+s2);
}
BigInt::BigInt(const std::string& s){*this = s;}
BigInt & BigInt::operator=(long long l){
    digits.clear();
    for(;l>0;l/=10){
        digits.push_back((int)(l%10));
    }
    std::reverse(digits.begin(),digits.end());
    return *this;
}
BigInt & BigInt::operator=(const std::string& s){
    digits.clear();
    for(size_t i=0;i<s.size();++i){
        digits.push_back((int)(s.at(i)-'0'));
    }
    return *this;
}
std::istream & operator>>(std::istream & i,BigInt & a){
    std::string s;i >> s;a = s;
    if(a.digits[0] == 0) {
        auto it = find_if(a.digits.begin(),a.digits.end(),[](int x){ return x != 0;});
        a.digits.erase(a.digits.begin(), it);
    }
    return i;
}
std::ostream & operator<<(std::ostream & o,const BigInt & b){
    if(b.isZero()) o << "0";
    else for(int n:b.digits){o << n;}
    return o;
}
bool BigInt::operator==(const BigInt & b) const{
    if(digits.size()!=b.digits.size()){return false;}
    else if(digits != b.digits){return false;}
    else {return true;}
}
bool BigInt::operator!=(const BigInt & b) const{
    if(digits.size()!=b.digits.size()){return true;}
    else if(digits != b.digits){return true;}
    else {return false;}
}
bool BigInt::operator>=(const BigInt & b) const{
    if(digits.size()<b.digits.size()){return false;}
    else if(digits < b.digits){return false;}
    else {return true;}
}
bool BigInt::operator<=(const BigInt & b) const{
    if(digits.size()>b.digits.size()){return false;}
    else if(digits > b.digits){return false;}
    else {return true;}
}

bool BigInt::operator>(const BigInt & b) const{
    if(digits.size() < b.digits.size()){return false;}
    else if(digits.size() > b.digits.size()){return true;}
    else {
        for(size_t i=0;i<digits.size();++i){
            if(digits[i]<b.digits[i]) return false;
        }
        return true;
    }
}
bool BigInt::operator<(const BigInt & b) const{
    if(digits.size() > b.digits.size()){return false;}
    else if(digits.size() < b.digits.size()){return true;}
    else {
        for(size_t i=0;i<digits.size();++i){
            if(digits[i]>b.digits[i]) return false;
        }
        return true;
    }
}

bool BigInt::isZero()const{return digits.size()==0;}
int BigInt::size()const{return digits.size()?digits.size():1;}
int BigInt::length()const{return size();}
int BigInt::at(unsigned int n)const{
    if(n<size()) return digits[n];
    else return -1;
}
int BigInt::operator[](unsigned int n)const{
    return digits[n];
}

BigInt BigInt::addOne(int n) const{
    BigInt a,b=*this;
    reverse(b.digits.begin(),b.digits.end());
    if(b.digits.size()==0){a=n;}
    else if(n==0){a=b;}
    else{
        int carry=0,sum=0;
        for(int i=0;i<digits.size();++i){
            sum = n + digits[i] + carry;
            carry = sum/10;
            a.digits.push_back(sum%10);
        }
        while(carry>0){
            a.digits.push_back(carry);
            carry/=10;
        }
    }
    reverse(a.digits.begin(),a.digits.end());
    if(a.digits[0] != 0) return a;
    auto it = find_if(a.digits.begin(),a.digits.end(),[](int x){ return x != 0;});
    a.digits.erase(a.digits.begin(), it);
    return a;
}

BigInt BigInt::operator+(const BigInt & bi) const{
    BigInt c;
    if(digits.size()==0){c=bi;return c;}
    else if(bi.digits.size()==0){return *this;}
    else {
        BigInt a = *this,b = bi;
        reverse(a.digits.begin(),a.digits.end());
        reverse(b.digits.begin(),b.digits.end());
        int carry=0,sum=0;
        int s1 = a.digits.size(),s2 = b.digits.size();
        if(s1 <= s2){
            for(int i=0;i<s1;++i){
                sum = carry+a.digits[i]+b.digits[i];
                carry = sum/10;
                c.digits.push_back(sum%10);
            }
            for(int i=s1;i<s2;++i){
                sum = carry + b.digits[i];
                carry = sum/10;
                c.digits.push_back(sum%10);
            }
            while(carry>0){
                c.digits.push_back(carry);
                carry/=10;
            }
        } else {
            for(int i=0;i<s2;++i){
                sum = carry+a.digits[i]+b.digits[i];
                carry = sum/10;
                c.digits.push_back(sum%10);
            }
            for(int i=s2;i<s1;++i){
                sum = carry + a.digits[i];
                carry = sum/10;
                c.digits.push_back(sum%10);
            }
            while(carry>0){
                c.digits.push_back(carry);
                carry/=10;
            }
        }
    }
    reverse(c.digits.begin(),c.digits.end());
    if(c.digits[0] != 0) return c;
    auto it = find_if(c.digits.begin(),c.digits.end(),[](int x){ return x != 0;});
    c.digits.erase(c.digits.begin(), it);
    return c;
}


BigInt BigInt::operator-(const BigInt & bi) const{
    BigInt a=*this,b=bi,c;
    int carry = 0,sub = 0,i=0;
    int s1 = a.digits.size(), s2 = b.digits.size(),s3=0;
    if(s1 == s2){
        if(a.digits == b.digits) return BigInt();
        else if(a.digits > b.digits){
            for(i=s1-1;i>=0;--i){
                sub = a.digits[i] - b.digits[i] - carry;
                if(sub < 0){sub += 10;carry = 1;}
                else {carry = 0;}
                c.digits.push_back(sub);
            }
        } else {
            for(i=s2-1;i>=0;--i){
                sub = b.digits[i] - a.digits[i] - carry;
                if(sub < 0){sub += 10;carry = 1;}
                else {carry = 0;}
                c.digits.push_back(sub);
            }
        }
        
    }else if(s1 > s2){
        s3 = s1 - s2;
        for(i=s1-1;i>=s3;--i){
            sub = a.digits[i] - b.digits[i-s3] - carry;
            if(sub < 0){sub += 10;carry = 1;}
            else {carry = 0;}
            c.digits.push_back(sub);
        }
        while(i >= 0){
            sub = a.digits[i] - carry;
            if(sub < 0){sub += 10;carry = 1;}
            else {carry = 0;}
            c.digits.push_back(sub);
            --i;
        }
    } else {
        s3 = s2 - s1;
        for(i=s2-1;i>=s3;--i){
            sub = b.digits[i] - a.digits[i-s3] - carry;
            if(sub < 0){sub += 10;carry = 1;}
            else {carry = 0;}
            c.digits.push_back(sub);
        }
        while(i >= 0){
            sub = b.digits[i] - carry;
            if(sub < 0){sub += 10;carry = 1;}
            else {carry = 0;}
            c.digits.push_back(sub);
            --i;
        }
    }
    reverse(c.digits.begin(),c.digits.end());
    if(c.digits[0] != 0) return c;
    auto it = find_if(c.digits.begin(),c.digits.end(),[](int x){ return x != 0;});
    c.digits.erase(c.digits.begin(), it);
    return c;
}

BigInt BigInt::mulOne(int n) const{
    BigInt a = *this;
    reverse(a.digits.begin(),a.digits.end());
    int carry = 0, mul = 0;
    for(int i=0;i<a.digits.size();++i){
        mul = carry + a.digits[i]*n;
        carry = mul / 10;
        a.digits[i] = mul % 10;
    }
    while(carry){ a.digits.push_back(carry);carry/=10;}
    reverse(a.digits.begin(),a.digits.end());
    return a;
}

BigInt BigInt::operator*(const BigInt & bi) const{
    if(isZero() || bi.isZero()) return BigInt();  // 如果两个数中有0,则相乘结果为0
    BigInt re;
    int scale = 1;
    if(digits.size() > bi.digits.size()){
        for(int i=bi.digits.size()-1;i>=0;--i)
            re = re + mulOne(bi.digits[i]*scale);
        scale += 10;
    } else {
        for(int i=digits.size()-1;i>=0;--i)
            re = re + bi.mulOne(digits[i]*scale);
        scale += 10;
    }
    return re;
}
BigInt BigInt::operator/(const BigInt & b) const{
    // 如果除数或被除数为0,返回0 !!! 除数为0时未抛出异常
    if(b.isZero() || isZero()) return BigInt(); 
    if(*this < b) return 0; 
    else if(*this == b) return 1;
    else{
        BigInt ret,tmp;
        int s1 = size(),s2 = b.size(),s3,dig=0;
        s3 = s1 - s2;
        for(int i=0;i<=s3;++i){
            if(tmp == 0) {tmp = BigInt(*this, i, s2);
            } else {
                tmp.digits.push_back(digits[s2+i-1]);
            }
            dig = 0;
            if(tmp < b){ret.digits.push_back(dig);}
            else {
                while(tmp > b){
                    tmp = tmp - b;
                    dig += 1;
                }
                ret.digits.push_back(dig);
            }
        }
        if(ret.digits[0] != 0) return ret;
        auto it = find_if(ret.digits.begin(),ret.digits.end(),[](int x){ return x != 0;});
        ret.digits.erase(ret.digits.begin(), it);
        return ret;
    }
}

BigInt operator>>(unsigned int n) const{
    if(n>=size()) return BigInt();
    else {
        BigInt re = *this;
        re.digits.assign(re.digits.begin(),re.digits.begin()+size()-n);
        return re;
    }
}
BigInt operator<<(unsigned int n) const{
    if(isZero()) return BigInt();
    else{
        BigInt re = *this;
        // while(n--){re.digits.push_back(0);}
        re.digits.insert(re.digits.end(), n, 0);
        return re;
    }
}

BigInt operator+(unsigned long long a, const BigInt & b){return (b+a);}
BigInt operator-(unsigned long long a, const BigInt & b){return (BigInt(a)-b);}
BigInt operator*(unsigned long long a, const BigInt & b){return (b*a);}
BigInt operator/(unsigned long long a, const BigInt & b){return (BigInt(a)/b);}

BigInt & BigInt::operator+=(const BigInt b){*this = *this + b;return *this;}
BigInt & BigInt::operator-=(const BigInt b){*this = *this - b;return *this;}
BigInt & BigInt::operator*=(const BigInt b){*this = *this * b;return *this;}
BigInt & BigInt::operator/=(const BigInt b){*this = *this / b;return *this;}
BigInt & BigInt::operator>>=(unsigned int n){
    // *this = *this >> n;
    if(n>=size()){digits.clear();}
    else {digits.assign(digits.begin(),digits.begin()+size()-n);}
    return *this;
}
BigInt & BigInt::operator<<=(unsigned int n){
    if(!isZero()){
        // while(n--){digits.push_back(0);}
        digits.insert(digits.end(), n, 0);
    }
    return *this;
}

一个测试示例main.cpp

#include"BigInt.h"

using namespace std;

int main(){
    BigInt a(0),b(2);
    cout << "0/2=" << (a/b) << endl;
    a = 8,b = 2;
    cout << "8/2=" << (a/b) << endl;
    a = 15,b = 4;
    cout << "15/4=" << (a/b) << endl;
    a = 15,b = 0;
    cout << "15/0=" << (a/b) << endl;
    a = 128,b = 11;
    cout << "128/11=" << (a/b) << endl;
    a = 1100,b = 9;
    cout << "1100/9=" << (a/b) << endl;
    a = 15,b = 20;
    cout << "15/20=" << (a/b) << endl;

    
    // cout << "111" << endl;
    // BigInt a(123),b("124"),c;
    // cout << "a=" << a << endl;
    // cout << "b=" << b << endl;
    // cout << "a+b=" << (a+b) << endl;
    // cout << "a-b=" << (a-b) << endl;
    // cout << "b-a=" << (b-a) << endl;
    // cout << "b*9=" << (b*9) << endl;
    // cout << "9*b=" << (9*b) << endl;
    // cout << "b+9=" << (b+9) << endl;
    // cout << "9+b=" << (9+b) << endl;
    // cout << "b-9=" << (b-9) << endl;
    // cout << "9-b=" << (9-b) << endl;
    // if(a<b)cout << "a<b" << endl;
    // if(a>b)cout << "a>b" << endl;
    // if(a<=b)cout << "a<=b" << endl;
    // if(a>=b)cout << "a>=b" << endl;


    // BigInt a(123);
    // a = 444;
    // cout << a << endl;
    // a = "555";
    // cout << a << endl;
    // BigInt b(123);
    // cout << b << endl;
    // BigInt c("123");
    // cout << c << endl;
    // BigInt d(c);
    // cout << d << endl;
    // BigInt e=d;
    // cout << d << endl;
    // BigInt f(111);
    // cout << f << endl;
    // f = d;
    // cout << f << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

今夕何夕2112

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值