大整数类BIgInteger的简单实现

在阅读算法竞赛入门经典时,书中提到了大整数类,并给出了实现,参照书上的代码简单试着实现了一下,直接看代码.

#include <cstdio>
#include <iostream>
#include <vector>
#include <cstring>
#include <set>
#include <map>
#include <algorithm>
using namespace std;
// c++中struct和class基本没有区别,最大的区别是默认权限和继承方式的不同,这里其实用class更好
struct BigInteger {
    // 书上采用的是数组一位存一个8位数
	static const int BASE = 100000000;
	static const int WIDTH = 8;
    // 存储大整数
	vector<int> s;
	// 构造函数
	BigInteger(long long num = 0) {
		*this = num;
	}
    // 重载'=',用于long long类型的赋值,这个用处不大
	BigInteger operator = (long long num) {
        // 赋值时,需要把原来的数清空
		s.clear();
		do {
            // 倒序储存,一个空最多存一个8位数
			s.push_back(num % BASE);
			num /= BASE;
		} while (num > 0);
		return *this;
	}
    // 如果数太大了,就要使用字符串来存储数
	BigInteger operator = (const string& str) {
        // 清空原来的数
		s.clear();
        // 需要几位的数组
		int x, len = (str.length() - 1) / WIDTH + 1;
		for (int i = 0; i < len; i++) {
            // 得到一次读取时的终点和起点
			int end = str.length() - i * WIDTH;
			int start = max(0, end - WIDTH);
            // substr,读取子字符串,.c_str(),把string转换为sscanf能识别的指针
			sscanf(str.substr(start, end - start).c_str(), "%d", &x);
			s.push_back(x);
		}
		return *this;
	}
	BigInteger operator + (const BigInteger& b) const {
		BigInteger c;
		c.s.clear();
		for (int i = 0, g = 0; ; i++) {
            // g为进位数
			if (g == 0 && i >= s.size() && i >= b.s.size()) {
				break;
			}
            // x为当前轮所累加的数
			int x = g;
			if (i < s.size()) {
				x += s[i];
			}
			if (i < b.s.size()) {
				x += b.s[i];
			}
            // 保证8位
			c.s.push_back(x % BASE);
			g = x / BASE;
		}
		return c;
	}
    // 重载'<',sort,set这些的比较就是基于'<'
	bool operator < (const BigInteger & b) const {
        // 不相等则返回长度比较结果
		if (s.size() != b.s.size()) {
			return s.size() < b.s.size();
		}
        // 相等则逐个判断大小,从大的往小的判断,因为是逆序储存的
		for (int i = s.size() - 1; i >= 0; i--) {
			if (s[i] != b.s[i]) {
				return s[i] < b.s[i];
			}
		}
        // 相等
		return false;
	}
};
// 重载'<<'需要到结构体定义外面
ostream& operator << (ostream &out, const BigInteger& x) {
     // 因为s的尾部就数的头部,可能不满8位,先输出
		out << x.s.back();
		for (int i = x.s.size() - 2; i >= 0; i--) {
			char buf[20];
            // 注意不足8位的,用0补足右边
			sprintf(buf, "%08d", x.s[i]);
			for (int j = 0; j < strlen(buf); j++) {
				out << buf[j];
			}
		}
		return out;
	}
istream& operator >> (istream& in, BigInteger& x) {
	string s;
	if (!(in >> s)) {
		return in;
	}
	x = s;
	return in;
}
int main() {
	BigInteger arr[10];
	arr[0] = 123123131;
	arr[1] = 1234567890;
	arr[2] = 12345678;
	arr[3] = 98765432198764356;
	arr[4] = 45753467574545344575344557;
	cin >> arr[5] >> arr[6];
	cout << arr[5] + arr[6] << endl;
	sort(arr, arr + 7);
	for (int i = 0; i < 7; i++) {
		cout << arr[i] << endl;
	}
	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值