gestion doublons ok
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
25
srcs/stop.c
25
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user