C++高精度整数处理板子

支持加减,日后会完善

struct BigInteger{
    static const int Base = 10;
    static const int WIDTH = 1;
    vector<int>s;
    BigInteger (ll num = 0){
        *this = num;
    }
    BigInteger operator = (ll num){
        s.clear();
        do{
            s.push_back(num%Base);
            num/=Base;
        }while (num > 0);
        return *this;
    }

    BigInteger operator = (const string &str){
        s.clear();
        int i,len = (str.length());
        for(i=1;i<=len;i++){
            s.push_back(str[len-i] - '0');
        }
        return *this;
    }

    BigInteger operator + (const BigInteger &b){
        BigInteger c;
        c.s.clear();
        for(int i = 0,g = 0;;i++){
            if(g == 0 && i >= s.size() && i > b.s.size()){break;}
            int x = g;
            if(i < s.size()){
                x+=s[i];
            }
            if(i < b.s.size()){
                x+=b.s[i];
            }
            c.s.push_back(x%Base);
            g = x/Base;
        }
        return c;
    }

    BigInteger operator - (const BigInteger &b){
        BigInteger c = *this;
        for(int i = 0;i<b.s.size();i++){
            c.s[i] -= b.s[i];
            if(c.s[i] < 0){
                c.s[i+1]--;
                c.s[i]+=Base;
            }
        }
        return c;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,我无法理解“c++ 高精度猴子吃桃”这个问题的含义。但是,根据您提供的引用内容,我可以为您介绍一下C++高精度整数类以及为什么需要进行高精度运算。 C++高精度整数类是一种自定义的整数类型,可以处理比标准整数类型更大的整数。这种类型通常用于需要处理大量数字的计算机程序,例如密码学、大数据处理和科学计算等领域。C++高精度整数类支持四则运算、比大小、自增自减等操作,可以让程序员更加方便地进行高精度计算。 为什么需要进行高精度运算呢?因为C++中的一般运算变量类型(例如int、long long等)有大小限制,无法完成大数字的运算。例如,如果要计算两个非常大的整数的和,使用int类型可能会导致溢出,从而得到错误的结果。因此,需要使用高精度整数类来处理这些大数字。 下面是一个使用C++高精度整数类计算斐波那契数列的例子: ```c++ #include <iostream> #include <vector> using namespace std; class BigInteger { public: BigInteger() : digits(1, 0) {} BigInteger(int num) { while (num) { digits.push_back(num % 10); num /= 10; } } BigInteger(const string& str) { for (int i = str.size() - 1; i >= 0; i--) { digits.push_back(str[i] - '0'); } } BigInteger operator+(const BigInteger& other) const { BigInteger result; int carry = 0; for (int i = 0; i < max(digits.size(), other.digits.size()); i++) { int sum = carry; if (i < digits.size()) { sum += digits[i]; } if (i < other.digits.size()) { sum += other.digits[i]; } result.digits.push_back(sum % 10); carry = sum / 10; } if (carry) { result.digits.push_back(carry); } return result; } friend ostream& operator<<(ostream& os, const BigInteger& num) { for (int i = num.digits.size() - 1; i >= 0; i--) { os << num.digits[i]; } return os; } private: vector<int> digits; }; int main() { int n; cin >> n; BigInteger a = 0, b = 1; for (int i = 0; i < n; i++) { BigInteger c = a + b; a = b; b = c; } cout << a << endl; return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值