-
Notifications
You must be signed in to change notification settings - Fork 0
/
bigNumber.c
61 lines (52 loc) · 1.46 KB
/
bigNumber.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
#include <string.h>
#include <math.h>
#define MAX 1024
int strToIntArray(int *number, char *str, int intLimit)
{
int result = 0;
char buffer[MAX];
int i;
sprintf(buffer, "%%%dd", intLimit);
for (i = strlen(str) - intLimit; i >= 0; i -= intLimit)
sscanf(str + i, buffer, number + result++);
if ((i += intLimit) > 0) {
sprintf(buffer, "%%%dd", i);
sscanf(str, buffer, number + result++);
}
return result;
}
int main(int argc, char *argv[])
{
int num1[MAX], num2[MAX];
int num3[2 * MAX + 1] = {0};
int size = 4; // log_2 10^(4 * 2) < 32
int overflow = pow(10, size);
char numStr1[MAX * size + 1], numStr2[MAX * size + 1];
char buffer[MAX];
int length1, length2;
scanf("%s %s", numStr1, numStr2);
//
length1 = strToIntArray(num1, numStr1, size);
length2 = strToIntArray(num2, numStr2, size);
//
for (int x = 0; x < length1; x++) {
for (int y = 0; y < length2; y++) {
num3[x + y] += num1[x] * num2[y];
if (num3[x + y] >= overflow) {
num3[x + y + 1] += num3[x + y] / overflow;
num3[x + y] %= overflow;
}
}
}
//
sprintf(buffer, "%%0%dd,", size);
int i = length1 + length2 - 2;
if (num3[i + 1])
i++;
printf("%d,", num3[i--]);
while (i >= 0)
printf(buffer, num3[i--]);
printf("\b \n");
return 0;
}