| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | ** EPITECH PROJECT, 2023 | ||
| 3 | ** my_putnbr_base | ||
| 4 | ** File description: | ||
| 5 | ** Prints a number (nb) in the requested base (base) in stdout | ||
| 6 | */ | ||
| 7 | |||
| 8 | #include "my.h" | ||
| 9 | |||
| 10 | 8 | static void put_str_nb(int nb, int len_nb, char const *base, int base_len) | |
| 11 | 8 | { | |
| 12 | 8 | char nb_str[len_nb]; | |
| 13 | 8 | int figure_temp = nb; | |
| 14 | |||
| 15 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 8 times.
|
20 | for (int i = 0; i < len_nb; i++) { |
| 16 | 12 | nb_str[len_nb - i - 1] = base[figure_temp % base_len]; | |
| 17 | 12 | figure_temp = (figure_temp - (figure_temp % base_len)) / base_len; | |
| 18 | } | ||
| 19 | 8 | nb_str[len_nb] = '\0'; | |
| 20 | 8 | my_putstr(nb_str); | |
| 21 | 8 | } | |
| 22 | |||
| 23 | 8 | int my_putnbr_base(int nbr, char const *base) | |
| 24 | { | ||
| 25 | 8 | int len_nb = 1; | |
| 26 | 8 | int temp_nb = nbr; | |
| 27 | 8 | int base_len = 0; | |
| 28 | |||
| 29 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7 times.
|
8 | if (nbr < 0) { |
| 30 | 1 | my_putchar('-'); | |
| 31 | 1 | nbr = -nbr; | |
| 32 | } | ||
| 33 |
2/2✓ Branch 0 taken 122 times.
✓ Branch 1 taken 8 times.
|
130 | while (base[base_len] != '\0') { |
| 34 | 122 | base_len = base_len + 1; | |
| 35 | } | ||
| 36 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 8 times.
|
12 | while ((temp_nb / base_len) != 0) { |
| 37 | 4 | len_nb = len_nb + 1; | |
| 38 | 4 | temp_nb = temp_nb / base_len; | |
| 39 | } | ||
| 40 | 8 | put_str_nb(nbr, len_nb, base, base_len); | |
| 41 | 8 | return 0; | |
| 42 | } | ||
| 43 |