This commit is contained in:
kinou-p 2022-01-19 06:16:40 +01:00
parent e9fdc6a0dc
commit ce00d75eb3
7 changed files with 56 additions and 48 deletions

View File

@ -6,7 +6,7 @@
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ # # By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2021/11/13 13:06:47 by apommier #+# #+# # # Created: 2021/11/13 13:06:47 by apommier #+# #+# #
# Updated: 2022/01/19 03:50:25 by apommier ### ########.fr # # Updated: 2022/01/19 05:41:28 by apommier ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -42,7 +42,7 @@ LIBFT = ./libft
${NAME}:${OBJS} ${NAME}:${OBJS}
make bonus -C ${LIBFT} make bonus -C ${LIBFT}
gcc -g ${OBJS} ${LIBFT}/libft.a gcc -g ${OBJS} ${LIBFT}/libft.a
mv a.out push_swap mv a.out push_swap
bonus:${BONUS_O} bonus:${BONUS_O}

View File

@ -35,3 +35,28 @@ void lst_indexing(t_list *list)
save = save->next; save = save->next;
} }
} }
t_list *is_double_one(t_list *lst)
{
t_list *start;
t_list *swap;
start = lst;
swap = 0;
while (lst)
{
swap = lst->next;
while (swap)
{
if (*(int *)lst->nbr == *(int *)swap->nbr)
{
ft_putstr_fd("Error\n", 2);
ft_lstclear(&start, &free);
return (0);
}
swap = swap->next;
}
lst = lst->next;
}
return (start);
}

View File

@ -6,18 +6,16 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/25 23:10:37 by apommier #+# #+# */ /* Created: 2021/11/25 23:10:37 by apommier #+# #+# */
/* Updated: 2022/01/18 06:08:53 by apommier ### ########.fr */ /* Updated: 2022/01/19 05:53:41 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "push_swap.h" #include "push_swap.h"
int is_nbr(int nbrarg, char **list) int is_nbr(int nbrarg, char **list, int i)
{ {
int i;
int j; int j;
i = 0;
j = 0; j = 0;
while (--nbrarg) while (--nbrarg)
{ {
@ -29,12 +27,14 @@ int is_nbr(int nbrarg, char **list)
i++; i++;
if (list[j][i] == '-' || (list[j][i] == '+')) if (list[j][i] == '-' || (list[j][i] == '+'))
i++; i++;
if (list[j][i] && (list[j][i] < '0' || list[j][i] > '9')) if (!list[j][i] && (list[j][i] < '0' || list[j][i] > '9'))
return (0); return (0);
while (list[j][i] && list[j][i] >= '0' && list[j][i] <= '9') while (list[j][i] && list[j][i] >= '0' && list[j][i] <= '9')
i++; i++;
if (list[j][i] && list[j][i] != ' ') if (list[j][i] && list[j][i] != ' ')
return (0); return (0);
while (list[j][i] == ' ')
i++;
} }
} }
return (1); return (1);
@ -73,27 +73,30 @@ int push_swap(int nbrarg, char **list)
int lst_size; int lst_size;
start = 0; start = 0;
if (!is_nbr(nbrarg, list) || !is_double(nbrarg, list)) if (!is_nbr(nbrarg, list, 0) || !is_double(nbrarg, list))
{ {
ft_putstr_fd("Error\n", 2); ft_putstr_fd("Error\n", 2);
return (0); return (0);
} }
start = set_list(--nbrarg, list); start = set_list(--nbrarg, list);
lst_indexing(start);
start = is_double_one(start);
if (!start)
return (0);
lst_size = ft_lstsize(start); lst_size = ft_lstsize(start);
if (lst_size == 1) if (lst_size == 1)
return (1); return (1);
lst_indexing(start);
if (lst_size == 3 || lst_size == 5) if (lst_size == 3 || lst_size == 5)
{
sort_little(start); sort_little(start);
return (1); else
{
start = find_loop_index(start);
ft_lstclear(&start, &free);
} }
start = find_loop_index(start);
ft_lstclear(&start, &free);
return (1); return (1);
} }
t_list *set_list(int nbrarg, char **srcs) t_list *set_list(int nbrarg, char **src)
{ {
int *tmp; int *tmp;
int i; int i;
@ -107,14 +110,14 @@ t_list *set_list(int nbrarg, char **srcs)
while (nbrarg--) while (nbrarg--)
{ {
i = 0; i = 0;
while (srcs[j][i]) while (src[j][i])
{ {
tmp = ft_calloc(sizeof(int), 1); tmp = ft_calloc(sizeof(int), 1);
*tmp = ft_atoi(&srcs[j][i]); *tmp = ft_atoi(&src[j][i]);
ft_lstadd_back(&swap, ft_lstnew(tmp)); ft_lstadd_back(&swap, ft_lstnew(tmp));
while (srcs[j][i] == ' ') while ((src[j][i] >= '0' && src[j][i] <= '9') || src[j][i] == '-')
i++; i++;
while (srcs[j][i] && srcs[j][i] != ' ') while (src[j][i] == ' ')
i++; i++;
} }
j++; j++;

View File

@ -6,14 +6,13 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/25 23:15:17 by apommier #+# #+# */ /* Created: 2021/11/25 23:15:17 by apommier #+# #+# */
/* Updated: 2022/01/19 03:53:31 by apommier ### ########.fr */ /* Updated: 2022/01/19 06:06:31 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef PUSH_SWAP_H #ifndef PUSH_SWAP_H
# define PUSH_SWAP_H # define PUSH_SWAP_H
# include <stdio.h>
# include <unistd.h> # include <unistd.h>
# include "../libft/libft.h" # include "../libft/libft.h"
# include "../libft/get_next_line.h" # include "../libft/get_next_line.h"
@ -25,10 +24,11 @@ typedef struct procest_slist
} t_slist; } t_slist;
int push_swap(int nbrarg, char **list); int push_swap(int nbrarg, char **list);
int is_nbr(int nbrarg, char **list); int is_nbr(int nbrarg, char **list, int i);
t_list *set_list(int nbrarg, char **list); t_list *set_list(int nbrarg, char **list);
void lst_indexing(t_list *list); void lst_indexing(t_list *list);
int is_double(int nbrarg, char **list); int is_double(int nbrarg, char **list);
t_list *is_double_one(t_list *lst);
t_list *find_loop_index(t_list *list); t_list *find_loop_index(t_list *list);
int find_loop(t_list *start, t_list *list, int setswap); int find_loop(t_list *start, t_list *list, int setswap);
t_list *ft_lstnew(void *content); t_list *ft_lstnew(void *content);

View File

@ -6,33 +6,12 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/18 03:31:31 by apommier #+# #+# */ /* Created: 2022/01/18 03:31:31 by apommier #+# #+# */
/* Updated: 2022/01/19 03:32:05 by apommier ### ########.fr */ /* Updated: 2022/01/19 06:03:44 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "push_swap.h" #include "push_swap.h"
//t_list *end_sort3(t_list *list, t_slist **action);
//{
//}
void printf_llist(t_list *start)
{
int i;
i = 0;
printf("----printf_list----\n");
while (start)
{
i++;
printf("lst%d--nbr:%d indx: %d\n", i,*(int *)start->nbr, start->index);
start = start->next;
}
printf("----end_print----");
return ;
}
void sort_little(t_list *list) void sort_little(t_list *list)
{ {
t_slist *action; t_slist *action;

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/18 02:14:22 by apommier #+# #+# */ /* Created: 2022/01/18 02:14:22 by apommier #+# #+# */
/* Updated: 2022/01/19 03:42:58 by apommier ### ########.fr */ /* Updated: 2022/01/19 06:10:06 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -28,5 +28,6 @@ int do_move2(t_list **lst, t_list **lst_b, t_slist *a, t_slist **delete)
if (!rra_rrb(lst_b, delete, 'a')) if (!rra_rrb(lst_b, delete, 'a'))
return (0); return (0);
} }
s_lstclear(delete);
return (1); return (1);
} }

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/17 21:29:45 by apommier #+# #+# */ /* Created: 2022/01/17 21:29:45 by apommier #+# #+# */
/* Updated: 2022/01/19 03:44:06 by apommier ### ########.fr */ /* Updated: 2022/01/19 06:07:41 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -50,7 +50,7 @@ t_slist *verify_list(t_slist *a)
return (start); return (start);
} }
int do_move(t_list **lst, t_list **lst_b, t_slist *a, t_slist *delete) int do_move(t_list **lst, t_list **lst_b, t_slist *a, t_slist *delete)
{ {
if (!ft_strcmp("pa\n", a) && !pa(lst, lst_b, &delete)) if (!ft_strcmp("pa\n", a) && !pa(lst, lst_b, &delete))
return (0); return (0);
@ -104,7 +104,7 @@ int main(int argc, char **argv)
start_action = 0; start_action = 0;
if (argc == 1) if (argc == 1)
return (1); return (1);
if (!is_nbr(argc, argv) || !is_double(argc, argv)) if (!is_nbr(argc, argv, 0) || !is_double(argc, argv))
{ {
ft_putstr_fd("Error\n", 2); ft_putstr_fd("Error\n", 2);
return (0); return (0);