大整数的四则运算
int 表示的数据范围有限,当涉及大整数的运算时候,我们就只能借助数组来存储,借助数组就依靠手工加减乘除,就小学学的那种来运算,然后一个个存入。
先来个结构体bign,含有一个数组和数组长度。我们用字符串读入的数字可以存入bign。头尾颠倒存入,因为模拟手工加减乘这三个都是从低位开始的,颠倒后方便操作
bign change(char str[])
{
bign a;
a.len = strlen(str);
for (int i = 0; i < a.len; i++)
{
a.d[i] = str[a.len - i - 1] - '0';
}
return a;
}
加法
bign add(bign a, bign b)
{
bign c;
int carry = 0;
for (int i = 0; i < a.len || i < b.len; i++)
{
int temp = a.d[i] + b.d[i] + carry;
c.d[c.len++] = temp % 10;
carry = temp / 10;
}
if (carry != 0)
{
c.d[c.len++] = carry;
}
return c;
}
减法
记得去掉前面的多余的0。
bign sub(bign a, bign b)
{
bign c;
for (int i = 0; i < a.len || i < b.len; i++)
{
if (a.d[i] < b.d[i])
{
a.d[i + 1]--;
a.d[i] += 10;
}
c.d[c.len++] = a.d[i] - b.d[i];
}
while (c.len - 1 >= 1 && c.d[c.len - 1] == 0)
c.len--; //删除除个位上的0
return c;
}
乘法(高精度与低精度)
乘法与手工乘法有些不同,他是把被乘数当成了一个整体,所得个位数做该位结果,高位部分作为新的进位。
然后乘法进位可能不止一位了。用while
前面bign 后面int
bign multi(bign a, int b)
{
bign c;
int carry = 0;
for (int i = 0; i < a.len; i++)
{
int temp = a.d[i] * b + carry;
c.d[c.len++] = temp % 10;
carry = temp / 10;
}
while (carry != 0)
{
c.d[c.len++] = carry % 10;
carry /= 10;
}
return c;
}
除法(高精度与低精度)
是余数和除数相除,注意从尾部开始计算,也要除去多余的0
前面bign 后面int
bign divide(bign a, int b, int& r)
{
bign c;
c.len = a.len;
for (int i = a.len - 1; i >= 0; i--)
{
r = r * 10 + a.d[i];
if (r < b)
c.d[i] = 0;
else
{
c.d[i] = r / b;
r = r % b;
}
}
while (c.len - 1 >= 1 && c.d[c.len - 1] == 0)
{
c.len--;
}
return c;
}
全部代码奉上
//找质数,找能除的质数
#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
#include<time.h>
#include<math.h>
using namespace std;
typedef long long ll;
struct bign {
int d[1000];
int len;
bign()
{
memset(d, 0, sizeof(d));
len = 0;
}
};
bign change(char str[])
{
bign a;
a.len = strlen(str);
for (int i = 0; i < a.len; i++)
{
//
a.d[i] = str[a.len - i - 1] - '0';
}
return a;
}
int compare(bign a, bign b)
{
if (a.len > b.len) return 1;
else if (a.len < b.len) return -1;
else
{
for (int i = a.len - 1; i >= 0; i--)
{
if (a.d[i] > b.d[i]) return 1;
else if (a.d[i] < b.d[i]) return -1;
}
return 0;
}
}
bign add(bign a, bign b)
{
bign c;
int carry = 0;
for (int i = 0; i < a.len || i < b.len; i++)
{
int temp = a.d[i] + b.d[i] + carry;
c.d[c.len++] = temp % 10;
carry = temp / 10;
}
if (carry != 0)
{
c.d[c.len++] = carry;
}
return c;
}
bign sub(bign a, bign b)
{
bign c;
for (int i = 0; i < a.len || i < b.len; i++)
{
if (a.d[i] < b.d[i])
{
a.d[i + 1]--;
a.d[i] += 10;
}
c.d[c.len++] = a.d[i] - b.d[i];
}
while (c.len - 1 >= 1 && c.d[c.len - 1] == 0)
c.len--; //删除除个位上的0
return c;
}
//
bign multi(bign a, int b)
{
bign c;
int carry = 0;
for (int i = 0; i < a.len; i++)
{
int temp = a.d[i] * b + carry;
c.d[c.len++] = temp % 10;
carry = temp / 10;
}
while (carry != 0)
{
c.d[c.len++] = carry % 10;
carry /= 10;
}
return c;
}
bign divide(bign a, int b, int& r)
{
bign c;
c.len = a.len;
for (int i = a.len - 1; i >= 0; i--)
{
r = r * 10 + a.d[i];
if (r < b)
c.d[i] = 0;
else
{
c.d[i] = r / b;
r = r % b;
}
}
while (c.len - 1 >= 1 && c.d[c.len - 1] == 0)
{
c.len--;
}
return c;
}
void print(bign a)
{
for (int i = a.len - 1; i >= 0; i--)
{
cout << a.d[i];
}
}
int main()
{
//char str1[1010], str2[1010];
//int b, r = 0;
//cin >> str1 >> b;
//bign a = change(str1);
//print(divide(a, b, r));
//cout << " " << r << endl;
char str3[1010], str4[1010];
cin >> str3 >> str4;
int q3;
bign q1 = change(str3);
bign q2 = change(str4);
cin >> q3;
print(add(q1, q2));
cout << endl;
print(sub(q1, q2));
cout << endl;
print(multi(q1, q3));
cout << endl;
}