128 lines
2.3 KiB
C
128 lines
2.3 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* push_swap.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2021/11/25 23:10:37 by apommier #+# #+# */
|
|
/* Updated: 2021/11/26 00:00:53 by apommier ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "push_swap.h"
|
|
|
|
int is_nbr(int nbrarg, char **list)
|
|
{
|
|
int i;
|
|
int j;
|
|
|
|
i = 0;
|
|
j = 0;
|
|
printf("test\n");
|
|
|
|
while (++j < nbrarg)
|
|
{
|
|
printf("string= %s\n", list[j]);
|
|
printf("j1= %d\n", j);
|
|
i = 0;
|
|
if (list[j][i] == '-' && !list[j][i++])
|
|
return (0);
|
|
while (list[j][i])
|
|
{
|
|
if (list[j][i] < '0' || list[j][i] > '9')
|
|
{
|
|
printf("0000\n");
|
|
return (0);
|
|
}
|
|
printf("char = %c\n", list[j][i]);
|
|
i++;
|
|
}
|
|
}
|
|
|
|
return (1);
|
|
}
|
|
|
|
int is_double(int nbrarg, char **list)
|
|
{
|
|
int i;
|
|
int j;
|
|
char *save;
|
|
|
|
i = 0;
|
|
j = 0;
|
|
save = *list + 1;
|
|
while (save + j + 1)
|
|
{
|
|
while (j + i + save)
|
|
{
|
|
i++;
|
|
if (ft_strncmp(save + j, save + j + i, ft_strlen(save)) == 0)
|
|
{
|
|
return (0);
|
|
}
|
|
}
|
|
j++;
|
|
i = 0;
|
|
}
|
|
return (1);
|
|
}
|
|
|
|
int push_swap(int nbrarg, char **list)
|
|
{
|
|
t_list *start;
|
|
|
|
start = 0;
|
|
if (!is_nbr(nbrarg, list))
|
|
{
|
|
ft_putstr_fd("Error\n", 2);
|
|
return (0);
|
|
}
|
|
else
|
|
{
|
|
printf("done");
|
|
return(1);
|
|
}
|
|
start = set_list(nbrarg++, list);
|
|
if (is_sorted(*list))
|
|
return (1);
|
|
else
|
|
choose_algo(start);
|
|
return (1);
|
|
}
|
|
|
|
t_list *set_list(int nbrarg, char **list)
|
|
{
|
|
t_list *start;
|
|
|
|
(*list)++;
|
|
start = ft_lstnew(*list);
|
|
while (nbrarg)
|
|
{
|
|
(*list)++;
|
|
ft_lstadd_back(&start, ft_lstnew(*list));
|
|
}
|
|
return (start);
|
|
}
|
|
|
|
|
|
int is_sorted(t_list *list)
|
|
{
|
|
while (list->next)
|
|
{
|
|
if (list->nbr > list->next->nbr)
|
|
return (0);
|
|
else
|
|
list = list->next;
|
|
}
|
|
return (1);
|
|
}
|
|
|
|
choose_algo(t_list *list)
|
|
{
|
|
int size;
|
|
|
|
size = lst_size(list);
|
|
if (size == 3)
|
|
sort3(list );
|
|
} |