HugeInt大整数类作业

//1.HugeInt.h
#ifndef HUGEINT_H
#define HUGEINT_H

#include<array>
#include<iostream>
#include<string>

class HugeInt
{
	friend std::ostream& operator<<(std::ostream&, const HugeInt&);
public:
	static const int digits = 30;
	HugeInt(long = 0);
	HugeInt(const std::string&);
	HugeInt operator+(const HugeInt&)const;
	HugeInt operator+(int)const;
	HugeInt operator+(const std::string&)const;

	HugeInt operator-(const HugeInt&)const;
	HugeInt operator-(int)const;
	HugeInt operator-(const std::string&)const;

	HugeInt operator*(const HugeInt&)const;
	HugeInt operator*(int)const;
	HugeInt operator*(const std::string&)const;

	HugeInt operator/(const HugeInt&)const;
	HugeInt operator/(int)const;
	HugeInt operator/(const std::string&)const;

	bool operator<(const HugeInt&)const;
	bool operator<(int)const;
	bool operator<(const std::string&)const;

	bool operator>(const HugeInt&)const;
	bool operator>(int)const;
	bool operator>(const std::string&)const;

	bool operator<=(const HugeInt&)const;
	bool operator<=(int)const;
	bool operator<=(const std::string&)const;

	bool operator>=(const HugeInt&)const;
	bool operator>=(int)const;
	bool operator>=(const std::string&)const;

	bool operator==(const HugeInt&)const;
	bool operator==(int)const;
	bool operator==(const std::string&)const;
private:
	std::array<short, digits>integer;
};
#endif // !HUGEINT_H
//2.HugeInt.cpp
#include<cctype>
#include"HugeInt.h"
using namespace std;

HugeInt::HugeInt(long value)
{
	for (short& element : integer)
		element = 0;
	for (size_t j = digits - 1;value != 0 && j >= 0;j--)
	{
		integer[j] = value % 10;
		value /= 10;
	}
}

HugeInt::HugeInt(const string& number)
{
	for (short& elements : integer)
		elements = 0;
	size_t length = number.size();
	for (size_t j = digits - length, k = 0;j < digits;++j, ++k)
		if (isdigit(number[k]))
			integer[j] = number[k] - '0';
}

HugeInt HugeInt::operator+(const HugeInt& op2)const
{
	HugeInt temp;
	int carry = 0;
	for (int i = digits - 1;i >= 0;i--)
	{
		temp.integer[i] = integer[i] + op2.integer[i] + carry;
		if (temp.integer[i] > 9)
		{
			temp.integer[i] %= 10;
			carry = 1;
		}
		else
			carry = 0;
	}
	return temp;
}

HugeInt HugeInt::operator+(int op2)const
{
	return *this + HugeInt(op2);
}

HugeInt HugeInt::operator+(const string& op2)const
{
	return *this + HugeInt(op2);
}

HugeInt HugeInt::operator-(const HugeInt& op2)const
{
	HugeInt temp;
	int carry = 0;
	for (int i = digits-1;i >= 0;i--)
	{
		temp.integer[i] = integer[i] - op2.integer[i] - carry;
		carry = 0;
		if (temp.integer[i] < 0)
		{
			temp.integer[i] += 10;
			carry = 1;
		}
	}
	return temp;
}

HugeInt HugeInt::operator-(int op2)const
{
	return *this - HugeInt(op2);
}

HugeInt HugeInt::operator-(const std::string& op2)const
{
	return *this - HugeInt(op2);
}

ostream& operator<<(ostream& output, const HugeInt& num)
{
	int i;
	for (i = 0;(i < HugeInt::digits) && (0 == num.integer[i]);++i)
		;
	if (i == HugeInt::digits)
		output << 0;
	else
		for (;i < HugeInt::digits;++i)
			output << num.integer[i];
	return output;
}

HugeInt HugeInt::operator*(const HugeInt& op2)const
{
	HugeInt temp(0);
	int carry = 0;
	for (int i = digits-1;i >= 0;i--)
	{
		for (int j = digits-1;j >=0;j--)
		{
			if(29-(29-i+29-j)<=digits-1&&0<= 29 - (29 - i + 29 - j))
				temp.integer[29-((29-i)+(29-j))] += (integer[i] * op2.integer[j]);
		}
	}
	for (int k = digits-1;k >= 0;k--)
	{
		temp.integer[k] += carry;
		if (temp.integer[k] > 9)
		{
			carry = temp.integer[k] / 10;
			temp.integer[k] %= 10;
		}
		else
			carry = 0;
	}
	return temp;
}

HugeInt HugeInt::operator*(int c)const
{
	HugeInt classC(c);
	return *this * classC;
}

HugeInt HugeInt::operator*(const std::string& c)const
{
	HugeInt classC(c);
	return *this * classC;
}

HugeInt HugeInt::operator/(const HugeInt& c)const
{
	HugeInt temp(0);
	HugeInt temp2(*this);

	for (int j = 1;j <= 10;j++)
	{
		HugeInt temp1(0);
		if (temp2 < c)
			break;
		if (temp2 < c * pow(10,j) )//必须是temp2<c*pow(10,j),否则会出现负数,导致程序炸掉
		{
			for (int p=1;p <= 9;p++)
			{
				if (temp2 < c * pow(10, j - 1) * p )
				{
					temp1 = temp2 - c * pow(10, j - 1)*(p-1);
					temp2 = temp1;
					temp.integer[29-j + 1] = p - 1;
					break;
				}
				if (temp2 == c * pow(10, j - 1) * p )
				{
					temp.integer[29-j + 1] = p ;
					return temp;
				}
			}
			j = 1;
		}
		if (temp2 == c * pow(10, j) )
		{
			temp.integer[29-j] = j;
			return temp;
		}
	}
	return temp;
}
HugeInt HugeInt::operator/(int op)const
{
	return *this / HugeInt(op);
}

HugeInt HugeInt::operator/(const std::string&op)const
{
	return *this / HugeInt(op);
}

bool HugeInt::operator<(const HugeInt& op)const
{
	for (int i = 0;i <= digits-1;i++)
	{
		if (integer[i] > op.integer[i])
		{
			return 0;
		}
		if (integer[i] < op.integer[i])
		{
			return 1;
		}
	}
	return 0;
}

bool HugeInt::operator<(int op)const
{
	return(*this < HugeInt(op));
}

bool HugeInt::operator<(const std::string& op)const
{
	return(*this < HugeInt(op));
}

bool HugeInt::operator>(const HugeInt& op)const
{
	for (int i = digits-1;i >=0 ;i)
	{
		if (integer[i] < op.integer[i])
			return 0;
		if (integer[i] > op.integer[i])
			return 1;
	}
	return 0;
}
bool HugeInt::operator>(int op)const
{
	return(*this > HugeInt(op));
}
bool HugeInt::operator>(const std::string& op)const
{
	return(*this > HugeInt(op));
}

bool HugeInt::operator<=(const HugeInt& op)const
{
	return((*this < op) || (*this == op));
}

bool HugeInt::operator<=(int op)const
{
	return((*this < op) || (*this == op));
}

bool HugeInt::operator<=(const std::string& op)const
{
	return((*this < op) || (*this == op));
}

bool HugeInt::operator>=(const HugeInt& op)const
{
	return((*this > op) || (*this == op));
}

bool HugeInt::operator>=(int op)const
{
	return((*this > op) || (*this == op));
}

bool HugeInt::operator>=(const std::string& op)const
{
	return((*this > op) || (*this == op));
}

bool HugeInt::operator==(const HugeInt& op)const
{
	bool flag = 1;
	for (int i = digits - 1;i >= 0;i--)
	{
		if (integer[i] != op.integer[i])
			flag = 0;
	}
	return flag;
}

bool HugeInt::operator==(int op)const
{
	return(*this == HugeInt(op));
}

bool HugeInt::operator==(const std::string& op)const
{
	return(*this == HugeInt(op));
}

//3.main.cpp
#include<iostream>
#include"Hugeint.h"
using namespace std;

int main()
{
	HugeInt n1(7654321);
	HugeInt n11(7654321);
	HugeInt n2(7891234);
	HugeInt n3("99999999999999999999999999999");
	HugeInt n4("1");
	HugeInt n5;

	cout << "n1 is " << n1 << "\nn2 is " << n2
		<< "\nn3 is " << n3 << "\nn4 is " << n4
		<< "\nn5 is " << n5 << "\n\n";

	n5 = n1 + n2;
	cout << n1 << "+" << n2 << "=" << n5 << "\n\n";

	cout << n3 << "+" << n4 << "\n=" << (n3 + n4) << "\n\n";

	n5 = n1 + 9;
	cout << n1 << "+" << 9 << "=" << n5 << "\n\n";

	n5 = n2 + "10000";
	cout << n2 << "+" << "10000" << "=" << n5 << endl;

	cout << "n2 - n1 = " << n2 - n1 << endl;
	cout << "n1 * 3 = " << n1 * 3 << endl;
	cout << "n2 / n1 = " << n2 / n1 << endl;

	cout << (n1 < n2) << endl;
	cout << (n1 > n2) << endl;

	HugeInt a1(900); HugeInt a2(45);HugeInt a3(90);HugeInt a4(1318902);
	cout << a1 / a2 << endl;
	cout << a1 / a3 << endl;
	cout << a1 / a4 << endl;

	return 0;
}

大概是一个初学者的作业存档

大整数类的乘法:

设a*b

则temp[i+j]=a[i]*b[j];

再将temp每一位判断是否有carry,再输出就行了。

大整数类的除法

这是全篇最难的地方,采用的方法是例如10000/45

则10000-4500*2=1000

商1=4500*2的那个2

商2=1000-450*2的那个2

商3=100-45*2的那个2

余10<45 over~

你看这个构造函数,integer是反着排的

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
HugeCalc 是一款高精度算法库(同时支持 MBCS + UNICODE 版),适合于大规模科学计算,尤其适用于数论、密码学等领域研究,其核心算法耗费作者十余年的心血。具有占用资源少、效率高、使用便捷、易二次开发、可移植性强、可扩展性好等特点。关键文件 HugeCalc.dll 虽然很小,却提供了公共函数接口 709 个(标准C++接口 473 个;标准C接口 236 个),且其计算速度完全可与大型专业数学工具软件媲美! 现已提供了如下功能: ⊙ 高精度快速加法 ⊙ 高精度快速减法 ⊙ 高精度快速乘法 ⊙ 高精度快速除法 ⊙ 高精度快速同余 ⊙ 高精度快速位运算 ⊙ 高精度快速乘方 ⊙ 高精度快速开方 ⊙ 超大整数快速取对数 ⊙ 高精度快速求排列 ⊙ 高精度快速求组合 ⊙ 高精度快速阶乘、双阶乘、素数阶乘 ⊙ 高精度快速计算 Fibonacci、Lucas 数列 ⊙ 高精度快速乘积取模 ⊙ 高精度快速数论倒数取模运算 ⊙ 高精度快速乘方取模(支持负指数) ⊙ 高精度快速求最大公约数(支持群组运算) ⊙ 高精度快速计算扩展最大公约数 ⊙ 高精度快速求最小公倍数(支持群组运算) ⊙ 高精度快速“等幂和”(支持群组运算) ⊙ 高精度快速任意进制转换 ⊙ 超大整数素性快速检测 ⊙ 生成随机超大(素)整数、快速生成最邻近素数 ⊙ 自由指定有效位运算 ⊙ 强大而灵活的输出 ⊙ 高精度计时器(有暂停、累计、复位等功能) 为了与广大网友分享 HugeCalc 带来的便捷,该版公开了 HugeCalc.dll 的所有接口文件(同时支持 MBCS + UNICODE 版),大家可以更自由地进行高精度计算或自开发,而无须再依赖于 Mathematica 等大型软件。 V6.x 新增了各种标准导入接口,可方便各种编程语言进行二次开发,如 C++、C、VB、Delphi 等。 V7.x 可自动侦测用户 CPU 的型号,并据此自动调整算法及相应参数,使在兼顾老式机器的前提下,可充分发挥现代及未来 CPU 的功效(如采用 SSE2 指令集、多核并行等)。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GeometryGEN

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值