| Line | Branch | Exec | Source | 
|---|---|---|---|
| 1 | /* | ||
| 2 | ** EPITECH PROJECT, 2023 | ||
| 3 | ** my_printf | ||
| 4 | ** File description: | ||
| 5 | ** Flag to print a %E (a double) | ||
| 6 | */ | ||
| 7 | /** | ||
| 8 | * @file flag_bige.c | ||
| 9 | * @brief The file containing the flag_bige function | ||
| 10 | * @author Nicolas TORO | ||
| 11 | */ | ||
| 12 | |||
| 13 | #include "myprintf.h" | ||
| 14 | |||
| 15 | 6 | static char *my_get_str_float(double nb, formating_t *formating) | |
| 16 | { | ||
| 17 | 6 | long long int partie_entiere = ABS((int)nb); | |
| 18 | 2/2✓ Branch 0 taken 2 times. ✓ Branch 1 taken 4 times. | 6 | double partie_d = ABS(nb - partie_entiere); | 
| 19 | 6 | char *str_finale = malloc(sizeof(char) * 1000000); | |
| 20 | |||
| 21 | 2/2✓ Branch 0 taken 2 times. ✓ Branch 1 taken 4 times. | 6 | if (nb < 0){ | 
| 22 | 2 | my_strcat(str_finale, "-"); | |
| 23 | 2 | nb = -nb; | |
| 24 | } | ||
| 25 | 6 | my_strcat(str_finale, my_str_nbr_long_long(partie_entiere)); | |
| 26 | 6 | precise_it_double(str_finale, formating, partie_d); | |
| 27 | 6 | return str_finale; | |
| 28 | } | ||
| 29 | |||
| 30 | 5 | static char *my_get_power(int exposant) | |
| 31 | { | ||
| 32 | char *power; | ||
| 33 | |||
| 34 | 2/2✓ Branch 0 taken 1 times. ✓ Branch 1 taken 4 times. | 5 | if (ABS(exposant) >= 100) { | 
| 35 | 1 | power = malloc(sizeof(char) * 5); | |
| 36 | } else { | ||
| 37 | 4 | power = malloc(sizeof(char) * 4); | |
| 38 | } | ||
| 39 | 2/2✓ Branch 0 taken 2 times. ✓ Branch 1 taken 3 times. | 5 | if (exposant >= 0) { | 
| 40 | 2 | my_strcat(power, "+"); | |
| 41 | } else { | ||
| 42 | 3 | my_strcat(power, "-"); | |
| 43 | } | ||
| 44 | 3/4✓ Branch 0 taken 4 times. ✓ Branch 1 taken 1 times. ✓ Branch 2 taken 4 times. ✗ Branch 3 not taken. | 5 | if (ABS(exposant) < 10) | 
| 45 | 4 | my_strcat(power, "0"); | |
| 46 | 5 | my_strcat(power, my_str_nbr(ABS(exposant))); | |
| 47 | 5 | my_strcat(power, "\0"); | |
| 48 | 5 | return power; | |
| 49 | } | ||
| 50 | |||
| 51 | 5 | static void condition(double nb, int *exposant, | |
| 52 | double *temp_nb_double, int *temp_nb) | ||
| 53 | { | ||
| 54 | 4/4✓ Branch 0 taken 2 times. ✓ Branch 1 taken 3 times. ✓ Branch 2 taken 2 times. ✓ Branch 3 taken 3 times. | 5 | if (ABS(nb) > 1) { | 
| 55 | 4/4✓ Branch 0 taken 2 times. ✓ Branch 1 taken 2 times. ✓ Branch 4 taken 2 times. ✓ Branch 5 taken 2 times. | 4 | for (int i = 0; i < my_strlen(my_str_nbr(ABS(nb))) - 1; i++) { | 
| 56 | 2 | *exposant = *exposant + 1; | |
| 57 | 2 | *temp_nb_double = *temp_nb_double / 10; | |
| 58 | } | ||
| 59 | } else { | ||
| 60 | 2/2✓ Branch 0 taken 104 times. ✓ Branch 1 taken 3 times. | 107 | while (*temp_nb % 10 == 0) { | 
| 61 | 104 | *exposant = *exposant - 1; | |
| 62 | 104 | *temp_nb_double = *temp_nb_double * 10; | |
| 63 | 104 | *temp_nb = *temp_nb_double; | |
| 64 | } | ||
| 65 | } | ||
| 66 | 5 | } | |
| 67 | |||
| 68 | 6 | static char *my_get_str_float_scientific(double nb, formating_t *formating) | |
| 69 | { | ||
| 70 | char *float_nb; | ||
| 71 | 6 | int signe = 0; | |
| 72 | 6 | int exposant = 0; | |
| 73 | 6 | int temp_nb = nb; | |
| 74 | 6 | double temp_nb_double = nb; | |
| 75 | |||
| 76 | 2/2✓ Branch 0 taken 2 times. ✓ Branch 1 taken 4 times. | 6 | if (nb < 0) | 
| 77 | 2 | signe = 1; | |
| 78 | 6 | float_nb = malloc(sizeof(char) * (16 + signe)); | |
| 79 | 2/2✓ Branch 0 taken 1 times. ✓ Branch 1 taken 5 times. | 6 | if (nb == 0.0) { | 
| 80 | 1 | my_strcat(float_nb, my_get_str_float(temp_nb_double, formating)); | |
| 81 | 1 | my_strcat(float_nb, "E+00\0"); | |
| 82 | 1 | return float_nb; | |
| 83 | } | ||
| 84 | 5 | condition(nb, &exposant, &temp_nb_double, &temp_nb); | |
| 85 | 5 | my_strcat(float_nb, my_get_str_float(temp_nb_double, formating)); | |
| 86 | 5 | my_strcat(float_nb, "E"); | |
| 87 | 5 | my_strcat(float_nb, my_get_power(exposant)); | |
| 88 | 5 | return float_nb; | |
| 89 | } | ||
| 90 | |||
| 91 | 6 | int flag_bige(va_list list, formating_t *formating) | |
| 92 | { | ||
| 93 | 6 | double temp_double = va_arg(list, double); | |
| 94 | 6 | char *temp = my_get_str_float_scientific(temp_double, formating); | |
| 95 | |||
| 96 | 6 | format_it_double(temp, formating, temp_double); | |
| 97 | 6 | return my_putstr_fd_free(temp, formating->fd); | |
| 98 | } | ||
| 99 |