跳到正文

科技·工程

Bigint::bigint

SunnyFishQwQ约 13 分钟
原文
文章信息与版本

文章信息

发布
2025年10月2日
收录
2025年12月12日
更新
2026年11月10日
篇幅
约 13 分钟

版本

  1. v1当前
C++
#include <bits/stdc++.h>
using sz = long long;
#define elif else if
#define fori(na, st, ed) for (int(na) = st; (na) <= (ed); (na)++)
#define forn(na, ed) fori(na, 1, ed)
#define forit(tp, na, st, ed) for (tp(na) = st; (na) <= (ed); (na)++)
#define fornt(tp, na, ed) fori(tp, na, 1, ed)
class bigint {
  public:
	std::string __num;
	bool isNeg;
	bigint() : isNeg(false) {}

	explicit bigint(std::string _other)
		: isNeg(false), __num(std::move(_other)) {
		if (__num.empty() || __num == "-") {
			__num = "0";
			isNeg = false;
		}
		elif (__num[0] == '-') {
			isNeg = true;
			__num.erase(__num.begin());
		}
	}

	bigint(const bigint &_other) = default;

	explicit bigint(const char *_other) : isNeg(false), __num(_other) {
		if (__num.empty() || __num == "-") {
			__num = "0";
			isNeg = false;
		}
		elif (__num[0] == '-') {
			isNeg = true;
			__num.erase(__num.begin());
		}
	}

	int cmp(const bigint &_other) const {
		if (isNeg != _other.isNeg) {
			return isNeg ? -1 : 1;
		}
		std::string _othernum(_other.__num);
		if (__num.size() != _othernum.size()) {
			return (__num.size() > _othernum.size() ? 1 : -1) *
				   (isNeg ? -1 : 1);
		}
		// clang-format off
		forit(sz, i, 0, __num.size() - 1)
			if (__num[i] != _othernum[i]) {
				return (__num[i] > _othernum[i] ? 1 : -1) * (isNeg ? -1 : 1);
			}
		// clang-format on
		return 0;
	}

	bool operator<(const bigint &_other) const { return cmp(_other) < 0; }

	bool operator>(const bigint &_other) const { return cmp(_other) > 0; }

	bool operator<=(const bigint &_other) const { return cmp(_other) <= 0; }

	bool operator>=(const bigint &_other) const { return cmp(_other) >= 0; }

	bool operator==(const bigint &_other) const { return cmp(_other) == 0; }

	bool operator!=(const bigint &_other) const { return cmp(_other) != 0; }

	bigint &operator=(const std::string &_other) {
		__num = _other;
		isNeg = false;
		return *this;
	}

	bigint &operator=(const char *_other) {
		__num = _other;
		isNeg = false;
		return *this;
	}

	bigint &operator=(const bigint &_other) {
		if (this == &_other) {
			return *this;
		}
		__num = _other.__num;
		isNeg = _other.isNeg;
		return *this;
	};

	bigint operator+(const bigint &_other) const {
		if (isNeg != _other.isNeg) {
			bigint tmp = *this;
			tmp.isNeg = !tmp.isNeg;
			return _other - tmp;
		}
		std::string _tnum(__num), _othernum(_other.__num), _ans;
		while (_tnum.size() < _othernum.size()) {
			_tnum.insert(_tnum.begin(), '0');
		}
		while (_tnum.size() > _othernum.size()) {
			_othernum.insert(_othernum.begin(), '0');
		}
		sz len = _tnum.size();
		int jw = 0;
		for (sz i = len - 1; i >= 0; i--) {
			int t = _tnum[i] - '0' + _othernum[i] - '0' + jw;
			jw = t / 10;
			t %= 10;
			_ans.insert(_ans.begin(), char(t + '0'));
		}
		if (jw > 0) {
			_ans.insert(_ans.begin(), char(jw + '0'));
		}
		bigint R_ans(_ans);
		R_ans.isNeg = isNeg;
		return R_ans;
	}

	bigint operator-(const bigint &_other) const {
		if (isNeg != _other.isNeg) {
			bigint tmp = _other;
			tmp.isNeg = !tmp.isNeg;
			return *this + tmp;
		}
		if (*this < _other) {
			bigint result = _other - *this;
			result.isNeg = !isNeg;
			return result;
		}
		std::string _tnum(__num), _othernum(_other.__num), _ans;
		while (_tnum.size() < _othernum.size()) {
			_tnum.insert(_tnum.begin(), '0');
		}
		while (_tnum.size() > _othernum.size()) {
			_othernum.insert(_othernum.begin(), '0');
		}
		sz len = _tnum.size();
		int tw = 0;
		for (sz i = len - 1; i >= 0; i--) {
			int t = _tnum[i] - '0' - (_othernum[i] - '0') - tw;
			tw = t < 0 ? 1 : 0;
			t = t < 0 ? t + 10 : t;
			_ans.insert(_ans.begin(), char(t + '0'));
		}
		while (_ans.size() > 1 && _ans.front() == '0') {
			_ans.erase(_ans.begin());
		}
		bigint R_ans(_ans);
		R_ans.isNeg = isNeg;
		return R_ans;
	}

	bigint operator*(const bigint &_other) const {
		std::string _tnum(__num), _othernum(_other.__num);
		sz len = _tnum.size() + _othernum.size();
		std::string _ans(len, '0');
		forit(int, i, 0, _tnum.size() - 1) {
			int jw = 0;
			fori(j, 0, _othernum.size() - 1) {
				int t = (_tnum[_tnum.size() - 1 - i] - '0') *
							(_othernum[_othernum.size() - 1 - j] - '0') +
						jw + (_ans[len - 1 - (i + j)] - '0');
				jw = t / 10;
				t %= 10;
				_ans[len - 1 - (i + j)] = char(t + '0');
			}
			if (jw > 0) {
				_ans[len - 1 - (i + _othernum.size())] = char(jw + '0');
			}
		}
		while (_ans.size() > 1 && _ans.front() == '0') {
			_ans.erase(_ans.begin());
		}
		bigint R_ans(_ans);
		R_ans.isNeg = isNeg != _other.isNeg;
		return R_ans;
	}

	bigint operator/(const bigint &_other) const {
		if (_other.__num == "0") {
			throw std::runtime_error("Division by zero");
		}
		std::string _tnum(__num), _othernum(_other.__num);
		std::string _ans;
		bigint remainder("0");
		for (char ch : _tnum) {
			remainder = remainder * bigint("10") + bigint(std::string(1, ch));
			int count = 0;
			while (remainder >= _other) {
				remainder = remainder - _other;
				count++;
			}
			_ans += std::to_string(count);
		}
		while (_ans.size() > 1 && _ans.front() == '0') {
			_ans.erase(_ans.begin());
		}
		bigint R_ans(_ans);
		R_ans.isNeg = isNeg != _other.isNeg;
		return R_ans;
	}

	bigint operator%(const bigint &_other) const {
		if (_other.__num == "0") {
			throw std::runtime_error("Division by zero");
		}
		bigint remainder("0");
		for (char ch : __num) {
			remainder = remainder * bigint("10") + bigint(std::string(1, ch));
			while (remainder >= _other) {
				remainder = remainder - _other;
			}
		}
		remainder.isNeg = isNeg;
		return remainder;
	}

	bigint operator+=(const bigint &_other) {
		*this = *this + _other;
		return *this;
	}

	bigint operator-=(const bigint &_other) {
		*this = *this - _other;
		return *this;
	}

	bigint operator*=(const bigint &_other) {
		*this = *this * _other;
		return *this;
	}

	bigint operator/=(const bigint &_other) {
		*this = *this / _other;
		return *this;
	}

	bigint operator%=(const bigint &_other) {
		*this = *this % _other;
		return *this;
	}

	bigint operator++() {
		*this = *this + bigint("1");
		return *this;
	}

	bigint operator--() {
		*this = *this - bigint("1");
		return *this;
	}
};
inline std::ostream &operator<<(std::ostream &os, const bigint &_other) {
	if (_other.isNeg) {
		os << "-";
	}
	os << _other.__num;
	return os;
}

inline std::istream &operator>>(std::istream &is, bigint &_other) {
	std::string _tnum;
	is >> _tnum;
	_other = bigint(_tnum);
	return is;
}
原文