C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了C++大数模板大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
#include<bits/stdc++.h>

//大整数
struct BigInteger {
    static const int BASE = 100000000;//和WIDTH保持一致
    static const int WIDTH = 8;//八位一存储,如修改记得修改输出中的%08d
    bool sign;//符号,0表示负数
    size_t length;
    std::vector<int> num;//反序存
//构造函数
    BigInteger(long long x = 0) { *this = x; }
    BigInteger(const std::string &X) { *this = x; }
    BigInteger(const BigInteger &X) { *this = x; }

//剪掉前导0
    void cutLeadingZero() {
        while (num.BACk() == 0 && num.size() != 1) { num.pop_BACk(); }
    }

//设置数的长度
    void setLength() {
        cutLeadingZero();
        int tmp = num.BACk();
        if (tmp == 0) { length = 1; }
        else {
            length = (num.size() - 1) * WIDTH;
            while (tmp > 0) {
                ++length;
                tmp /= 10;
            }
        }
    }

//赋值运算符
    BigInteger &operator=(long long X) {
        num.clear();
        if (x >= 0) sign = true;
        else {
            sign = false;
            x = -x;
        }
        do {
            num.emplace_BACk(x % BASE);
            x /= BASE;
        } while (x > 0);
        setLength();
        return *this;
    }

//赋值运算符
    BigInteger &operator=(const std::string &str) {
        num.clear();
        sign = (str[0] != ‘-‘);//设置符号
        int x,len = (str.size() - 1 - (!sign)) / WIDTH + 1;
        for (int i = 0; i < len; i++) {
            int End = str.length() - i * WIDTH;
            int start = std::max((int) (!sign),End - WIDTH);//防止越界
            sscanf(str.substr(start,End - start).c_str(),"%d",&X);
            num.push_BACk(X);
        }
        setLength();
        return *this;
    }

//赋值运算符
    BigInteger &operator=(const BigInteger &tmp) {
        num = tmp.num;
        sign = tmp.sign;
        length = tmp.length;
        return *this;
    }


//数的位数
    size_t size() const { return length; }

//*10^n 除法中用到
    BigInteger e(size_t n) const {
        int tmp = n % WIDTH;
        BigInteger ans;
        ans.length = n + 1;
        n /= WIDTH;
        while (ans.num.size() <= n) ans.num.push_BACk(0);
        ans.num[n] = 1;
        while (tmp--) ans.num[n] *= 10;
        return ans * (*this);
    }

//绝对值
    BigInteger abs() const {
        BigInteger ans(*this);
        ans.sign = true;
        return ans;
    }

//正号
    const BigInteger &operator+() const { return *this; }

// + 运算符
    BigInteger operator+(const BigInteger &b) const {
        if (!b.sign) { return *this - (-b); }
        if (!sign) { return b - (-*this); }
        BigInteger ans;
        ans.num.clear();
        for (int i = 0,g = 0;; i++) {
            if (g == 0 && i >= num.size() && i >= b.num.size()) break;
            int x = g;
            if (i < num.size()) x += num[i];
            if (i < b.num.size()) x += b.num[i];
            ans.num.push_BACk(x % BASE);
            g = x / BASE;
        }
        ans.setLength();
        return ans;
    }

//负号
    BigInteger operator-() const {
        BigInteger ans(*this);
        if (ans != 0) ans.sign = !ans.sign;
        return ans;
    }

// - 运算符
    BigInteger operator-(const BigInteger &b) const {
        if (!b.sign) { return *this + (-b); }
        if (!sign) { return -((-*this) + b); }
        if (*this < b) { return -(b - *this); }
        BigInteger ans;
        ans.num.clear();
        for (int i = 0,g = 0;; i++) {
            if (g == 0 && i >= num.size() && i >= b.num.size()) break;
            int x = g;
            g = 0;
            if (i < num.size()) x += num[i];
            if (i < b.num.size()) x -= b.num[i];
            if (x < 0) {
                x += BASE;
                g = -1;
            }
            ans.num.push_BACk(X);
        }
        ans.setLength();
        return ans;
    }

// * 运算符
    BigInteger operator*(const BigInteger &b) const {
        int lena = num.size(),lenb = b.num.size();
        std::vector<long long> ansLL;
        for (int i = 0; i < lena + lenb; i++) ansLl.push_BACk(0);
        for (int i = 0; i < lena; i++) {
            for (int j = 0; j < lenb; j++) {
                ansLL[i + j] += (long long) num[i] * (long long) b.num[j];
            }
        }
        while (ansLl.BACk() == 0 && ansLl.size() != 1) ansLl.pop_BACk();
        int len = ansLl.size();
        long long g = 0,tmp;
        BigInteger ans;
        ans.sign = (ansLl.size() == 1 && ansLL[0] == 0) || (sign == b.sign);
        ans.num.clear();
        for (int i = 0; i < len; i++) {
            tmp = ansLL[i];
            ans.num.emplace_BACk((tmp + g) % BASE);
            g = (tmp + g) / BASE;
        }
        if (g > 0) ans.num.emplace_BACk(g);
        ans.setLength();
        return ans;
    }

// / 运算符 (大数除小数)
    BigInteger operator/(const long long &b) const {
        BigInteger c;
        c.num.clear();
        for (int i = 0; i < num.size(); i++) {
            c.num.push_BACk(0);
        }
        long long g = 0;
        for (int i = num.size() - 1; i >= 0; i--) {
            c.num[i] = int((num[i] + g * BASE) / b);
            g = num[i] + g * BASE - c.num[i] * b;
        }
        for (int i = num.size() - 1; c.num[i] == 0; i--) {
            c.num.pop_BACk();
        }
        return c;
    }

// /运算符 (大数除大数)
    BigInteger operator/(const BigInteger &b) const {
        BigInteger aa((*this).abs());
        BigInteger bb(b.abs());
        if (aa < bb) return 0;
        char *str = new char[aa.size() + 1];
        memset(str,sizeof(char) * (aa.size() + 1));
        BigInteger tmp;
        int lena = aa.length,lenb = bb.length;
        for (int i = 0; i <= lena - lenb; i++) {
            tmp = bb.e(lena - lenb - i);
            while (aa >= tmp) {
                ++str[i];
                aa = aa - tmp;
            }
            str[i] += ‘0‘;
        }
        BigInteger ans(str);
        delete[]str;
        ans.sign = (ans == 0 || sign == b.sign);
        return ans;
    }

// % 运算符 (大数取模小数)
    BigInteger operator%(const long long &b) const {
        long long ans = 0;
        for (int i = num.size() - 1; i >= 0; i--)
            ans = (ans * BASE + num[i]) % b;
        return ans;
    }

// %运算符 (大数取模大数)
    BigInteger operator%(const BigInteger &b) const {
        return *this - *this / b * b;
    }

    BigInteger &operator++() {
        *this = *this + 1;
        return *this;
    } // ++ 运算符
    BigInteger &operator--() {
        *this = *this - 1;
        return *this;
    } // -- 运算符
    BigInteger &operator+=(const BigInteger &b) {
        *this = *this + b;
        return *this;
    } // += 运算符
    BigInteger &operator-=(const BigInteger &b) {
        *this = *this - b;
        return *this;
    } // -= 运算符
    BigInteger &operator*=(const BigInteger &b) {
        *this = *this * b;
        return *this;
    } // *=运算符
    BigInteger &operator/=(const long long &b) {
        *this = *this / b;
        return *this;
    } // /=运算符
    BigInteger &operator/=(const BigInteger &b) {
        *this = *this / b;
        return *this;
    } // /= 运算符
    BigInteger &operator%=(const long long &b) {
        *this = *this % b;
        return *this;
    } // %=运算符
    BigInteger &operator%=(const BigInteger &b) {
        *this = *this % b;
        return *this;
    } // %=运算符
// < 运算符
    bool operator<(const BigInteger &b) const {
        if (sign && !b.sign) { return false; }//正负
        else if (!sign && b.sign) { return true; }//负正
        else if (!sign && !b.sign) { return -b < -*this; }//负负
        //正正
        if (num.size() != b.num.size()) return num.size() < b.num.size();
        for (int i = num.size() - 1; i >= 0; i--)
            if (num[i] != b.num[i]) return num[i] < b.num[i];
        return false;
    }

    bool operator>(const BigInteger &b) const { return b < *this; }              // >  运算符
    bool operator<=(const BigInteger &b) const { return !(b < *this); }           // <= 运算符
    bool operator>=(const BigInteger &b) const { return !(*this < b); }           // >= 运算符
    bool operator!=(const BigInteger &b) const { return b < *this || *this < b; }     // != 运算符
    bool operator==(const BigInteger &b) const { return !(b < *this) && !(*this < b); }//==运算符

    bool operator||(const BigInteger &b) const { return *this != 0 || b != 0; } // || 运算符
    bool operator&&(const BigInteger &b) const { return *this != 0 && b != 0; } // && 运算符
    bool operator!() { return (bool) (*this == 0); }                            // ! 运算符

    //重载<<使得可以直接输出大数
    friend std::ostream &operator<<(std::ostream &out,const BigInteger &X) {
        if (!x.sign) out << ‘-‘;
        out << x.num.BACk();
        for (int i = x.num.size() - 2; i >= 0; i--) {
            char buf[10];
            //如WIDTH和BASR有变化,此处要修改为%0(WIDTH)d
            sprintf(buf,"%08d",x.num[i]);
            for (int j = 0; j < strlen(buf); j++) out << buf[j];
        }
        return out;
    }

    //重载>>使得可以直接输入大数
    friend std::istream &operator>>(std::istream &in,BigInteger &X) {
        std::string str;
        in >> str;
        size_t len = str.size();
        int start = 0;
        if (str[0] == ‘-‘) start = 1;
        if (str[start] == ‘\0‘) return in;
        for (int i = start; i < len; i++) {
            if (str[i] < ‘0‘ || str[i] > ‘9‘) return in;
        }
        x.sign = !start;
        x = str.c_str();
        return in;
    }
    BigInteger pow(int n) {
        BigInteger ans = 1,base = *this;
        while (n) {
            if (n & 1) ans = ans * base;
            base = base * base;
            n >>= 1;
        }
        return ans;
    }
};

int main() {
    BigInteger a,b;
    std::cin >> a >> b;
    std::cout << (a + b) << std::endl;
    return 0;
}

大佬总结

以上是大佬教程为你收集整理的C++大数模板全部内容,希望文章能够帮你解决C++大数模板所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。