C++定义一个类实现大数运算

这是从洛谷一篇题解找到的

原作者(洛谷):baqkqpfhv

这篇题解链接:登录 - 洛谷https://www.luogu.com.cn/problem/solution/P1932这里面定义了大数运算的加减乘除模,用起来很方便,代码如下:

class mine
{
public :
    //constructor
    mine(long long = 0);
    mine(const string &);
    mine(const char *str) { *this = string(str); }

    //assignment operators
    mine &operator=(long long num) { return *this = mine(num); }
    mine &operator=(const string &str) { return *this = mine(str); }
    mine &operator=(const char *str) { return *this = mine(str); }

    //relatiional operators
    bool operator<(const mine &obj) const { return cmp(obj) < 0; }
    bool operator>(const mine &obj) const { return cmp(obj) > 0; }
    bool operator<=(const mine &obj) const { return cmp(obj) <= 0; }
    bool operator>=(const mine &obj) const { return cmp(obj) >= 0; }
    bool operator==(const mine &obj) const { return cmp(obj) == 0; }
    bool operator!=(const mine &obj) const { return cmp(obj) != 0; }

    //arithmetic operators
    mine operator+() const { return *this; }
    mine operator-() const { return mine(-sign_, val_); }
    mine operator+(const mine &) const;
    mine operator-(const mine &) const;
    mine operator*(const mine &) const;
    mine operator/(const mine &) const;
    mine operator%(const mine &) const;

    //compound assignment operators
    mine &operator+=(const mine &obj) { return *this = *this + obj; }
    mine &operator-=(const mine &obj) { return *this = *this - obj; }
    mine &operator*=(const mine &obj) { return *this = *this * obj; }
    mine &operator/=(const mine &obj) { return *this = *this / obj; }
    mine &operator%=(const mine &obj) { return *this = *this % obj; }

    //increment and decrement operators
    mine &operator++() { return *this += 1; }
    mine &operator--() { return *this -= 1; }
    mine operator++(int);
    mine operator--(int);

    //input and output
    friend istream &operator>>(istream &, mine &);
    friend ostream &operator<<(ostream &, const mine &);

protected :
    enum div_type { division, remainder };
    enum cmp_type { with_sign, without_sign };
    static const int base_ = (int)1e4;
    static const int width_ = 4;
    mine(int s, const vector<int> &v) : sign_(s), val_(v) {}
    int cmp(const mine &, cmp_type = with_sign) const;
    mine &delZero();
    mine &add(const mine &);
    mine &sub(const mine &);
    mine &mul(const mine &, const mine &);
    mine &div(mine &, mine, div_type = division);

private :
    int sign_;
    vector<int> val_;
};

mine::                      mine(long long num) : sign_(0)
{
    if (num < 0) sign_ = -1, num = -num;
    else if (num > 0) sign_ = 1;
    do
    {
        val_.push_back(num % base_);
        num /= base_;
    } while (num);
}

mine::mine(const string &str)
{
    sign_ = str[0] == '-' ? -1 : 1;
    int be = str[0] == '-' ? 1 : 0, en = str.size();
    while ((en -= width_) >= be)
    {
        stringstream ss(str.substr(en, width_));
        int temp;
        ss >> temp;
        val_.push_back(temp);
    }
    if ((en += width_) > be)
    {
        stringstream ss(str.substr(be, en - be));
        int temp;
        ss >> temp;
        val_.push_back(temp);
    }
    delZero();
}

mine mine::operator+(const mine &obj) const
{
    if (sign_ * obj.sign_ == 1)
    {
        mine temp;
        return cmp(obj, without_sign) >= 0 ? (temp = *this).add(obj) : (temp = obj).add(*this);
    }
    else if (sign_ * obj.sign_ == -1) return *this - -obj;
    else return sign_ == 0 ? obj : *this;
}

mine mine::operator-(const mine &obj) const
{
    if (sign_ * obj.sign_ == 1)
    {
        mine temp;
        return cmp(obj, without_sign) >= 0 ? (temp = *this).sub(obj) : (temp = -obj).sub(*this);
    }
    else if (sign_ * obj.sign_ == -1) return *this + -obj;
    else return sign_ == 0 ? -obj : *this;
}

inline mine mine::operator*(const mine &obj) const
{
    mine temp;
    return (temp.sign_ = sign_ * obj.sign_) == 0 ? temp : temp.mul(*this, obj);
}

inline mine mine::operator/(const mine &obj) const
{
    mine temp, mod = *this;
    return cmp(obj, without_sign) < 0 || (temp.sign_ = sign_ * obj.sign_) == 0 ? temp : temp.div(mod, obj);
}

inline mine mine::operator%(const mine &obj) const
{
    mine temp, mod = *this;
    return cmp(obj, without_sign) < 0 || (temp.sign_ = sign_) == 0 ? mod : temp.div(mod, obj, remainder);
}

inline mine mine::operator++(int)
{
    mine temp = *this;
    ++*this;
    return temp;
}

inline mine mine::operator--(int)
{
    mine temp = *this;
    --*this;
    return temp;
}

inline istream &operator>>(istream &in, mine &obj)
{
    string str;
    if (in >> str) obj = str;
    return in;
}

ostream &operator<<(ostream &out, const mine &obj)
{
    if (obj.sign_ == -1) cout << '-';
    out << obj.val_.back();
    for (int i = obj.val_.size() - 2; i >= 0; i--)
        out << setw(mine::width_) << setfill('0') << obj.val_[i];
    return out;
}

int mine::cmp(const mine &obj, cmp_type typ) const
{
    if (typ == with_sign && sign_ != obj.sign_) return sign_ - obj.sign_;
    int sign = typ == with_sign ? sign_ : 1;
    if (val_.size() != obj.val_.size()) return sign * (val_.size() - obj.val_.size());
    for (int i = val_.size() - 1; i >= 0; i--)
        if (val_[i] != obj.val_[i]) return sign * (val_[i] - obj.val_[i]);
    return 0;
}

inline mine &mine::delZero()
{
    while(val_.back() == 0 && val_.size() > 1) val_.pop_back();
    if (val_.back() == 0) sign_ = 0;
    return *this;
}

mine &mine::add(const mine &obj)
{
    int ts = val_.size(), os = obj.val_.size();
    for (int i = 0; i < os; i++) val_[i] += obj.val_[i];
    val_.push_back(0);
    for (int i = 0; i < ts; i++)
        if (val_[i] >= base_) val_[i] -= base_, ++val_[i + 1];
    return delZero();
}

mine &mine::sub(const mine &obj)
{
    int pos = obj.val_.size();
    for (int i = 0; i < pos; i++)
        if ((val_[i] -= obj.val_[i]) < 0) val_[i] += base_, --val_[i + 1];
    while (val_[pos] < 0) val_[pos] += base_, --val_[++pos];
    return delZero();
}

mine &mine::mul(const mine &a, const mine &b)
{
    int as = a.val_.size(), bs = b.val_.size();
    val_.resize(as + bs);
    for (int i = 0; i < as; i++) for (int j = 0; j < bs; j++)
    {
        int x = i + j;
        val_[x] += a.val_[i] * b.val_[j];
        val_[x + 1] += val_[x] / base_;
        val_[x] %= base_;
    }
    return delZero();
}

mine &mine::div(mine &a, mine b, div_type typ)
{
    int move = a.val_.size() - b.val_.size();
    val_.resize(move + 1);
    b.val_.insert(b.val_.begin(), move, 0);
    for (int i = move; i >= 0; i--)
    {
        int left = 0, right = base_;
        while (left + 1 < right)
        {
            int mid = (left + right) >> 1;
            if (a.cmp(b * mine(mid), without_sign) >= 0) left = mid;
            else right = mid;
        }
        val_[i] = left;
        a.sub(b * mine(left));
        b.val_.erase(b.val_.begin());
    }
    return typ == division ? delZero() : a;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值