高精度模板

我的高精度模板

vector存储,带符号,带输入输出,加减乘,比较大小,赋值,暂时不完全。
(请无视前几行的zz操作)

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

typedef std :: vector<int> vec;
typedef std :: string string;
typedef std :: istream istream;
typedef std :: ostream ostream;
#define cin std :: cin
#define cout std :: cout

class BigNum {
private :
    vec num;
    bool sign;

public :
    BigNum() :
        sign(0) {}

    BigNum operator + (const BigNum &a) const {
        int size1 = this->num.size(), size2 = a.num.size();
        int size = std :: max(size1, size2);
        if (this->sign ==  a.sign) {
            int x = 0;
            BigNum res;
            for (int i = 0; i < size; i++) {
                int now = x;
                if (size1 > i) {
                    now += this->num[i];
                }
                if (size2 > i) {
                    now += a.num[i];
                }
                x = now / 10;
                now = now % 10;
                res.num.push_back(now);
            }
            if (x) {
                res.num.push_back(x);
            }
            res.sign = this->sign;
            return res;
        } else {
            BigNum b, res;
            if (this->sign) { //*this < 0
                b = *this;
                b.sign = 0;
                if (a < b) {
                    res = b - a;
                    res.sign = 1;
                    return res;
                } else {
                    return a - b;
                }
            } else { //a < 0
                b = a;
                b.sign = 0;
                if (*this < b) {
                    res = b - *this;
                    res.sign = 1;
                    return res;
                } else {
                    return *this - b;
                }
            }
        }
    }

    BigNum operator - (const BigNum &a) const { // *this > a && this->sign == a.sign
        int size = this->num.size(), sizea = a.num.size();
        BigNum res, b = *this;
        for (int i = 0; i < size; i++) {
            int now = sizea > i ? a.num[i] : 0;
            if (b.num[i] < now) {
                b.num[i] += 10;
                b.num[i + 1] -= 1;
            }
            res.num.push_back(b.num[i] - now);
        }

        while (res.num.size() > 1 && res.num.back() == 0) {
            res.num.pop_back();
        }
        return res;
    }

    BigNum operator * (const BigNum &a) const {
        BigNum res;
        int size1 = this->num.size(), size2 = a.num.size();
        res.num.resize(size1 + size2 + 2);
        for (int i = 0; i < size1; i++) {
            for (int j = 0; j < size2; j++) {
                res.num[i + j] += this->num[i] * a.num[j];
                res.num[i + j + 1] += res.num[i + j] / 10;
                res.num[i + j] %= 10;
            }
        }
        for(int i = 0; i <= size1 + size2; i++) {
            res.num[i+1] += res.num[i] / 10;
            res.num[i] %= 10;
        }
        while (res.num.size() > 1 && res.num.back() == 0) {
            res.num.pop_back();
        }
        res.sign = a.sign ^ this->sign;
        if (res.num.size() == 1 && res.num[0] == 0) {
            res.sign = 0;
        }
        return res;
    }

    bool operator < (const BigNum &a) const {
        if (this->sign != a.sign) {
            return this->sign > a.sign;
        }
        if (this->sign == 0) {
            if (this->num.size() == a.num.size()) {
                for (int i = a.num.size() - 1; i >= 0; i--) {
                    if (this->num[i] < a.num[i]) {
                        return 1;
                    }
                }
                return 0;
            } else {
                return this->num.size() < a.num.size();
            }           
        } else {
            if (this->num.size() == a.num.size()) {
                for (int i = a.num.size() - 1; i >= 0; i--) {
                    if (this->num[i] > a.num[i]) {
                        return 1;
                    }
                }
                return 0;
            } else {
                return this->num.size() > a.num.size();
            }   
        }
    }

    bool operator > (const BigNum &a) const {
        if (this->sign != a.sign) {
            return this->sign < a.sign;
        }
        if (this->sign == 1) {
            if (this->num.size() == a.num.size()) {
                for (int i = a.num.size() - 1; i >= 0; i--) {
                    if (this->num[i] < a.num[i]) {
                        return 1;
                    }
                }
                return 0;
            } else {
                return this->num.size() < a.num.size();
            }           
        } else {
            if (this->num.size() == a.num.size()) {
                for (int i = a.num.size() - 1; i >= 0; i--) {
                    if (this->num[i] > a.num[i]) {
                        return 1;
                    }
                }
                return 0;
            } else {
                return this->num.size() > a.num.size();
            }   
        }       if (this->num.size() == a.num.size()) {
            for (int i = a.num.size() - 1; i >= 0; i--) {
                if (this->num[i] > a.num[i]) {
                    return 1;
                }
            }
            return 0;
        } else {
            return this->num.size() > a.num.size();
        }
    }   

    friend istream& operator >> (istream& in, BigNum& a) {
        string s;
        in >> s;
        a.num.clear();
        a.sign = 0;
        int start = 0;
        if (s[0] == '-') {
            a.sign = 1;
            start++;
        }
        for (int i = s.size() - 1; i >= start; i--) {
            a.num.push_back(s[i] - '0');
        }
        return in;
    }

    friend ostream& operator << (ostream& out, const BigNum& a) {
        if (a.sign) {
            out << '-';
        }
        for (int i = a.num.size() - 1; i >= 0; i--) {
            out << a.num[i];
        }
        return out;
    }
    
    BigNum& operator = (int x) {
        this->num.clear();
        if (!x) {
            this->num.push_back(0);
        }
        while (x) {
            this->num.push_back(x % 10);
            x /= 10;
        }
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值