项目概述
导入
在现代科学研究和技术开发中,高精度的数值计算是不可或缺的。特别是在物理、天文学、密码学等领域,精确的大数运算对于理论模型的验证和应用实现至关重要。我们要做的是利用该类,计算圆周率到数千甚至数万位的精度,展示其处理复杂数学问题的能力。
具体问题描述
32位整数的计算机可以表示整数的范围近似为-20亿到+20亿。在这个范围内操作一般不会出现问题,但是有的应用程序可能需要使用超出上述范围的整数。C++可以满足这个需求,创建功能强大的新的数据类型。根据后缀,定义一个大整数类,实现大整数的加、减、乘、除、幂、输出重载等,并能去计算pi的值。要求:能在两秒内算到1000位。
输入一个整数n,
输出圆周率的前n位数字(不包含小数点)
样例输入
3
样例输入
314
样例输入
1000
样例输出
3141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420198
想要实现这一功能,我们需要完成如下任务:
- 基本算术运算:实现大整数的加、减、乘、除等基本算术运算,支持超出常规整数范围的数值计算。
- 运算符重载:通过重载加法、减法、乘法和除法运算符,提供与普通整数类似的操作体验,使得大整数运算直观并易于集成到现有代码中。
- 性能优化:针对大数运算进行优化,减少计算时间和资源消耗,特别是在乘法和除法等复杂运算中寻找效率更高的算法。
运行环境及版本
- 使用软件:DEVC++ 5.11
- 设置环境
将gcc配置为32位的目的是为了防止中文乱码;编译时加入 -std=c++11是因为我的代码中包含C++11版本及以上才支持的特性
代码实现(结构清晰,结合注释理解)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class TeamWorkBigInt {
private:
static const int digits = 10000; // 大整数的位数
short integer[digits]; // 存储大整数的数组
public:
int sign = 1; // 大整数的符号,默认为正
// 构造函数,初始化大整数为0或给定的值
TeamWorkBigInt(long = 0);
// 重载加法运算符
friend TeamWorkBigInt operator+(const TeamWorkBigInt &, const TeamWorkBigInt &);
// 大整数加上一个整数
TeamWorkBigInt operator+(int) const;
// 重载减法运算符
TeamWorkBigInt operator-(const TeamWorkBigInt &) const;
// 重载比较运算符(大于)
int operator>(const TeamWorkBigInt &) const;
// 重载乘法运算符
TeamWorkBigInt operator*(const TeamWorkBigInt &) const;
// 大整数乘以一个整数
TeamWorkBigInt operator*(int) const;
// 大整数除以一个整数
TeamWorkBigInt operator/(int);
// 大整数除以另一个大整数
TeamWorkBigInt operator/(const TeamWorkBigInt &);
// 赋值运算符重载
TeamWorkBigInt &operator=(int);
// 大整数除以一个整数(改变自身)
TeamWorkBigInt &operator/=(int);
// 大整数加上另一个大整数(改变自身)
TeamWorkBigInt &operator+=(const TeamWorkBigInt &);
// 计算大整数的指数幂
friend TeamWorkBigInt pow(TeamWorkBigInt a, int b);
// 获取大整数的长度(位数)
int getLength() const;
// 构造函数,初始化为给定的整数
explicit TeamWorkBigInt(int i);
// 重载输出流运算符,用于打印大整数
friend std::ostream &operator<<(std::ostream &, const TeamWorkBigInt &);
};
// 构造函数实现
TeamWorkBigInt::TeamWorkBigInt(long value) {
for (short & i : integer)
i = 0;
int i = 0;
while (value != 0) {
integer[i] = value % 10;
value /= 10;
i++;
}
}
/