add_to_list a la norme et sans erreurs visibles (a part du charabia de valgrind a comprendre plus tard :p)

This commit is contained in:
Hugo LAMY
2019-04-15 17:19:11 +02:00
3 changed files with 12 additions and 18 deletions

BIN
a.out

Binary file not shown.

View File

@@ -6,7 +6,7 @@
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */ /* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/14 15:20:53 by hulamy #+# #+# */ /* Created: 2019/04/14 15:20:53 by hulamy #+# #+# */
/* Updated: 2019/04/15 16:41:09 by hulamy ### ########.fr */ /* Updated: 2019/04/15 17:16:09 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -18,7 +18,7 @@
** with the most little rectangle that fit the tetraminos ** with the most little rectangle that fit the tetraminos
*/ */
char **fill_tetraminos(char **square, int x1, int y1, int x2, int y2) char **fill_tetraminos(char **square, int *tab)
{ {
char **result; char **result;
int height; int height;
@@ -27,11 +27,11 @@ char **fill_tetraminos(char **square, int x1, int y1, int x2, int y2)
int j; int j;
i = 0; i = 0;
height = x2 - x1 + 1; height = tab[2] - tab[0] + 1;
length = y2 - y1 + 1; length = tab[3] - tab[1] + 1;
if (!(result = (char**)malloc(sizeof(*result) * (height + 1)))) if (!(result = (char**)malloc(sizeof(*result) * (height))))
return (NULL); return (NULL);
while (i < 4) while (i < height)
{ {
if (!(result[i] = (char*)malloc(sizeof(**result) * (length + 1)))) if (!(result[i] = (char*)malloc(sizeof(**result) * (length + 1))))
return (NULL); return (NULL);
@@ -41,7 +41,7 @@ char **fill_tetraminos(char **square, int x1, int y1, int x2, int y2)
i = -1; i = -1;
while (++i < height && (j = -1)) while (++i < height && (j = -1))
while (++j < length) while (++j < length)
result[i][j] = square[x1 + i][y1 + j]; result[i][j] = square[tab[0] + i][tab[1] + j];
return (result); return (result);
} }
@@ -94,21 +94,13 @@ void find_start_and_end(char **square, int **x)
int fill_list(char **square, t_fillist *list) int fill_list(char **square, t_fillist *list)
{ {
int *tab; int *tab;
int x1;
int x2;
int y1;
int y2;
tab = (int*)malloc(sizeof(int) * 4); tab = (int*)malloc(sizeof(int) * 4);
find_start_and_end(square, &tab); find_start_and_end(square, &tab);
x1 = tab[0]; list->size[0] = tab[3] - tab[1] + 1;
y1 = tab[1]; list->size[1] = tab[2] - tab[0] + 1;
x2 = tab[2];
y2 = tab[3];
list->size[0] = y2 - y1 + 1;
list->size[1] = x2 - x1 + 1;
list->area = list->size[0] * list->size[1]; list->area = list->size[0] * list->size[1];
list->tetraminos = fill_tetraminos(square, x1, y1, x2, y2); list->tetraminos = fill_tetraminos(square, tab);
return (1); return (1);
} }
@@ -135,6 +127,7 @@ int add_to_list(char **square, t_fillist **list)
return (1); return (1);
} }
int main(int ac, char **av) int main(int ac, char **av)
{ {
static t_fillist *list = NULL; // avant d'appeller add_to_list il faut declarer un pointeur static vers la structure static t_fillist *list = NULL; // avant d'appeller add_to_list il faut declarer un pointeur static vers la structure
@@ -156,3 +149,4 @@ int main(int ac, char **av)
return (0); return (0);
} }