各种模板 - 持续更新中···

无限长大数(理论)

class BigInt
{
   
public:
	BigInt():num(), negative(false){
   }
	BigInt(const LLT);
	BigInt(const char*);
	BigInt(const string);
	BigInt(const BigInt & x);
	BigInt & operator = (const BigInt &);
	friend istream & operator >> (istream &, BigInt &);
	friend ostream & operator << (ostream &, BigInt);
	const BigInt operator + (const BigInt &) const;
	const BigInt operator - (const BigInt &) const;
	const BigInt operator * (const BigInt &) const;
	const BigInt operator / (const LLT &) const;
	const LLT operator % (const LLT &) const;
	bool operator > (const BigInt &) const;
	bool operator < (const BigInt &) const;
	bool operator == (const BigInt &) const;
	bool operator >= (const BigInt &) const;
	bool operator <= (const BigInt &) const;
	friend const BigInt abs(const BigInt &);
	const BigInt operator - () const;
private:
	deque<int> num;
	bool negative;
};

BigInt::BigInt(const LLT x){
   
	LLT t = abs(x);
	negative = x >= 0 ? false : true;
	while (t > 0){
   
		num.push_back(t % 10);
		t /= 10;
	}
}
BigInt::BigInt(const char* str){
   
	unsigned i = str[0] == '-' ? 1 : 0;
	this->negative = (str[0] == '-' ? true : false);
	for (; i < strlen(str); ++i) num.push_back(str[i] - '0');
}
BigInt::BigInt(const string str){
   
	unsigned i = str[0] == '-' ? 1 : 0;
	this->negative = (str[0] == '-' ? true : false);
	for (; i < str.size(); ++i) num.push_back(str[i] - '0');
}
BigInt::BigInt(const BigInt &x):num(x.num), negative(x.negative){
   }
BigInt & BigInt::operator = (const BigInt &x){
   
	negative = x.negative;
	num = x.num;
	return (*this);
}
istream & operator >> (istream &is, BigInt & x){
   
	string str; is >> str;
	x = str;
	return is;
}
ostream & operator << (ostream &os, BigInt x){
   
	if (x.negative) os << '-';
	for (unsigned i = 0; i != x.num.size(); ++i)
		os << x.num[i];
	return os;
}
bool BigInt::operator > (const BigInt & rhs) const {
   
	BigInt x = (*this), y = rhs;
	if (!x.negative && y.negative) return true;
	if (x.negative && !y.negative) return false;
	if (x.negative && y.negative) swap(x, y);
	if (x.num.size() > y.num.size()) return true;
	if (x.num.size() < y.num.size()) return false;
	for (unsigned i = 0; i != x.num.size(); ++i) {
   
		if (x.num[i] > y.num[i]) return true;
		if (x.num[i] < y.num[i]) return false;
	}
	return false;
}
bool BigInt::operator < (const BigInt & rhs) const {
   
    return rhs < *this;
}
bool BigInt::operator == (const BigInt & rhs) const {
   
	return negative == rhs.negative && num == rhs.num;
}
bool BigInt::operator >= (const BigInt & rhs) const {
   
	return *this > rhs || *this == rhs;
}
bool BigInt::operator <= (const BigInt & rhs) const {
   
    return rhs >= *this;
}
const BigInt abs(const BigInt & rhs){
   
	BigInt res;
	res.negative = false;
	res.num = rhs.num;
	return res;
}
const BigInt BigInt::operator - () const {
   
	BigInt ret = *this; ret.negative = !ret.negative;
	return ret;
}
const BigInt BigInt::operator + (const BigInt & y) const {
   
	if (!this->negative && y.negative) return *this - abs(y);
	if (this->negative && !y.negative) return y - abs(*this);
	if (this->negative && y.negative) return -(abs(*this) + abs(y));
	BigInt x = *this, res;
	int temp = 0;
	for (int i = x.num.size() - 1, j = y.num.size() - 1; i >= 0 || j >= 0; --i, --j) {
   
		int a = i < 0 ? 0 : x.num[i];
		int b = j < 0 ? 0 : y.num[j];
		res.num.push_front((a + b + temp) % 10);
		temp = (a + b + temp) / 10;
	}
	if (temp != 0) res.num.push_front(temp);
	return res;
}
const BigInt BigInt::operator * (const BigInt & y) const {
   
	deque<int> a, b, res;
	copy(this->num.begin(), this->num.end(), front_inserter(a));
	copy(y.num.begin(), y.num.end(), front_inserter(b));
	res.resize(a.size() + b.size() + 5);
	for (unsigned i = 0; i < a.size(); ++i) for (unsigned j = 0; j < b.size(); ++j)
		res[i + j] += a[i] * b[j];
	for (unsigned i = 0; i < res.size() - 1; ++i){
   
		res[i + 1] += res[i] / 10;
		res[i] %= 10;
	}
	while (res.size() >= 2 && res.back() == 0)
		res.pop_back();
	reverse(res.begin(), res.end());
	BigInt ret; ret.negative = this->negative ^ y.negative; ret.num = res;
	return ret;
}
const BigInt BigInt::operator - (const BigInt & y) const {
   
	if (!this->negative && y.negative) return *this + abs(y);
	if (this->negative && !y.negative) return -(abs(*this) + y);
	if (this->negative && y.negative) return abs(y) - abs(*this);
	deque<int> a, b, res; BigInt ret;
	copy(this->num.begin(), this->num.end(), front_inserter(a));
	copy(y.num.begin(), y.num.end(), front_inserter(b));
	if (y > 
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值