Java和Pathon可以不用往下看了

C++的基本数据类型中,范围最大的数据类型不同编译器不同,但是最大的整数范围只有[-2^63—2^63-1](对应8个字节所对应的二进制数大小)。但是对于某些需要更大数据的问题来说,C++的基本数据类型就不够用了。因此大整数类应运而生。

大整数类实现的基本思路:使用数组来储存数字的每一位,然后用类似于手写“+-×÷”竖式的过程来实现大整数类的加减乘除运算。注意每一个数在数组中都是高位在后,低位在前这样便于进行运算。

代码如下:

详细的东西都在注释中写出来了

函数的接口:因为是string类型的,所以在初始化的时候为Bright(“1”),注意要加双引号。

 1 #include <iostream>
2 #include <cstring>
3 #include <queue>
4 using namespace std;
5 const int maxn = 100;//根据要求定
6 struct Bright {
7 int len, a[maxn] = { 0 };//数组的初始化很重要
8 string x;
9 Bright(string x = "0")//初始化函数
10 {
11 len = x.length();
12 for (int i = len - 1, j = 1; i >= 0; j++, i--)
13 a[j] = x[i] - '0';
14 while (!a[len] && len > 0)//确定位数
15 len--;
16 }
17 int& operator[](int i)//重新定义[]使程序写起来更为方便,int i 中的i表示[]中的值
18 {
19 return a[i];
20 }
21 void print()//打印出值
22 {
23 for (int i = max(len, 1); i >= 1; i--)
24 cout << a[i];
25 }
26 void zhanpin(int len1)//将数组中所有的数变为一位数(展平)
27 {
28 len = len1;
29 for (int i = 1; i < len; i++)
30 {
31 a[i + 1] += a[i] / 10;
32 a[i] = a[i] % 10;
33 }
34 while (!a[len])//确定位数
35 len--;
36 }
37 };
38
39 Bright operator+(Bright a, Bright b)//高精度+高精度
40 {
41 Bright c;
42 int len = max(a.len, b.len);
43 for (int i = 1; i <= len; i++)
44 c[i] += a[i] + b[i];//计算贡献
45 c.zhanpin(len + 1);
46 return c;
47 }
48
49 Bright operator*(Bright a, int b)//高精度*低精度
50 {
51 Bright c;
52 for (int i = 1; i <= a.len; i++)
53 c[i] = a[i] * b;//计算贡献
54 c.zhanpin(a.len + 11);
55 return c;
56 }
57 Bright operator*(Bright a, Bright b)//高精度*高精度
58 {
59 Bright c;
60 int len = a.len + b.len;
61 for (int i = 1; i <= a.len; i++)
62 for (int j = 1; j <= b.len; j++)
63 c[i + j - 1] += a[i] * b[j];//通过竖式计算可以发现a的第i位以及b的第j位相乘是对c的第i+j-1位有贡献。
64 c.zhanpin(len);
65 return c;
66 }
67
68
69 int main()
70 {
71 string A, B;
72 cin >> A >> B;
73 Bright a(A), b(B), c;
74 c = a * b;//(a+b,a-b)
75 c.print();
76 }

下面附上另一种数组储存,不是string类型储存。

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = 100;//根据实际设置大小
struct Bright {
int len, a[maxn];
Bright(int x = 0)//初始化函数
{
memset(a, 0, sizeof(a));//这个初始化很重要,不要忘记了
for (len = 1; x; len++)//判断结束是以x的值来判断
a[len] = x % 10, x = x / 10;//在这里就已经倒置了
len--;
}
int& operator[](int i)//重新定义[]使程序写起来更为方便,int i 中的i表示[]中的值
{
return a[i];
}
void print()//打印出值
{
for (int i = max(len, 1); i >= 1; i--)
cout << a[i];
}
void zhanpin(int len1)//将数组中所有的数变为一位数
{
len = len1;
for (int i = 1; i < len; i++)
{
a[i + 1] += a[i] / 10;
a[i] = a[i] % 10;
}
while (!a[len])//确定位数
len--;
}
}; Bright operator+(Bright a, Bright b)//高精度+高精度
{
Bright c;
int len = max(a.len, b.len);
for (int i = 1; i <= len; i++)
c[i] += a[i] + b[i];//计算贡献
c.zhanpin(len + 1);
return c;
} Bright operator*(Bright a, int b)//高精度*低精度
{
Bright c;
for (int i = 1; i <= a.len; i++)
c[i] = a[i] * b;//计算贡献
c.zhanpin(a.len + 11);
return c;
}
Bright operator*(Bright a, Bright b)//高精度*高精度
{
Bright c;
int len = a.len + b.len;
for (int i = 1; i < a.len; i++)
for (int j = 1; j < b.len; j++)
c[i + j - 1] += a[i] * b[j];
c.zhanpin(len);
return c;
}
//main函数(省略)

C++高精度计算(大整数类)的更多相关文章

  1. C# 基于大整数类的RSA算法实现(公钥加密私钥解密,私钥加密公钥解密)

    但是C#自带的RSA算法类RSACryptoServiceProvider只支持公钥加密私钥解密,即数字证书的使用. 所以参考了一些网上的资料写了一个RSA的算法实现.算法实现是基于网上提供的一个大整 ...

  2. N!的阶乘附带简单大整数类的输入输出(暂时没有深入的了解)

    Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! 我的思路:就想着大整数类去了,才发现自己还不能很好的掌握,其实这是一个大 ...

  3. 大整数类BIGN的设计与实现 C++高精度模板

    首先感谢刘汝佳所著的<算法竞赛入门经典>. 众所周知,C++中储存能力最大的unsigned long long 也是有着一个上限,如果我们想计算非常大的整数时,就不知所措了,所以,我写了 ...

  4. 用Java的大整数类BigInteger来实现大整数的一些运算

    关于BigInteger的构造函数,一般会用到两个: BigInteger(String val); //将指定字符串转换为十进制表示形式: BigInteger(String val,int rad ...

  5. [ C++ 快速高精度模板 ] [ BigN类 ] 大整数类 高精度 模板 BigInt FFT 快速傅里叶变换

    [原创 转载请注明]瞎写的,如果代码有错,或者各位大佬有什么意见建议,望不吝赐教 更新日志: 对于规模较小的整数乘法使用$$O(n^2)$$方法,提高速度 modify()和operator[]的bu ...

  6. C++ BigInteger 大整数类模板(转)

    #include <deque> #include <vector> #include <iostream> #include <string> #in ...

  7. C++大整数类模板

    参考 :http://172.21.85.56/oj/resource/reportdetail?report_id=1678 支持 =.abs().pow().+=.-= *=./=.%=.+.-. ...

  8. 【Java编程】Java中的大整数计算

    在上一篇文章中,我们实现了c语言中的大整数的运算,并且用Miller-Rabin算法实现了对大素数的测试.本来我准备用Java代码实现大整数的运算,查了一下资料发现Java中java.math的Big ...

  9. Ural 1158. Censored! 有限状态自动机+DP+大整数

    Ural1158 看上去很困难的一道题. 原文地址 http://blog.csdn.net/prolightsfxjh/article/details/54729646 题意:给出n个不同的字符,用 ...

随机推荐

  1. 设计模式(十八)——观察者模式(JDK Observable源码分析)

    1 天气预报项目需求,具体要求如下: 1) 气象站可以将每天测量到的温度,湿度,气压等等以公告的形式发布出去(比如发布到自己的网站或第三方). 2) 需要设计开放型 API,便于其他第三方也能接入气象 ...

  2. MyBatis中传参时为什么要用#{}

    MyBatis中传参时为什么要用#{},这个问题和MyBatis如何防止SQL注入类似.不过在解释这个问题之前,先解释一下什么是SQL注入,还有些称作注入攻击这个问题. SQL注入就是SQL 对传入参 ...

  3. RT-Thread学习笔记1-启动顺序与线程创建

    目录 1. 启动顺序 2. 堆范围 3. 线程创建 3.1 线程代码(入口函数) 3.2 线程控制块 3.3 线程栈 4. 系统滴答时钟 5. GPIO驱动架构操作IO 6. 线程优先级 & ...

  4. 使用SQL-Server分区表功能提高数据库的读写性能

    首先祝大家新年快乐,身体健康,万事如意. 一般来说一个系统最先出现瓶颈的点很可能是数据库.比如我们的生产系统并发量很高在跑一段时间后,数据库中某些表的数据量会越来越大.海量的数据会严重影响数据库的读写 ...

  5. TS & ES-Next & playground

    TS & ES-Next & playground TS TypeScript: TS Playground - An online editor for exploring Type ...

  6. 使用 js 和 Beacon API 实现一个简易版的前端埋点监控 npm 包

    使用 js 和 Beacon API 实现一个简易版的前端埋点监控 npm 包 前端监控,埋点,数据收集,性能监控 Beacon API https://caniuse.com/beacon 优点,请 ...

  7. module patterns

    module patterns ebooks https://github.com/xyzata/2017-new-ebooks/blob/master/Succinctly/modulepatter ...

  8. how to fetch a group promise api in order with the returned resolved result

    how to fetch a group promise api in order with the returned resolved result promise 一组依次请求,generator ...

  9. Serverless & FaaS

    Serverless & FaaS Function as a Service 通过 Functions(一个事件驱动型无服务器计算平台,还可以解决复杂的业务流程问题)更加高效地进行开发; 在 ...

  10. qrcode & console.log

    qrcode & console.log image https://fs-api.lightyy.com/service/utils/qrcode?url=http://169.254.13 ...