101 lines
2.0 KiB
C
101 lines
2.0 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;
|
|
char *save;
|
|
|
|
save = *list;
|
|
i = 0;
|
|
while (nbrarg)
|
|
{
|
|
i = 0;
|
|
save++;
|
|
while (save[i])
|
|
{
|
|
if ((save[i] < '0' || save[i] > '9') && save[i] != '-')
|
|
return (0);
|
|
i++;
|
|
}
|
|
nbrarg--;
|
|
}
|
|
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) || !is_double(nbrarg++, list))
|
|
{
|
|
ft_putstr_fd("Error\n", 2);
|
|
return (0);
|
|
}
|
|
start = set_list(nbrarg++, list);
|
|
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->content > list->next->content)
|
|
return (0);
|
|
else
|
|
list = list->next;
|
|
}
|
|
return (1);
|
|
}
|
|
|