-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlong_multiplication.c
206 lines (172 loc) · 5 KB
/
long_multiplication.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_DIGITS 10000000
_CRTIMP char* __cdecl __MINGW_NOTHROW strrev (char*);
bool mem_alloc(void **ptr, size_t n, bool bRealloc)
{
if (bRealloc && *ptr != NULL)
*ptr = realloc(*ptr, n);
else
*ptr = malloc(n);
if (*ptr == NULL)
{
printf("Failed to allocate memory.\n");
return false;
}
else
{
printf("Allocated %u bytes at 0x%"PRIxPTR"\n", (uint32_t)n, (uint64_t)*ptr);
return true;
}
}
void long_mult(char *c, char *a, char *b)
{
if (strcmp(a, "0") == 0 || strcmp(b, "0") == 0)
{
strcpy(c, "0");
return;
}
strrev(a); strrev(b);
// the following swap doesn't make any difference
if (strlen(a) > strlen(b))
{
char *t = NULL;
if ( !mem_alloc((void *)&t, MAX_DIGITS, false) );
strcpy(t, a);
strcpy(a, b);
strcpy(b, t);
}
#ifdef _DEBUG
printf("multiplicand: %s\nmultiplier: %s\n", a, b);
#endif
for (int i = 0; i < strlen(a)+strlen(b); i++)
c[i] = '0';
// terminating null
c[strlen(a)+strlen(b)] = '\0';
#ifdef _DEBUG
printf("c: %s\nstrlen(c): %d\n", c, (int)strlen(c));
#endif
for (int i = 0; i < strlen(a); i++)
{
char carry;
for (int j = 0; j < strlen(b); j++)
{
char digit_i = a[i] - '0'; //putchar(a[i]);
char digit_j = b[j] - '0'; //putchar(b[j]);
#ifdef _DEBUG
printf("%s\n", c);
#endif
carry = (c[i+j] - '0' + digit_i * digit_j) / 10;
#ifdef _DEBUG
putchar(carry + '0');
printf("%d\n", carry);
putchar(c[i+j]);
#endif
c[i+j] = (c[i+j] - '0' + digit_i * digit_j) % 10 + '0';
#ifdef _DEBUG
putchar(c[i+j]);
#endif
if (carry > 0)
for (int k = i+j+1; k < strlen(a)+strlen(b); k++)
{
char newcarry = (c[k] - '0' + carry) / 10;
#ifdef _DEBUG
putchar(newcarry + '0');
#endif
c[k] = (c[k] - '0' + carry) % 10 + '0';
#ifdef _DEBUG
putchar(c[k]);
printf("%c\n", c[k]);
#endif
if (newcarry == 0)
break;
else
carry = newcarry;
}
}
} // for
if (c[strlen(c)-1] == '0')
c[strlen(c)-1] = '\0';
strrev(c);
return;
}
void addition(char *c, char *a, char *b)
{
strrev(a); strrev(b);
if (strlen(a) > strlen(b)) // make a the shorter string
{
char t[MAX_DIGITS];
strcpy(t, a);
strcpy(a, b);
strcpy(b, t);
}
for (int i=0; i<=strlen(b); i++)
c[i] = '0';
// terminating null
c[strlen(b)+1] = '\0';
for (int i=0; i<strlen(a); i++)
{
char carry;
char digit_a = a[i] - '0';
char digit_b = b[i] - '0';
carry = (c[i] - '0' + digit_a + digit_b) / 10;
c[i] = (c[i] - '0' + digit_a + digit_b) % 10 + '0';
if (carry > 0)
c[i+1] = carry + '0';
}
if (c[strlen(c)-1] == '0')
c[strlen(c)-1] = '\0';
strrev(c);
return;
}
int main(int argc, char *argv[])
{
char *a = NULL, *b = NULL, *c = NULL;
int num_cases, start, stop;
if ( !mem_alloc((void *)&a, MAX_DIGITS, false) )
return EXIT_FAILURE;
//a = malloc(MAX_DIGITS);
//printf("%X\n", a);
if ( !mem_alloc((void *)&b, MAX_DIGITS, false) )
return EXIT_FAILURE;
if ( !mem_alloc((void *)&c, 2 * MAX_DIGITS, false) )
return EXIT_FAILURE;
FILE *f_in = fopen("long_multiplication.txt", "r");
FILE *f_out = fopen("long_multiplication_results.txt", "w");
fscanf(f_in, "%d\n", &num_cases);
while (num_cases--)
{
if ( !mem_alloc((void *)&a, MAX_DIGITS, true) )
return EXIT_FAILURE;
if ( !mem_alloc((void *)&b, MAX_DIGITS, true) )
return EXIT_FAILURE;
//scanf("%s %s", a, b);
fgets(a, MAX_DIGITS, f_in);
*strrchr(a, '\n') = '\0'; // remove trailing newline!
printf("strlen(a): %d\n", (int)strlen(a));
fgets(b, MAX_DIGITS, f_in);
*strrchr(b, '\n') = '\0'; // remove trailing newline!
printf("strlen(b): %d\n", (int)strlen(b));
if ( !mem_alloc((void *)&c, 2 * MAX_DIGITS, true) )
return EXIT_FAILURE;
start = clock();
long_mult(c, a, b);
stop = clock();
//addition(c, a, b);
//printf("%s\n", c);
fputs(c, f_out);
fputc('\n', f_out);
printf("wrote strlen(c) = %d bytes to file.\n", (int)strlen(c));
printf("\ntime: %d ms\n", (stop-start));
}
fclose(f_in);
fclose(f_out);
free(a);
free(b);
free(c);
return 0;
}