-
Notifications
You must be signed in to change notification settings - Fork 0
/
arithmatoy.c
320 lines (275 loc) · 8.81 KB
/
arithmatoy.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
#include "utils.h"
int VERBOSE = 0;
typedef struct {
char *data;
size_t size;
size_t capacity;
} DynamicArray;
// Here are some utility functions that might be helpful to implement add, sub
// and mul:
unsigned int get_digit_value(char digit) {
// Convert a digit from get_all_digits() to its integer value
if (digit >= '0' && digit <= '9') {
return digit - '0';
}
if (digit >= 'a' && digit <= 'z') {
return 10 + (digit - 'a');
}
return -1;
}
char to_digit(unsigned int value) {
// Convert an integer value to a digit from get_all_digits()
if (value >= ALL_DIGIT_COUNT) {
debug_abort("Invalid value for to_digit()");
return 0;
}
return get_all_digits()[value];
}
char *reverse(char *str) {
// Reverse a string in place, return the pointer for convenience
// Might be helpful if you fill your char* buffer from left to right
const size_t length = strlen(str);
const size_t bound = length / 2;
for (size_t i = 0; i < bound; ++i) {
char tmp = str[i];
const size_t mirror = length - i - 1;
str[i] = str[mirror];
str[mirror] = tmp;
}
return str;
}
const char *drop_leading_zeros(const char *number) {
// If the number has leading zeros, return a pointer past these zeros
// Might be helpful to avoid computing a result with leading zeros
if (*number == '\0') {
return number;
}
while (*number == '0') {
++number;
}
if (*number == '\0') {
--number;
}
return number;
}
void debug_abort(const char *debug_msg) {
// Print a message and exit
fprintf(stderr, debug_msg);
exit(EXIT_FAILURE);
}
DynamicArray *createDynamicArray() {
DynamicArray *array = (DynamicArray *) malloc(sizeof(DynamicArray));
array->data = NULL;
array->size = 0;
array->capacity = 0;
return array;
}
void append(DynamicArray *array, char value) {
if (array->size >= array->capacity) {
size_t new_capacity = array->capacity == 0 ? 1 : array->capacity * 2;
char *new_data = (char *) realloc(array->data, new_capacity * sizeof(char));
if (new_data == NULL) {
fprintf(stderr, "Error: could not allocate memory\n");
exit(EXIT_FAILURE);
}
array->data = new_data;
array->capacity = new_capacity;
}
array->data[array->size++] = value;
}
void setDynamicArray(DynamicArray *array, char *data, size_t size, size_t capacity){
array->data = data;
array->size = size;
array->capacity = capacity;
}
void destroyDynamicArray(DynamicArray *array) {
free(array->data);
free(array);
}
const char *get_all_digits() { return "0123456789abcdefghijklmnopqrstuvwxyz"; }
const size_t ALL_DIGIT_COUNT = 36;
void arithmatoy_free(char *number) { free(number); }
char* get_reverse(const char* to_reverse, size_t length){
char *rev = calloc(length, sizeof(char));
memcpy(rev, to_reverse, length * sizeof(char));
return reverse(rev);
}
int abs(int value){
return value < 0 ? -value : value;
}
// Fill the function, the goal is to compute lhs + rhs
// You should allocate a new char* large enough to store the result as a
// string Implement the algorithm Return the result
char *arithmatoy_add(unsigned int base, const char *lhs, const char *rhs) {
if (VERBOSE) {
fprintf(stderr, "add: entering function\n");
}
DynamicArray *result = createDynamicArray();
const char* charset = get_all_digits();
const char *n_rhs = drop_leading_zeros(rhs);
const char *n_lhs = drop_leading_zeros(lhs);
size_t lhs_len = strlen(n_lhs);
size_t rhs_len = strlen(n_rhs);
size_t max = lhs_len > rhs_len ? lhs_len : rhs_len;
char *rev_rhs = get_reverse(n_rhs, rhs_len);
char *rev_lhs = get_reverse(n_lhs, lhs_len);
char a, b;
int retenu = 0;
char calcul;
for(int i=0; i < max; i++){
a = lhs_len <= i ? '0' : rev_lhs[i];
b = rhs_len <= i ? '0' : rev_rhs[i];
calcul = charset[(get_digit_value(a) + get_digit_value(b) + retenu) % base];
retenu = (get_digit_value(a) + get_digit_value(b) + retenu) / base;
if(VERBOSE)
fprintf(stderr, "add: digit %c digit %c carry %d\n", a, b, retenu);
append(result, calcul);
}
while(retenu != 0){
calcul = charset[retenu % base];
retenu = retenu / base;
append(result, calcul);
}
append(result, '\0');
size_t length = result->size;
char* final_result = calloc(length, sizeof(char));
memcpy(final_result, result->data, length * sizeof(char));
destroyDynamicArray(result);
free(rev_lhs);
free(rev_rhs);
return reverse(final_result);
}
// Fill the function, the goal is to compute lhs - rhs (assuming lhs > rhs)
// You should allocate a new char* large enough to store the result as a
// string Implement the algorithm Return the result
char *arithmatoy_sub(unsigned int base, const char *lhs, const char *rhs) {
if (VERBOSE) {
fprintf(stderr, "sub: entering function\n");
}
DynamicArray *result = createDynamicArray();
const char* charset = get_all_digits();
const char *n_rhs = drop_leading_zeros(rhs);
const char *n_lhs = drop_leading_zeros(lhs);
size_t lhs_len = strlen(n_lhs);
size_t rhs_len = strlen(n_rhs);
size_t max = lhs_len > rhs_len ? lhs_len : rhs_len;
char *rev_rhs = get_reverse(n_rhs, rhs_len);
char *rev_lhs = get_reverse(n_lhs, lhs_len);
char a, b;
int retenu = 0;
int next_retenu = 0;
int intermediaire;
int index;
char calcul;
for(int i=0; i < max; i++){
a = lhs_len <= i ? '0' : rev_lhs[i];
b = rhs_len <= i ? '0' : rev_rhs[i];
intermediaire = get_digit_value(a) - get_digit_value(b) - retenu;
if(intermediaire < 0){
index = base - abs(intermediaire);
next_retenu = 1;
} else {
index = intermediaire;
next_retenu = 0;
}
calcul = charset[index % base];
retenu = next_retenu;
if(VERBOSE)
fprintf(stderr, "add: digit %c digit %c carry %d\n", a, b, retenu);
append(result, calcul);
}
append(result, '\0');
size_t length = result->size;
char* final_result = calloc(length, sizeof(char));
memcpy(final_result, result->data, length * sizeof(char));
destroyDynamicArray(result);
free(rev_lhs);
free(rev_rhs);
// lhs < rhs
if(retenu > 0){
free(final_result);
return NULL;
}
final_result = reverse(final_result);
final_result = (char*) drop_leading_zeros((const char*) final_result);
return final_result;
}
char *multiple_adding(DynamicArray **results, size_t length, unsigned int base){
char *final_result;
if(length==1){
return results[0]->data;
}
final_result = results[0]->data;
for(int i=1; i<length; i++){
final_result = arithmatoy_add(base, (const char*) final_result, (const char*) results[i]->data);
// printf("AFTER results[%d] = %s\n", i, results[i]->data);
// printf("AFTER final_result = %s\n", final_result);
}
return final_result;
}
int my_pow(int base, int exp){
if(exp == 0)
return 1;
int res = 1;
for(int i = 0; i < exp; i++)
res *= base;
return res;
}
// Fill the function, the goal is to compute lhs * rhs
// You should allocate a new char* large enough to store the result as a
// string Implement the algorithm Return the result
char *arithmatoy_mul(unsigned int base, const char *lhs, const char *rhs) {
if (VERBOSE) {
fprintf(stderr, "mul: entering function\n");
}
const char* charset = get_all_digits();
const char *n_rhs = drop_leading_zeros(rhs);
const char *n_lhs = drop_leading_zeros(lhs);
size_t lhs_len = strlen(n_lhs);
size_t rhs_len = strlen(n_rhs);
char *rev_rhs = get_reverse(n_rhs, rhs_len);
char *rev_lhs = get_reverse(n_lhs, lhs_len);
// Initialize result array with zeroes
size_t result_len = lhs_len + rhs_len;
int *result = calloc(result_len, sizeof(int));
for (size_t i = 0; i < lhs_len; i++) {
for (size_t j = 0; j < rhs_len; j++) {
int lhs_digit = get_digit_value(rev_lhs[i]);
int rhs_digit = get_digit_value(rev_rhs[j]);
int product = lhs_digit * rhs_digit;
result[i + j] += product;
// Handle carry
size_t k = i + j;
while (result[k] >= base) {
result[k + 1] += result[k] / base;
result[k] %= base;
k++;
}
}
}
// Convert result array back to string
DynamicArray *result_str = createDynamicArray();
int start = result_len - 1;
while (start >= 0 && result[start] == 0) {
start--; // Remove trailing zeros
}
for (int i = start; i >= 0; i--) {
append(result_str, to_digit(result[i]));
}
if (start < 0) {
append(result_str, '0'); // If the result is 0
}
append(result_str, '\0');
// Clean up
free(result);
free(rev_lhs);
free(rev_rhs);
char *final_result = strdup(result_str->data);
destroyDynamicArray(result_str);
return final_result;
}