Skip to content
This repository has been archived by the owner on Feb 25, 2021. It is now read-only.

Commit

Permalink
mult and mod
Browse files Browse the repository at this point in the history
  • Loading branch information
umberd committed Oct 30, 2019
1 parent 3b1aad2 commit 9abf288
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion inf_mod.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ char *inf_mod(char *a, char *b, char *base)
free(nb_a);
nb_a = getstr_from_to(a, 0, len_b);
}
res = inf_div_two(nb_a, nb_b, a, base);
res = inf_mod_two(nb_a, nb_b, a, base);
return my_putstr_l_z(res, 0);
}
23 changes: 16 additions & 7 deletions inf_mult.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@

char *my_putstr_l_z(char *result, int is_neg);

int base_i(char const c, char const *base)
{
for (int i = 0; base[i] != '\0'; i++)
if (base[i] == c)
return i;
return 'a';
}

char *my_memset(char *s, char c, int n)
{
for (int i = 0; i < n; i++) {
Expand All @@ -24,19 +32,20 @@ char *inf_mult_two(char *result, char *nb1, char *nb2,
char const *base)
{
int i;
(void) base;
int len_base = my_strlen(base);
int tmp;
int carry = 0;
int j;

for (i = 0; nb2[i] != '\0'; i++) {
carry = 0;
for (j = 0; nb1[j] != '\0'; j++) {
tmp = carry + (nb1[j] - '0') * (nb2[i] - '0') + result[i+j] - '0';
carry = tmp / 10;
result[i+j] = (tmp % 10) + '0';
tmp = carry + base_i(nb1[j], base) * base_i(nb2[i], base) +
base_i(result[i+j], base);
carry = tmp / len_base;
result[i+j] = base[tmp % len_base];
}
result[i+j] = carry + '0';
result[i+j] = base[carry];
j++;
}
result[i+j] = '\0';
Expand All @@ -45,15 +54,15 @@ char *inf_mult_two(char *result, char *nb1, char *nb2,

char *inf_mult(char *nb1, char *nb2, char const *base)
{
(void) base;
int len_result = my_strlen(nb1) + my_strlen(nb2);
char *result = malloc(sizeof(char)*(len_result + 1));
char *cp1;
char *cp2;

printf("%s %s\n", nb1, nb2);
if (!result)
return (NULL);
my_memset(result, '0', len_result);
my_memset(result, base[0], len_result);
cp1 = my_revstr(my_strdup(nb1));
cp2 = my_revstr(my_strdup(nb2));
result = inf_mult_two(result, cp1, cp2, base);
Expand Down

0 comments on commit 9abf288

Please sign in to comment.