大整数乘法分治法的c++代码
时间: 2023-05-29 19:02:08 浏览: 101
以下是大整数乘法分治法的C代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 用于将字符转换为数字
int char_to_int(char c)
{
return (int)(c - '0');
}
// 用于将数字转换为字符
char int_to_char(int n)
{
return (char)(n + '0');
}
// 用于将字符串表示的数字转换为整数数组
void string_to_array(char *str, int *a, int len)
{
int i, j;
for (i = 0, j = len - 1; i < len; i++, j--) {
a[i] = char_to_int(str[j]);
}
}
// 用于将整数数组表示的数字转换为字符串
void array_to_string(int *a, int len, char *str)
{
int i, j;
for (i = len - 1, j = 0; i >= 0; i--, j++) {
str[j] = int_to_char(a[i]);
}
str[len] = '\0';
}
// 用于向数组中添加零
void append_zero(int *a, int len, int n)
{
int i;
for (i = len; i < len + n; i++) {
a[i] = 0;
}
}
// 用于删除数组中前导零
int remove_leading_zeros(int *a, int len)
{
int i;
for (i = len - 1; i > 0; i--) {
if (a[i] != 0) {
break;
}
}
return i + 1;
}
// 用于将两个整数数组相加
void add(int *a, int *b, int *c, int len)
{
int i, carry = 0;
for (i = 0; i < len; i++) {
c[i] = a[i] + b[i] + carry;
carry = c[i] / 10;
c[i] %= 10;
}
if (carry > 0) {
c[i] = carry;
}
}
// 用于将两个整数数组相减
void subtract(int *a, int *b, int *c, int len)
{
int i, borrow = 0;
for (i = 0; i < len; i++) {
c[i] = a[i] - b[i] - borrow;
if (c[i] < 0) {
c[i] += 10;
borrow = 1;
} else {
borrow = 0;
}
}
}
// 用于将两个整数数组相乘
void multiply(int *a, int *b, int *c, int len)
{
if (len == 1) {
c[0] = a[0] * b[0];
return;
}
int i, j, k;
int *a1 = (int *)malloc(len / 2 * sizeof(int));
int *b1 = (int *)malloc(len / 2 * sizeof(int));
int *c1 = (int *)malloc(len * sizeof(int));
int *a2 = (int *)malloc(len / 2 * sizeof(int));
int *b2 = (int *)malloc(len / 2 * sizeof(int));
int *c2 = (int *)malloc(len * sizeof(int));
int *a1b1 = (int *)malloc(len * sizeof(int));
int *a2b2 = (int *)malloc(len * sizeof(int));
int *tmp = (int *)malloc(len * sizeof(int));
memset(a1, 0, len / 2 * sizeof(int));
memset(b1, 0, len / 2 * sizeof(int));
memset(c1, 0, len * sizeof(int));
memset(a2, 0, len / 2 * sizeof(int));
memset(b2, 0, len / 2 * sizeof(int));
memset(c2, 0, len * sizeof(int));
memset(a1b1, 0, len * sizeof(int));
memset(a2b2, 0, len * sizeof(int));
memset(tmp, 0, len * sizeof(int));
for (i = 0; i < len / 2; i++) {
a1[i] = a[i];
b1[i] = b[i];
}
for (i = len / 2; i < len; i++) {
a2[i - len / 2] = a[i];
b2[i - len / 2] = b[i];
}
multiply(a1, b1, c1, len / 2);
multiply(a2, b2, c2, len / 2);
add(a1, a2, tmp, len / 2);
add(b1, b2, tmp + len / 2, len / 2);
multiply(tmp, tmp + len / 2, a1b1, len / 2 + 1);
subtract(a1b1, c1, a1b1, len * 2);
subtract(a1b1, c2, a1b1, len * 2 - (len / 2));
append_zero(a1b1, len * 2 - (len / 2), len / 2);
add(c1, a1b1, c1, len * 2);
append_zero(c2, len, len / 2);
add(c1, c2, c, len * 2);
free(a1);
free(b1);
free(c1);
free(a2);
free(b2);
free(c2);
free(a1b1);
free(a2b2);
free(tmp);
}
int main()
{
char a[1001];
char b[1001];
int len1, len2, len, i;
int *num1, *num2, *result;
scanf("%s", a);
scanf("%s", b);
len1 = strlen(a);
len2 = strlen(b);
len = len1 > len2 ? len1 : len2;
num1 = (int *)malloc(len * sizeof(int));
num2 = (int *)malloc(len * sizeof(int));
result = (int *)malloc(len * 2 * sizeof(int));
memset(num1, 0, len * sizeof(int));
memset(num2, 0, len * sizeof(int));
memset(result, 0, len * 2 * sizeof(int));
string_to_array(a, num1, len1);
string_to_array(b, num2, len2);
multiply(num1, num2, result, len);
i = remove_leading_zeros(result, len * 2);
if (i == 0) {
printf("0\n");
} else {
array_to_string(result, i, a);
printf("%s\n", a);
}
free(num1);
free(num2);
free(result);
return 0;
}
```
阅读全文