实现大整数类别(C++)

使用数组来实现非负大整数类,即每WIDTH位数字保存成数组中的一个元素。

/* 实现大整数类 */
#include<vector>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<iostream>

using namespace std;

struct BigInteger { // 非负大整数
	static const int BASE = 1e+8;  // 每8位数字单独保存
	static const int WIDTH = 8;

	vector<int> s; // 每8位存储一次,数字的低位在前,高位在后
	// 构造函数
	BigInteger(long long num = 0) { *this = num; } // 已经重载了赋值运算
	BigInteger operator = (long long num) { // 赋值运算
		s.clear();
		do {
			s.push_back(num % BASE);
			num /= BASE;
		} while (num > 0);
		return *this;
	}
	BigInteger operator = (const string& str) {
		s.clear();
		int len = str.length();
		int cnt = (len - 1) / WIDTH + 1; // 表示有几个周期
		for (int i = 0; i < cnt; i++) {
			int end = len - i * WIDTH;
			int start = max(0, end - WIDTH);
			string tmp = str.substr(start, end - start);
			s.push_back(stoi(tmp));
		}
		return *this;
	}
	// 重载输入输出
	friend ostream& operator << (ostream& out, BigInteger& x) {
		out << x.s.back();
		for (int i = x.s.size() - 2; i >= 0; i--) {
			char buf[20];
			sprintf(buf, "%08d", x.s[i]); // 不足8位补0
			for (int j = 0; j < strlen(buf); j++) out << buf[j];
		}
		return out;
	}
	friend istream& operator >> (istream& in, BigInteger& x) {
		string tmp;
		if (!(in >> tmp)) return in;
		x = tmp;
		return in;
	}
	// 比较运算,只支持非负整数比较
	bool operator < (const BigInteger& b) const {
		if (s.size() != b.s.size()) return s.size() < b.s.size(); // 不包含前导0
		for (int i = s.size() - 1; i >= 0; i--) {
			if (s[i] != b.s[i]) return s[i] < b.s[i];
		}
		return 0; // 相等
	}
	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;
	}
	bool operator == (const BigInteger& b) {
		return !(*this < b) && !(b < *this);
	}
	bool operator != (const BigInteger& b) {
		return (*this < b) || (b < *this);
	}
	// 四则运算
	// 加法
	BigInteger operator + (const BigInteger& b) const {
		BigInteger ans;
		ans.s.clear();
		int g = 0; // 进位
		for (int i = 0;; i++) {
			// g == 0 是防止最后加和溢出了
			if (g == 0 && i >= this->s.size() && i >= b.s.size()) break;
			if (i < this->s.size()) g += this->s[i];
			if (i < b.s.size()) g += b.s[i];
			ans.s.push_back(g % BASE);
			g /= BASE;
		}
		return ans;
	}
	BigInteger operator += (const BigInteger& b) {
		return *this = *this + b;
	}
	// 只支持大减小,结果未非负整数
	BigInteger operator - (const BigInteger& b) {
		BigInteger ans;
		ans.s.clear();
		if (*this < b) {
			ans.s.push_back(0);
			return ans;
		}
		int g = 0; // 借位
		for (int i = 0; i < s.size(); i++) {
			int x = g;
			x += s[i];
			if (i < b.s.size()) x -= b.s[i];
			if (x < 0) {
				g = -1;
				x += BASE;
			}
			else g = 0;
			ans.s.push_back(x);
		}
		// 删除前导0,留一位,防止结果为0
		for (int i = ans.s.size() - 1; i >= 1; i--) {
			if (ans.s[i]) break;
			ans.s.pop_back();
		}
		return ans;
	}
	// 乘法、除法暂未实现
	BigInteger operator * (const BigInteger& b) {
		BigInteger ans; ans.s.clear();
		return ans;
	}
	BigInteger operator / (const BigInteger& b) const {
		BigInteger ans; ans.s.clear();
		return ans;
	}
};

int main() {
	BigInteger num1 = 123456789067;
	BigInteger num2 = 123456789012;
	BigInteger res = num1 - num2;
	cout << (res) << endl;
	BigInteger nums[5] = {12345,2,5,8,5678 };
	sort(nums, nums + 5);
	for (int i = 0; i < 5; i++) cout << nums[i] << " ";
	cout << endl;

	return 0;
}

注意:
只支持非负整数,而且乘法和除法并未实现。
乘法可以用一个大整数逐位与另一个整数整体相乘再求和。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值