高精度整数机试模板

机试中会有一类题目,整数的数值非常大,特别是什么什么的指数,会特别大,这个时候不能使用任何的内置的整数类型来保存,我们就需要用高精度的整数的模板,去定义一个BigInteger的结构体。

#include <string.h>
#include <stdio.h>
#include <iostream>
using namespace std;

const int maxn = 10000;

struct BigInteger {
	int digit[maxn];
	int length;
	BigInteger();
	BigInteger(int x);
	BigInteger(string str);
	BigInteger(const BigInteger& b);
	BigInteger operator=(int x);
	BigInteger operator=(string str);
	BigInteger operator=(const BigInteger& b);
	bool operator<=(const BigInteger& b);
	bool operator==(const BigInteger& b);
	BigInteger operator+(const BigInteger& b);
	BigInteger operator-(const BigInteger& b);
	BigInteger operator*(const BigInteger& b);
	BigInteger operator/(const BigInteger& b);
	BigInteger operator%(const BigInteger& b);
	friend istream& operator >>(istream& in, BigInteger x);
	friend ostream& operator <<(ostream& out, BigInteger x);
};

istream& operator>>(istream& in, BigInteger& x)
{
	string str;
	in >> str;
	x = str;
	return in;
}

ostream& operator<<(ostream& out, BigInteger& x)
{
	for (int i = x.length - 1; i >= 0; i--)
	{
		out << x.digit[i];
	}
}

BigInteger::BigInteger()
{
	fill(digit, digit + maxn, 0);
	length = 0;
}

BigInteger::BigInteger(int x)
{
	fill(digit, digit + maxn, 0);
	length = 0;
	if (x == 0) {
		digit[length++] = 0;
	}
	while (x != 0) {
		digit[length++] = x % 10;
		x /= 10;
	}
}

BigInteger::BigInteger(string str)
{
	fill(digit, digit + maxn, 0);
	length = 0;
	for (int i = 0; i < str.size(); i++)
	{
		digit[i] = str[str.size() - i - 1] - '0';
	}
}

BigInteger::BigInteger(const BigInteger& b)
{
	fill(digit, digit + maxn, 0);
	length = b.length;
	for (int i = 0; i < length;i++)
	{
		digit[i] = b.digit[i];
	}
}

BigInteger BigInteger::operator=(int x)
{
	fill(digit, digit + maxn, 0);
	length = 0;
	if (x == 0)
		digit[length++] = 0;
	while (x != 0)
	{
		digit[length++] = x % 10;
		x /= 10;
	}
	return *this;
}

BigInteger BigInteger::operator=(string str)
{
	fill(digit, digit + maxn, 0);
	length = str.size();
	for (int i = 0; i < length; i++)
	{
		digit[i] = str[length - i - 1] - '0';
	}
	return *this;
}

BigInteger BigInteger::operator=(const BigInteger& b)
{
	fill(digit, digit + maxn, 0);
	length = b.length;
	for (int i = 0; i < length; i++)
	{
		digit[i] = b.digit[i];
	}
	return *this;
}

bool BigInteger::operator<=(const BigInteger& b)
{
	if (length < b.length) {
		return true;
	}
	else if (length > b.length) {
		return false;
	}
	else {
		for (int i = length - 1; i >= 0; i--) {
			if (digit[i] == b.digit[i])
				continue;
			else {
				return digit[i] < b.digit[i];
			}
		}
	}
	return true;
}

bool BigInteger::operator==(const BigInteger& b)
{
	if (length != b.length) {
		return false;
	}
	else {
		for (int i = 0; i < length; i++) {
			if (digit[i] != b.digit[i])
				return false;
		}
	}
	return true;
}

BigInteger BigInteger::operator+(const BigInteger& b)
{
	BigInteger answer;
	int carry = 0;
	for (int i = 0; i < length || i < b.length; i++)
	{
		int current = carry + digit[i] + b.digit[i];
		carry = current / 10;
		answer.digit[answer.length++] = current % 10;
	}
	if (carry != 0)
	{
		answer.digit[answer.length++] = carry;
	}
	return answer;
}

BigInteger BigInteger::operator-(const BigInteger& b)
{
	BigInteger answer = 0;
	int carry = 0;
	//正整数的运算,所以已经默认了当前这个数字一定更大
	for (int i = 0; i < length; i++)
	{
		int current = digit[i] - b.digit[i] - carry;
		if (current < 0)
		{
			carry = 1;
			current += 10;
		}
		else {
			carry = 0;
		}
		answer.digit[answer.length++] = current;
	}
	//去掉高位的0
	while (answer.digit[answer.length - 1] == 0 && answer.length > 1) {
		answer.length--;
	}
}

BigInteger BigInteger::operator*(const BigInteger& b)
{
	BigInteger answer;
	answer.length = length + b.length;
	for (int i = 0; i < length; i++)
	{
		for (int j = 0; j < b.length; j++)
		{
			answer.digit[i + j] = digit[i] * b.digit[j];
		}
	}
	for (int i = 0; i < answer.length; i++) {
		answer.digit[i + 1] = answer.digit[i] / 10;
		answer.digit[i] %= 10;
	}
	while (answer.digit[answer.length - 1] == 0 && answer.length > 0)
	{
		answer.length--;
	}
	return answer;
}

BigInteger BigInteger::operator/(const BigInteger& b)
{
	BigInteger answer;
	answer.length = length;
	BigInteger remainder;
	BigInteger temp = b;
	for (int i = length - 1; i >= 0; i--) {
		if (!(remainder.length == 1 && remainder.digit[0] == 0))
		{
			for (int j = remainder.length - 1; j >= 0; j--) {
				remainder.digit[j + 1] = remainder.digit[j];
			}
			remainder.length++;
		}
		remainder.digit[0] = digit[i];
		while (temp <= remainder)
		{
			remainder = remainder - temp;
			answer.digit[i]++;
		}
	}
	while (answer.digit[answer.length - 1] == 0 && answer.length > 1) {
		answer.length--;
	}
	return answer;
}

BigInteger BigInteger::operator%(const BigInteger& b)
{
	BigInteger remainder = 0;
	BigInteger temp = b;
	for (int i = length - 1; i >= 0; --i)
	{
		if (!(remainder.length == 1 && remainder.digit[0] == 0))
		{
			for (int j = remainder.length - 1; j >= 0; j--)
			{
				remainder.digit[j + 1] = remainder.digit[j];
			}
			remainder.length++;
		}
		remainder.digit[0] = digit[i];
		while (temp <= remainder)
		{
			remainder = remainder - temp;
		}
	}
	return remainder;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值