用C语言完成高精度乘法
时间: 2024-05-15 19:19:27 浏览: 41
C语言实现高精度乘法.c
```c
#include <stdio.h>
#include <string.h>
#define MAXN 1000
struct bigInt {
int digit[MAXN], len;
bigInt() {
memset(digit, 0, sizeof(digit));
len = 0;
}
};
bigInt toBigInt(char str[]) {
bigInt a;
a.len = strlen(str);
for (int i = 0; i < a.len; i++) {
a.digit[i] = str[a.len - 1 - i] - '0';
}
return a;
}
bigInt mul(bigInt a, int b) {
bigInt c;
int carry = 0;
for (int i = 0; i < a.len; i++) {
int tmp = a.digit[i] * b + carry;
c.digit[c.len++] = tmp % 10;
carry = tmp / 10;
}
if (carry > 0) {
c.digit[c.len++] = carry;
}
while (c.len > 1 && c.digit[c.len - 1] == 0) {
c.len--;
}
return c;
}
bigInt mul(bigInt a, bigInt b) {
bigInt c;
for (int i = 0; i < b.len; i++) {
bigInt tmp = mul(a, b.digit[i]);
for (int j = 0; j < i; j++) {
tmp.digit[tmp.len++] = 0;
}
c = add(c, tmp);
}
return c;
}
void printBigInt(bigInt a) {
for (int i = a.len - 1; i >= 0; i--) {
printf("%d", a.digit[i]);
}
printf("\n");
}
int main() {
char str1[MAXN], str2[MAXN];
scanf("%s%s", str1, str2);
bigInt a = toBigInt(str1);
bigInt b = toBigInt(str2);
bigInt c = mul(a, b);
printBigInt(c);
return 0;
}
```
阅读全文