diff --git a/builds/push_swap.o b/builds/push_swap.o index 145675d..634cf6e 100644 Binary files a/builds/push_swap.o and b/builds/push_swap.o differ diff --git a/builds/stop.o b/builds/stop.o index dfcc914..d289120 100644 Binary files a/builds/stop.o and b/builds/stop.o differ diff --git a/correction_pushswap.png b/correction_pushswap.png new file mode 100644 index 0000000..1fc1f96 Binary files /dev/null and b/correction_pushswap.png differ diff --git a/includes/push_swap.h b/includes/push_swap.h index d2b993b..b27230b 100644 --- a/includes/push_swap.h +++ b/includes/push_swap.h @@ -34,7 +34,7 @@ t_list *launch_algo(t_stack **a, t_stack **b, int i); void print_test(t_list *lst, char *s); void ps_usage(void); void ps_error(int i); -void ps_stop(t_list *lst, int i); +void ps_stop(char **tab, t_list *lst, int i); /* ** algo.c // sort.c diff --git a/push_swap b/push_swap index 9a1a1fd..489171c 100755 Binary files a/push_swap and b/push_swap differ diff --git a/srcs/push_swap.c b/srcs/push_swap.c index e792059..a328fb3 100644 --- a/srcs/push_swap.c +++ b/srcs/push_swap.c @@ -4,18 +4,47 @@ int is_ordered(t_stack *list) { int cmp; - cmp = list->n; - while (list->next != NULL) + while (list && list->next) { + cmp = list->n; list = list->next; - ft_printf("n:%i cmp:%i\n", cmp, list->n); if (list->n < cmp) return (0); - cmp = list->n; } return (1); } +int has_doubles(t_stack *list) +{ + t_stack *head; + int nbr; + + while (list) + { + head = list; + nbr = head->n; + while(head && head->next) + { + head = head->next; + if (head->n == nbr) + return (1); + } + list = list->next; + } + return (0); +} + +int check_valid(t_stack *list) +{ + if (!list) + return (0); + if (is_ordered(list) == 1) + return (0); + if (has_doubles(list) == 1) + ps_stop(NULL, (t_list *)list, 3); + return (1); +} + int check_flag(int *ac, char ***av) { if (ft_strcmp((*av)[1], "-p") == 0) @@ -33,21 +62,21 @@ int check_flag(int *ac, char ***av) return (0); } -t_stack *fill_stack(t_stack *list, char *nb) +t_stack *fill_stack(t_stack *list, char *nb, char **tab) { t_stack *new; long nbl; new = ft_calloc(1, sizeof(t_stack)); if (!new) - ps_stop((t_list *)list, 3); + ps_stop(tab, (t_list *)list, 3); new->next = list; list = new; if (ft_isnumber(nb) == 0) - ps_stop((t_list *)list, 3); + ps_stop(tab, (t_list *)list, 3); nbl = ft_atol(nb); if (nbl < INT_MIN || nbl > INT_MAX) - ps_stop((t_list *)list, 3); + ps_stop(tab, (t_list *)list, 3); list->n = nbl; list->limit = 0; return (list); @@ -62,7 +91,7 @@ t_stack *init_stack(int ac, char **av) list = NULL; if (ac > 2) while (--ac) - list = fill_stack(list, av[ac]); + list = fill_stack(list, av[ac], av); else if (ac == 2) { split = ft_split(av[1], ' '); @@ -70,7 +99,7 @@ t_stack *init_stack(int ac, char **av) while (split[i] != NULL) i++; while (--i >= 0) - list = fill_stack(list, split[i]); + list = fill_stack(list, split[i], split); ft_free_tab(split); } return (list); @@ -109,15 +138,17 @@ int main(int ac, char **av) int flag; if (ac < 2) - ps_stop(NULL, 1); + ps_stop(NULL, NULL, 1); flag = check_flag(&ac, &av); a = init_stack(ac, av); b = NULL; - if (is_ordered(a) == 1) + if (check_valid(a) == 1) { solution = launch_algo(&a, &b, flag); print_result(solution, flag); + ps_stop(NULL, solution, 0); } - ps_stop(solution, 0); + else + ps_stop(NULL, (t_list *)a, -1); return (0); } diff --git a/srcs/stop.c b/srcs/stop.c index 2f0b264..404c331 100644 --- a/srcs/stop.c +++ b/srcs/stop.c @@ -1,25 +1,18 @@ #include "push_swap.h" -void ps_error(int err) -{ - if (err == 1) - ft_printf("usage :\nlaunch executable : ./push_swap [flag -p] nb nb nb nb nb ...\n"); - if (err >= 2) - ft_putstr_fd("Error\n", 2); - exit(0); -} - /* ** if err = 0, the list are freed but the program doesn't stop ** if err = 1, 2 or 3, a message is printed and the program stop -** if err = 3, the parameter list is just a t_stack *list +** if err = 3 ou -1, the parameter list is just a t_stack *list */ -void ps_stop(t_list *lst, int err) +void ps_stop(char **tab, t_list *lst, int err) { t_stack **a; t_stack **b; - if (err == 3) + if (tab != NULL) + ft_free_tab(tab); + if (err == 3 || err == -1) ft_lstclear(&lst, NULL); else if (lst) { @@ -32,5 +25,11 @@ void ps_stop(t_list *lst, int err) ft_lstclear(&lst, free); } if (err > 0) - ps_error(err); + { + if (err == 1) + ft_printf("usage :\nlaunch executable : ./push_swap [flag -p] nb nb nb nb nb ...\n"); + if (err >= 2) + ft_putstr_fd("Error\n", 2); + exit(0); + } }