生活中,我们时常需要进行超大数的计算,可是系统所支持的计算范围却远不够这样的计算。这个时候我们就可以使用高精度。它的原理是:把数当做字符串读入,再把它们都变成数组,再用数组的每一个元素加减。有关如何编写头文件,请见:C++如何写自定义的头文件_钜铠的博客-CSDN博客 ,这里不再赘述。这次我们来一个实战,编写高精度头文件(大数加乘)——“clear.h”。
大数加法代码
话不多说,直接上代码。这是大数加法运算的一个代码:
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int A[1010], B[1010], sum[1010];
void s2BIG(string s, int a[])
{
a[0] = s.size();
for (int i = 1; i <= a[0]; i++)
{
a[i] = s[a[0] - i] - '0';
}
}
void addBIG(int x[], int y[], int z[])
{
z[0] = max(x[0], y[0]);
for (int i = 1; i <= z[0]; i++)
z[i] = x[i] + y[i];
for (int i = 1; i <= z[0]; i++)
{
z[i + 1] += z[i] / 10;
z[i] %= 10;
}
while (z[z[0] + 1] > 0) z[0]++;
}
void printBIG(int a[])
{
for (int i = a[0]; i >= 1; i--)
{
cout << a[i];
}
cout << endl;
}
int main()
{
string a, b;
cin >> a >> b;
s2BIG(a, A);
s2BIG(b, B);
addBIG(A, B, sum);
printBIG(sum);
return 0;
}
也有一个数是大数,一个数是普通数的可能(不过可以小数直接当做string用,但我们还是写得更好一些):
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int A[1010], B[1010], sum[1010];
void s2BIG(string s, int a[])
{
a[0] = s.size();
for (int i = 1; i <= a[0]; i++)
{
a[i] = s[a[0] - i] - '0';
}
}
void i2BIG(int n, int a[])
{
if (n == 0)
{
a[0] = 1;
return;
}
int cur = 0;
while (n > 0)
{
cur++;
a[cur] = n % 10;
n /= 10;
}
if (n == 0) cur++;
a[0] = cur;
}
void addBIG(int x[], int y[], int z[])
{
z[0] = max(x[0], y[0]);
for (int i = 1; i <= z[0]; i++)
z[i] = x[i] + y[i];
for (int i = 1; i <= z[0]; i++)
{
z[i + 1] += z[i] / 10;
z[i] %= 10;
}
while (z[z[0] + 1] > 0) z[0]++;
}
void printBIG(int a[])
{
for (int i = a[0]; i >= 1; i--)
{
cout << a[i];
}
cout << endl;
}
int main()
{
string x;
int y;
cin >> x >> y;
s2BIG(x, A);
i2BIG(y, B);
addBIG(A, B, sum);
printBIG(sum);
return 0;
}
大数乘法代码
大数乘法代码和大数加法差不多,只是把addBIG换成了mulBIG:
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int A[1010], B[1010], mul[1010];
void s2BIG(string s, int a[])
{
a[0] = s.size();
for (int i = 1; i <= a[0]; i++)
{
a[i] = s[a[0] - i] - '0';
}
}
void mulBIG(int x[], int y, int z[])
{
z[0] = x[0];
for (int i = 1; i <= z[0]; i++)
z[i] = x[i] * y;
for (int i = 1; i <= z[0]; i++)
{
z[i + 1] += z[i] / 10;
z[i] %= 10;
}
while (z[z[0] + 1] > 0)
{
z[0]++;
z[z[0] + 1] = z[z[0]] / 10;
z[z[0]] %= 10;
}
}
void printBIG(int a[])
{
for (int i = a[0]; i >= 1; i--)
{
cout << a[i];
}
cout << endl;
}
int main()
{
string a;
cin >> a;
int b;
cin >> b;
s2BIG(a, A);
mulBIG(A, b, mul);
printBIG(mul);
return 0;
}
框架
拿起框架,直接套:
#ifndef CLEAR_H
#define CLEAR_H
#include <iostream>
using namespace std;
#endif
完善的头文件
经过几步调整和完善,一个完整的头文件便做好了。未经许可,禁止转载。注意最后保存之后要修改扩展名为 .h 。如果要用,可以直接把头文件复制到系统头文件目录里(比如macOS和Linux就可以复制到/usr/local/include里),或者如果想调用,直接把它放在与主程序同一目录里。
#ifndef CLEAR_H
#define CLEAR_H
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
int A[1010], B[1010], sum[1010], mul[1010];
void s2BIG(string s, int a[])
{
a[0] = s.size();
for (int i = 1; i <= a[0]; i++)
{
a[i] = s[a[0] - i] - '0';
}
}
void i2BIG(int n, int a[])
{
if (n == 0)
{
a[0] = 1;
return;
}
int cur = 0;
while (n > 0)
{
cur++;
a[cur] = n % 10;
n /= 10;
}
if (n == 0) cur++;
a[0] = cur;
}
void addBIG(int x[], int y[], int z[])
{
z[0] = max(x[0], y[0]);
for (int i = 1; i <= z[0]; i++)
z[i] = x[i] + y[i];
for (int i = 1; i <= z[0]; i++)
{
z[i + 1] += z[i] / 10;
z[i] %= 10;
}
while (z[z[0] + 1] > 0) z[0]++;
}
void printBIG(int a[])
{
for (int i = a[0]; i >= 1; i--)
{
cout << a[i];
}
cout << endl;
}
void mulBIG(int x[], int y, int z[])
{
z[0] = x[0];
for (int i = 1; i <= z[0]; i++)
z[i] = x[i] * y;
for (int i = 1; i <= z[0]; i++)
{
z[i + 1] += z[i] / 10;
z[i] %= 10;
}
while (z[z[0] + 1] > 0)
{
z[0]++;
z[z[0] + 1] = z[z[0]] / 10;
z[z[0]] %= 10;
}
}
void plus()
{
string x, y;
cin >> x >> y;
memset(A, 0, sizeof(A));
memset(B, 0, sizeof(B));
memset(sum, 0, sizeof(sum));
s2BIG(x, A);
s2BIG(y, B);
addBIG(A, B, sum);
printBIG(sum);
}
void plussi()
{
string x;
int y;
cin >> x >> y;
memset(A, 0, sizeof(A));
memset(B, 0, sizeof(B));
memset(sum, 0, sizeof(sum));
s2BIG(x, A);
i2BIG(y, B);
addBIG(A, B, sum);
printBIG(sum);
}
void multiply()
{
string a;
int b;
cin >> a >> b;
memset(A, 0, sizeof(A));
memset(B, 0, sizeof(B));
memset(mul, 0, sizeof(mul));
s2BIG(a, A);
mulBIG(A, b, mul);
printBIG(mul);
}
#endif
以上就是我为大家带来的“实战:C++编写高精度头文件”,你学会了吗?