| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | ** EPITECH PROJECT, 2024 | ||
| 3 | ** template | ||
| 4 | ** File description: | ||
| 5 | ** Returns a float number starting from a string number (str) | ||
| 6 | */ | ||
| 7 | /** | ||
| 8 | * @file my_getnbr_float.c | ||
| 9 | * @brief The file containing the my_getnbr_float function | ||
| 10 | * @author Nicolas TORO | ||
| 11 | */ | ||
| 12 | |||
| 13 | #include "my.h" | ||
| 14 | |||
| 15 | 9 | static int index_negative(char const *str, int index) | |
| 16 | { | ||
| 17 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 4 times.
|
9 | if (index == 0) |
| 18 | 5 | return 1; | |
| 19 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
|
4 | if (str[index - 1] == '-') |
| 20 | 3 | return -1; | |
| 21 | 1 | return 1; | |
| 22 | } | ||
| 23 | |||
| 24 | 11 | float my_getnbr_float(char const *str) | |
| 25 | { | ||
| 26 | 11 | float convert = 0; | |
| 27 | 11 | int index = 0; | |
| 28 | 11 | float index_float = 0.1; | |
| 29 | |||
| 30 |
6/6✓ Branch 0 taken 7 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 4 times.
|
11 | if ((str[0] < '0' || str[0] > '9') && str[0] != '-') |
| 31 | 2 | return 0; | |
| 32 |
3/4✓ Branch 0 taken 5 times.
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
|
14 | while (str[index] < '0' || str[index] > '9') |
| 33 | 5 | index++; | |
| 34 | 9 | convert = convert * index_negative(str, index); | |
| 35 |
3/4✓ Branch 0 taken 35 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 35 times.
✗ Branch 3 not taken.
|
44 | for (; str[index] >= '0' && str[index] <= '9'; index++) |
| 36 | 35 | convert = convert * 10 + (str[index] - 48); | |
| 37 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5 times.
|
9 | if (str[index] == '.') { |
| 38 | 4 | index++; | |
| 39 |
4/4✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 2 times.
|
8 | for (; str[index] >= '0' && str[index] <= '9'; index++) { |
| 40 | 4 | convert = convert + index_float * (str[index] - 48); | |
| 41 | 4 | index_float = index_float * 0.1; | |
| 42 | } | ||
| 43 | } | ||
| 44 | 9 | return convert; | |
| 45 | } | ||
| 46 |