Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | ** EPITECH PROJECT, 2023 | ||
3 | ** my_convert_base | ||
4 | ** File description: | ||
5 | ** Returns the result of the conversion of number (nbr) | ||
6 | ** in a specific base (base_from) to another base (base_to) | ||
7 | */ | ||
8 | |||
9 | #include <stdlib.h> | ||
10 | #include "my.h" | ||
11 | |||
12 | 391 | static int is_valid(char c, char const *base) | |
13 | { | ||
14 |
2/2✓ Branch 1 taken 1750 times.
✓ Branch 2 taken 1 times.
|
1751 | for (int i = 0; i < my_strlen(base); i++) { |
15 |
2/2✓ Branch 0 taken 390 times.
✓ Branch 1 taken 1360 times.
|
1750 | if (c == base[i]) |
16 | 390 | return 1; | |
17 | } | ||
18 | 1 | return 0; | |
19 | } | ||
20 | |||
21 | 391 | static int calculate_base(char const *str, char const *base, | |
22 | int *index, int *error) | ||
23 | { | ||
24 |
2/2✓ Branch 1 taken 390 times.
✓ Branch 2 taken 1 times.
|
391 | if (is_valid(str[index[0]], base) == 1) { |
25 |
2/2✓ Branch 0 taken 39 times.
✓ Branch 1 taken 351 times.
|
390 | if (str[index[0]] == base[index[1]]) { |
26 | 39 | return index[1] * my_compute_power_rec(my_strlen(base), | |
27 | 39 | my_strlen(str) - index[0] - 1); | |
28 | } | ||
29 | } else { | ||
30 | 1 | *error = 1; | |
31 | } | ||
32 | 352 | return 0; | |
33 | } | ||
34 | |||
35 | 13 | static unsigned int my_getnbr_base_unsigned(char const *str, char const *base) | |
36 | { | ||
37 | 13 | unsigned int result = 0; | |
38 | 13 | int error = 0; | |
39 | int index[2]; | ||
40 | |||
41 |
2/2✓ Branch 1 taken 42 times.
✓ Branch 2 taken 13 times.
|
55 | for (int i = my_strlen(str) - 1; i >= 0; i--) { |
42 |
4/4✓ Branch 1 taken 394 times.
✓ Branch 2 taken 39 times.
✓ Branch 3 taken 391 times.
✓ Branch 4 taken 3 times.
|
433 | for (int j = 0; j < my_strlen(base) && error == 0; j++) { |
43 | 391 | index[0] = i; | |
44 | 391 | index[1] = j; | |
45 | 391 | result = result + calculate_base(str, base, index, &error); | |
46 | } | ||
47 | } | ||
48 | 13 | return result; | |
49 | } | ||
50 | |||
51 | 13 | static char *put_str_nb(unsigned int nb, char const *base, int base_len) | |
52 | { | ||
53 | 13 | int len_nb = 1; | |
54 | 13 | unsigned int temp_nb = nb; | |
55 | char *nb_str; | ||
56 | 13 | unsigned int figure_temp = nb; | |
57 | |||
58 |
2/2✓ Branch 0 taken 93 times.
✓ Branch 1 taken 13 times.
|
106 | while ((temp_nb / base_len) != 0) { |
59 | 93 | len_nb = len_nb + 1; | |
60 | 93 | temp_nb = temp_nb / base_len; | |
61 | } | ||
62 | 13 | nb_str = malloc(sizeof(char) * len_nb); | |
63 |
2/2✓ Branch 0 taken 106 times.
✓ Branch 1 taken 13 times.
|
119 | for (int i = 0; i < len_nb; i++) { |
64 | 106 | nb_str[len_nb - i - 1] = base[figure_temp % base_len]; | |
65 | 106 | figure_temp = (figure_temp - (figure_temp % base_len)) / base_len; | |
66 | } | ||
67 | 13 | nb_str[len_nb] = '\0'; | |
68 | 13 | return my_strdup(nb_str); | |
69 | } | ||
70 | |||
71 | 13 | static char *my_setnbr_base(unsigned int nbr, char const *base) | |
72 | { | ||
73 | 13 | int base_len = 0; | |
74 | |||
75 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 13 times.
|
39 | while (base[base_len] != '\0') |
76 | 26 | base_len = base_len + 1; | |
77 | 13 | return put_str_nb(nbr, base, base_len); | |
78 | } | ||
79 | |||
80 | 13 | char *my_convert_base_unsigned(char const *nbr, | |
81 | char const *base_from, char const *base_to) | ||
82 | { | ||
83 | 13 | unsigned int number = my_getnbr_base_unsigned(nbr, base_from); | |
84 | 13 | char *result = my_setnbr_base(number, base_to); | |
85 | |||
86 | 13 | return result; | |
87 | } | ||
88 |