From f2c69cb7987644bd2a460cf59a55bd901510ae3d Mon Sep 17 00:00:00 2001 From: joan cordelier Date: Thu, 11 Apr 2024 03:15:56 +0200 Subject: [PATCH] RM: major error of parsing --- include/my.h | 2 ++ include/struct.h | 2 +- src/free.c | 7 +++++++ src/get_start_end_robot.c | 8 ++++++-- src/main.c | 10 ++++++---- src/my_getnbr.c | 2 +- src/str/remoove_tag_str.c | 8 ++++---- src/struct_utils.c | 41 +++++++++++++-------------------------- 8 files changed, 41 insertions(+), 39 deletions(-) diff --git a/include/my.h b/include/my.h index b38d651..bc6a675 100644 --- a/include/my.h +++ b/include/my.h @@ -27,6 +27,7 @@ void free_graph(void *data); //init void init_parsing(parsing_t **lab); +void init_solution(int **matrice, int param[4], void *data); //Put int mini_printf(const char *format, ...); @@ -54,6 +55,7 @@ int get_start(void *data); int my_getnbr(char const *str); //Free +int free_all(parsing_t *lab, char **tab); void free_tab(char **tab); #endif /* MYH */ diff --git a/include/struct.h b/include/struct.h index 4fae1db..c1a5601 100644 --- a/include/struct.h +++ b/include/struct.h @@ -24,7 +24,7 @@ typedef struct parsing_s { } parsing_t; typedef struct solutions_s { - int *path; + char *path; int len; struct solutions_s *next; } solutions_t; diff --git a/src/free.c b/src/free.c index d73805d..a1eaa6b 100644 --- a/src/free.c +++ b/src/free.c @@ -7,6 +7,13 @@ #include "my.h" +int free_all(parsing_t *lab, char **tab) +{ + free_lst(&lab); + free_tab(tab); + return 0; +} + void free_tab(char **tab) { for (int i = 0; i < tab_len(tab); i++) diff --git a/src/get_start_end_robot.c b/src/get_start_end_robot.c index 1837ec3..3787dfb 100644 --- a/src/get_start_end_robot.c +++ b/src/get_start_end_robot.c @@ -39,14 +39,18 @@ int get_nb_robot(void *data) int get_start(void *data) { int end = -1; + int is_tunnel = -1; parsing_t **lab = (parsing_t **) data; reverse(lab); for (parsing_t *temp = (*lab)->next; temp != NULL; temp = temp->next) { - if ((temp)->next != NULL && my_strcmp((temp)->str, "##start") == 0) { + if ((temp)->next != NULL && my_strcmp((temp)->str, "##start") == 0) end = my_getnbr(temp->next->str); - } + if ((temp) != NULL && is_in_str((temp)->str, '-') != 0) + is_tunnel++; } reverse(lab); + if (is_tunnel == -1) + return is_tunnel; return end; } diff --git a/src/main.c b/src/main.c index ff656e4..1e029bc 100644 --- a/src/main.c +++ b/src/main.c @@ -146,14 +146,16 @@ int main(void) param[END] = get_end(&lab); param[ROBOT] = get_nb_robot(&lab); param[LEN] = my_get_biggest_nb(tab); + for (int i = 0; i < 3; i++) { + if (param[i] == -1) + return 84; + } moove(matrice, param); - free_lst(&lab); - free_tab(tab); - return 0; + return free_all(lab, tab); } /*for (int i = 0; i < 3; i++) { - printf("%d\n", param[i]); + mini_printf("%d\n", param[i]); if (param[i] == -1) return 84; }*/ diff --git a/src/my_getnbr.c b/src/my_getnbr.c index 14a8a12..2fa3b37 100644 --- a/src/my_getnbr.c +++ b/src/my_getnbr.c @@ -56,7 +56,7 @@ int my_getnbr(char const *str) i = nb_0(str, i); while (str[i + len_nb] >= '0' && str[i + len_nb] <= '9') len_nb++; - if (len_nb > 15 || len_nb == 0 && str[0] != '0') + if (len_nb > 15 || (len_nb == 0 && str[0] != '0')) return (-1); nb = get_nb_provi(puissance, len_nb, str, i); nb_final = test_max_int(neg, nb); diff --git a/src/str/remoove_tag_str.c b/src/str/remoove_tag_str.c index f59bafb..5da746e 100644 --- a/src/str/remoove_tag_str.c +++ b/src/str/remoove_tag_str.c @@ -12,7 +12,8 @@ char *rm_tag_str(char *str) char *cpy = NULL; int j = 0; - if (my_strcmp(str, "##start") == 0 || my_strcmp(str, "##end") == 0) + if (my_strcmp(str, "##start") == 0 || my_strcmp(str, "##end") == 0 || + str[0] == '#') return str; cpy = malloc(sizeof(char) * my_strlen(str)); for (int i = j; str[i] != '\0'; i++) { @@ -23,9 +24,8 @@ char *rm_tag_str(char *str) cpy[i] = str[i]; j++; } - if (j <= 1) - return str; - j--; + if (cpy[j] != '\0') + j--; cpy[j] = '\0'; return cpy; } diff --git a/src/struct_utils.c b/src/struct_utils.c index 2354dc1..7ff62cd 100644 --- a/src/struct_utils.c +++ b/src/struct_utils.c @@ -7,50 +7,37 @@ #include "my.h" -int nb_solutions(int **matrice, int start, int len) +char *init_shortest_path(int **matrice, int param[4]) { - int size = 0; - - for (int i = 0; i < len; i ++) { - if (matrice[start][i] == 1) - size++; - } - return size; -} - -int *init_shortest_path(int **matrice, int start, int end, int len) -{ - int *path = NULL; + char *path = NULL; return path; } -solutions_t *add_solution(int **matrice, int start, int end, int len) +int add_solution(int **matrice, int param[4], void *data) { + solutions_t **sol = (solutions_t **) data; solutions_t *new = NULL; - int size = nb_solutions(matrice, start, len); + int last = 0; new = malloc(sizeof(solutions_t)); new->path = NULL; new->len = -1; new->next = NULL; - return new; + return last; } -solutions_t *init_solutions(int **matrice, int start, int end, int len) +void init_solution(int **matrice, int param[4], void *data) { - solutions_t *sol = NULL; - int size = nb_solutions(matrice, start, len); + solutions_t **sol = (solutions_t **) data; + int size = 0; - sol = malloc(sizeof(solutions_t)); - sol->path = NULL; - sol->len = -1; - sol->next = NULL; - for (int i = 0; i < size; i++) { - sol->next = add_solution(matrice, start, end, len); - sol = sol->next; + (*sol)->path = NULL; + (*sol)->len = -1; + (*sol)->next = NULL; + while (size == 0) { + size = add_solution(matrice, param, sol); } - return sol; } void init_parsing(parsing_t **lab)