gestion doublons ok

This commit is contained in:
hugogogo
2021-09-27 14:07:52 +02:00
parent 60752e2faf
commit 2d37058a43
7 changed files with 57 additions and 27 deletions

Binary file not shown.

Binary file not shown.

BIN
correction_pushswap.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 MiB

View File

@@ -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 print_test(t_list *lst, char *s);
void ps_usage(void); void ps_usage(void);
void ps_error(int i); 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 ** algo.c // sort.c

BIN
push_swap

Binary file not shown.

View File

@@ -4,18 +4,47 @@ int is_ordered(t_stack *list)
{ {
int cmp; int cmp;
cmp = list->n; while (list && list->next)
while (list->next != NULL)
{ {
cmp = list->n;
list = list->next; list = list->next;
ft_printf("n:%i cmp:%i\n", cmp, list->n);
if (list->n < cmp) if (list->n < cmp)
return (0); return (0);
cmp = list->n;
} }
return (1); 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) int check_flag(int *ac, char ***av)
{ {
if (ft_strcmp((*av)[1], "-p") == 0) if (ft_strcmp((*av)[1], "-p") == 0)
@@ -33,21 +62,21 @@ int check_flag(int *ac, char ***av)
return (0); 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; t_stack *new;
long nbl; long nbl;
new = ft_calloc(1, sizeof(t_stack)); new = ft_calloc(1, sizeof(t_stack));
if (!new) if (!new)
ps_stop((t_list *)list, 3); ps_stop(tab, (t_list *)list, 3);
new->next = list; new->next = list;
list = new; list = new;
if (ft_isnumber(nb) == 0) if (ft_isnumber(nb) == 0)
ps_stop((t_list *)list, 3); ps_stop(tab, (t_list *)list, 3);
nbl = ft_atol(nb); nbl = ft_atol(nb);
if (nbl < INT_MIN || nbl > INT_MAX) if (nbl < INT_MIN || nbl > INT_MAX)
ps_stop((t_list *)list, 3); ps_stop(tab, (t_list *)list, 3);
list->n = nbl; list->n = nbl;
list->limit = 0; list->limit = 0;
return (list); return (list);
@@ -62,7 +91,7 @@ t_stack *init_stack(int ac, char **av)
list = NULL; list = NULL;
if (ac > 2) if (ac > 2)
while (--ac) while (--ac)
list = fill_stack(list, av[ac]); list = fill_stack(list, av[ac], av);
else if (ac == 2) else if (ac == 2)
{ {
split = ft_split(av[1], ' '); split = ft_split(av[1], ' ');
@@ -70,7 +99,7 @@ t_stack *init_stack(int ac, char **av)
while (split[i] != NULL) while (split[i] != NULL)
i++; i++;
while (--i >= 0) while (--i >= 0)
list = fill_stack(list, split[i]); list = fill_stack(list, split[i], split);
ft_free_tab(split); ft_free_tab(split);
} }
return (list); return (list);
@@ -109,15 +138,17 @@ int main(int ac, char **av)
int flag; int flag;
if (ac < 2) if (ac < 2)
ps_stop(NULL, 1); ps_stop(NULL, NULL, 1);
flag = check_flag(&ac, &av); flag = check_flag(&ac, &av);
a = init_stack(ac, av); a = init_stack(ac, av);
b = NULL; b = NULL;
if (is_ordered(a) == 1) if (check_valid(a) == 1)
{ {
solution = launch_algo(&a, &b, flag); solution = launch_algo(&a, &b, flag);
print_result(solution, flag); print_result(solution, flag);
ps_stop(NULL, solution, 0);
} }
ps_stop(solution, 0); else
ps_stop(NULL, (t_list *)a, -1);
return (0); return (0);
} }

View File

@@ -1,25 +1,18 @@
#include "push_swap.h" #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 = 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 = 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 **a;
t_stack **b; t_stack **b;
if (err == 3) if (tab != NULL)
ft_free_tab(tab);
if (err == 3 || err == -1)
ft_lstclear(&lst, NULL); ft_lstclear(&lst, NULL);
else if (lst) else if (lst)
{ {
@@ -32,5 +25,11 @@ void ps_stop(t_list *lst, int err)
ft_lstclear(&lst, free); ft_lstclear(&lst, free);
} }
if (err > 0) 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);
}
} }