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