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. AtCoder AIsing Programming Contest 2020 D - Anything Goes to Zero (二进制,模拟)

    题意:给你一个长度为\(n\)的\(01\)串,从高位到低位遍历,对该位取反,用得到的十进制数\(mod\)所有位上\(1\)的个数,不断循环直到为\(0\),输出每次遍历时循环的次数. 题解:根据题 ...

  2. MiniSMB 网络性能测试 免费版本安装指南

    1) 烧录Image至USB 在Linux环境下可以运行以下命令(假设usb设备符号为/dev/sdb): root# tar -Jxf minismb-free-edition.img.tar.xz ...

  3. Zabbix 触发器配置多监控项阈值

    配置内存自定义监控项 # 监控内存命令 [root@web01 ~]# free -m|awk '/^Mem/{print $NF/$2}' 0.664609 [root@web01 ~]# free ...

  4. 系统找不到C:\ProgramData\Oracle\Java\javapath\java.exe问题及解决方案

    一.问题由来 前一段时间本人的电脑崩溃了,系统还原之后,eclipse就用不了,也找不大原因.eclipse报错原因是jvm出现问题:JVM terminated Exit code=2 C:\Pro ...

  5. 多线程之ThreadLocal类

    深入研究java.lang.ThreadLocal类 0.前言 ThreadLocal(线程变量副本)Synchronized实现内存共享,ThreadLocal为每个线程维护一个本地变量.采用空间换 ...

  6. ysoserial Commons Collections2反序列化研究

    Apache Commons Collections2反序列化研究 环境准备 JDK 1.7 Commons Collections 4.0 javassit 前置知识 PriorityQueue() ...

  7. React Transforming Elements All In One

    React Transforming Elements All In One https://reactjs.org/docs/react-api.html#transforming-elements ...

  8. 市值达万亿?总量仅10万枚的VAST,先兑换先得!

    据了解,SPC第一轮.第二轮空投已经结束,两轮空投下来共发放了400万枚SPC.NGK所有算力持有者有效账户基本获得了SPC空投奖励,甚至有的NGK算力持有者获得了数千枚SPC. 而为了进一步奖励NG ...

  9. 10月上线的NGK global有怎样的发展前景?

    随着NGK global 10月份的上线时间将近,社区中也开始纷纷讨论.预测起NGK global上线后的表现,对此小编也有一些自己的理解,就此分享给大家. 在基于实体生态的赋能下,NGK globa ...

  10. WPF 数据绑定实例一

    前言: 数据绑定的基本步骤: (1)先声明一个类及其属性 (2)初始化类赋值 (3)在C#代码中把控件DataContext=对象: (4)在界面设计里,控件给要绑定的属性{Binding 绑定类的属 ...