Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | ** EPITECH PROJECT, 2023 | ||
3 | ** my_getnbr_base | ||
4 | ** File description: | ||
5 | ** Returns a int number starting from a char number (str) | ||
6 | ** in the requested base (base) | ||
7 | */ | ||
8 | |||
9 | #include "my.h" | ||
10 | |||
11 | 926 | static int is_valid(char c, char const *base) | |
12 | { | ||
13 |
2/2✓ Branch 1 taken 6398 times.
✓ Branch 2 taken 2 times.
|
6400 | for (int i = 0; i < my_strlen(base); i++) { |
14 |
2/2✓ Branch 0 taken 924 times.
✓ Branch 1 taken 5474 times.
|
6398 | if (c == base[i]) |
15 | 924 | return 1; | |
16 | } | ||
17 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
2 | if (c == '-' || c == '+') |
18 | 1 | return 1; | |
19 | 1 | return 0; | |
20 | } | ||
21 | |||
22 | 926 | static int calculate_base(char const *str, char const *base, | |
23 | int *index, int **ptr) | ||
24 | { | ||
25 |
2/2✓ Branch 1 taken 925 times.
✓ Branch 2 taken 1 times.
|
926 | if (is_valid(str[index[0]], base) == 1) { |
26 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 924 times.
|
925 | if (str[index[0]] == '-') { |
27 | 1 | *(ptr[0]) = - *(ptr[0]); | |
28 | 1 | return 0; | |
29 | } | ||
30 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 864 times.
|
924 | if (str[index[0]] == base[index[1]]) { |
31 | 60 | return index[1] * my_compute_power_rec(my_strlen(base), | |
32 | 60 | my_strlen(str) - index[0] - 1); | |
33 | } | ||
34 | } else { | ||
35 | 1 | *(ptr[1]) = 1; | |
36 | } | ||
37 | 865 | return 0; | |
38 | } | ||
39 | |||
40 | 21 | int my_getnbr_base(char const *str, char const *base) | |
41 | { | ||
42 | 21 | int result = 0; | |
43 | 21 | int signe_save = 0; | |
44 | 21 | int signe = 1; | |
45 | 21 | int error = 0; | |
46 | int index[2]; | ||
47 | int *ptr[2]; | ||
48 | |||
49 |
2/2✓ Branch 1 taken 62 times.
✓ Branch 2 taken 21 times.
|
83 | for (int i = my_strlen(str) - 1; i >= 0; i--) { |
50 |
6/6✓ Branch 1 taken 928 times.
✓ Branch 2 taken 60 times.
✓ Branch 3 taken 927 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 926 times.
✓ Branch 6 taken 1 times.
|
988 | for (int j = 0; j < my_strlen(base) && error == 0 && signe == 1; j++) { |
51 | 926 | index[0] = i; | |
52 | 926 | index[1] = j; | |
53 | 926 | ptr[0] = &signe; | |
54 | 926 | ptr[1] = &error; | |
55 | 926 | result = result + calculate_base(str, base, index, ptr); | |
56 | } | ||
57 | 62 | signe_save = signe; | |
58 | 62 | signe = 1; | |
59 | } | ||
60 | 21 | return result * signe_save; | |
61 | } | ||
62 |