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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user