add
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#define MAX_LEN 1000
|
||||
// 计算两个大整数的乘积
|
||||
void multiply(const char* a, const char* b, char* result) {
|
||||
int lenA = strlen(a), lenB = strlen(b), lenResult = lenA + lenB;
|
||||
int arr[MAX_LEN * 2] = {0};
|
||||
|
||||
// 进行乘法运算
|
||||
for (int i = 0; i < lenA; ++i)
|
||||
for (int j = 0; j < lenB; ++j)
|
||||
arr[i + j] += (a[lenA - i - 1] - '0') * (b[lenB - j - 1] - '0');
|
||||
// 处理进位
|
||||
for (int i = 0; i < lenResult - 1; ++i) {
|
||||
arr[i + 1] += arr[i] / 10;
|
||||
arr[i] %= 10;
|
||||
}
|
||||
// 构建结果字符串
|
||||
int index = 0;
|
||||
while (index < lenResult && arr[index] == 0) ++index;
|
||||
if (index == lenResult) strcpy(result, "0");
|
||||
else {
|
||||
for (int i = index; i < lenResult; ++i)
|
||||
result[i - index] = arr[lenResult - i - 1] + '0';
|
||||
result[lenResult - index] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
char a[MAX_LEN + 1], b[MAX_LEN + 1], result[MAX_LEN * 2 + 1];
|
||||
printf("输入第一个大整数: ");
|
||||
scanf("%s", a);
|
||||
printf("输入第二个大整数: ");
|
||||
scanf("%s", b);
|
||||
multiply(a, b, result);
|
||||
printf("结果: %s\n", result);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user