From c582aec0babbea067a7688122c0d7e77a834e20f Mon Sep 17 00:00:00 2001 From: kinou-p Date: Fri, 7 Jan 2022 19:28:04 +0100 Subject: [PATCH] test --- Makefile | 15 ++++--- libft/ft_lstclear.c | 4 +- libft/ft_lstnew.c | 4 +- libft/libft.h | 5 ++- process.c | 11 ++++- push_swap.c | 102 ++++++++++++++++++++++++++------------------ push_swap.h | 14 ++++-- sort3.c | 23 +++++++++- 8 files changed, 117 insertions(+), 61 deletions(-) diff --git a/Makefile b/Makefile index 584cc2c..4530356 100644 --- a/Makefile +++ b/Makefile @@ -6,22 +6,27 @@ # By: apommier +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2021/11/13 13:06:47 by apommier #+# #+# # -# Updated: 2021/11/25 23:57:06 by apommier ### ########.fr # +# Updated: 2022/01/06 23:59:23 by apommier ### ########.fr # # # # **************************************************************************** # NAME = push_swap SRCS = push_swap.c \ process.c\ - main.c + main.c\ + indexing.c OBJS = ${SRCS:.c=.o} -CFLAGS = -Wall -Wextra +CFLAGS = RM = rm -rf LIBFT = ./libft +2:${OBJS} + gcc -g ${CFLAGS} ${OBJS} ${LIBFT}/libft.a + mv a.out push_swap + ${NAME}:${OBJS} make bonus -C ${LIBFT} - gcc ${CFLAGS} ${OBJS} ${LIBFT}/libft.a + gcc -g ${CFLAGS} ${OBJS} ${LIBFT}/libft.a mv a.out push_swap all: ${NAME} @@ -36,5 +41,5 @@ fclean: clean re: fclean all -.PHONY: all clean fclean re bonus +.PHONY: all clean fclean re bonus diff --git a/libft/ft_lstclear.c b/libft/ft_lstclear.c index 4aa7802..1251484 100644 --- a/libft/ft_lstclear.c +++ b/libft/ft_lstclear.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ft_lstclear.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/09 19:58:04 by apommier #+# #+# */ -/* Updated: 2020/12/12 09:15:23 by apommier ### ########.fr */ +/* Updated: 2022/01/07 17:55:06 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/libft/ft_lstnew.c b/libft/ft_lstnew.c index 7dcf87d..346f1d5 100644 --- a/libft/ft_lstnew.c +++ b/libft/ft_lstnew.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ft_lstnew.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/09 01:06:20 by apommier #+# #+# */ -/* Updated: 2020/12/11 17:46:20 by apommier ### ########.fr */ +/* Updated: 2022/01/06 01:35:14 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/libft/libft.h b/libft/libft.h index c1ab8c5..c3e045a 100644 --- a/libft/libft.h +++ b/libft/libft.h @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* libft.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/11 03:21:27 by apommier #+# #+# */ -/* Updated: 2020/12/11 15:43:19 by apommier ### ########.fr */ +/* Updated: 2022/01/07 17:55:25 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,7 @@ typedef struct s_list { void *nbr; + int index; struct s_list *next; } t_list; diff --git a/process.c b/process.c index dc06e90..f5679b2 100644 --- a/process.c +++ b/process.c @@ -12,7 +12,7 @@ #include "push_swap.h" -void ft_sa_sb(t_list **list, t_list **process, char *type) +void ft_sa_sb(t_list **list, t_list **process, char type) { t_list *swap; @@ -22,7 +22,7 @@ void ft_sa_sb(t_list **list, t_list **process, char *type) (*list)->next->next = swap; } -void ft_ra_rb(t_list **list, t_list **process, char *type) +void ft_ra_rb(t_list **list, t_list **process, char type) { t_list *swap; @@ -49,3 +49,10 @@ void ft_pb(t_list **list_a, t_list **list_b, t_list **process) *list_a = (*list_a)->next; ft_lstadd_front(list_b, swap); } + +void ft_rra_rrb(t_list **list, t_list **process, char type) +{ + t_list *swap; + + swap = *list; +} diff --git a/push_swap.c b/push_swap.c index 207397b..c7000d6 100644 --- a/push_swap.c +++ b/push_swap.c @@ -6,25 +6,35 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/25 23:10:37 by apommier #+# #+# */ -/* Updated: 2021/11/26 00:00:53 by apommier ### ########.fr */ +/* Updated: 2022/01/07 18:57:42 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ #include "push_swap.h" +void printf_list(t_list *start) +{ + int i = 0; + + printf("----printf_list----\n"); + while(start) + { + i++; + printf("lst%d --- nbr: %d index: %d\n", i,*(int*)start->nbr, start->index); + start = start->next; + } + return; +} + int is_nbr(int nbrarg, char **list) { int i; int j; i = 0; - j = 0; - printf("test\n"); - + j = 0; 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); @@ -32,10 +42,8 @@ int is_nbr(int nbrarg, char **list) { if (list[j][i] < '0' || list[j][i] > '9') { - printf("0000\n"); return (0); } - printf("char = %c\n", list[j][i]); i++; } } @@ -45,25 +53,27 @@ int is_nbr(int nbrarg, char **list) int is_double(int nbrarg, char **list) { - int i; + int i; int j; - char *save; + int len; - i = 0; - j = 0; - save = *list + 1; - while (save + j + 1) + i = 1; + j = 1; + nbrarg--; + while (nbrarg - j) { - while (j + i + save) + i = 1; + while(list[j + i]) { - i++; - if (ft_strncmp(save + j, save + j + i, ft_strlen(save)) == 0) - { + if (ft_strlen(list[j]) > ft_strlen(list[j + i])) + len = ft_strlen(list[j]); + else + len = ft_strlen(list[j + i]); + if (ft_strncmp(list[j], list[j + i], len) == 0) return (0); - } + i++; } j++; - i = 0; } return (1); } @@ -73,44 +83,52 @@ int push_swap(int nbrarg, char **list) t_list *start; start = 0; - if (!is_nbr(nbrarg, list)) + if (!is_nbr(nbrarg, list) || !is_double(nbrarg, list)) { ft_putstr_fd("Error\n", 2); return (0); } - else + start = set_list(--nbrarg, list); + lst_indexing(start); + printf_list(start); + if (is_sorted(start)) { - printf("done"); - return(1); - } - start = set_list(nbrarg++, list); - if (is_sorted(*list)) + ft_lstclear(&start, &free); return (1); - else - choose_algo(start); + } + ft_lstclear(&start, &free); return (1); } -t_list *set_list(int nbrarg, char **list) +t_list *set_list(int nbrarg, char **srcs) { - t_list *start; + int *tmp; + t_list *start; + t_list *swap; - (*list)++; - start = ft_lstnew(*list); - while (nbrarg) + start = 0; + swap = 0; + *(srcs)++; + tmp = ft_calloc(sizeof(int), 1); + *tmp = ft_atoi(*srcs); + start = ft_lstnew(tmp); + swap = start; + while (--nbrarg) { - (*list)++; - ft_lstadd_back(&start, ft_lstnew(*list)); + *(srcs)++; + tmp = ft_calloc(sizeof(int), 1); + *tmp = ft_atoi(*srcs); + swap->next = ft_lstnew(tmp); + swap = swap->next; } return (start); } - int is_sorted(t_list *list) { while (list->next) { - if (list->nbr > list->next->nbr) + if (*(int*)list->nbr > *(int*)list->next->nbr) return (0); else list = list->next; @@ -118,11 +136,11 @@ int is_sorted(t_list *list) return (1); } -choose_algo(t_list *list) +/*void choose_algo(t_list *list) { int size; - size = lst_size(list); + size = ft_lstsize(list); if (size == 3) - sort3(list ); -} \ No newline at end of file + sort3(list); +}*/ \ No newline at end of file diff --git a/push_swap.h b/push_swap.h index 97bec83..3285426 100644 --- a/push_swap.h +++ b/push_swap.h @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/25 23:15:17 by apommier #+# #+# */ -/* Updated: 2021/11/25 23:48:20 by apommier ### ########.fr */ +/* Updated: 2022/01/06 04:38:07 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,15 +17,21 @@ # include # include "./libft/libft.h" -typedef struct action_list +/*typedef struct process_list { void *action; - struct action_list *next; -} action_list; + struct process_list *next; +} action_list;*/ +void sort3(t_list *list); int push_swap(int nbrarg, char **list); int is_nbr(int nbrarg, char **list); t_list *set_list(int nbrarg, char **list); +void lst_indexing(t_list *list); +int is_double(int nbrarg, char **list); +int is_sorted(t_list *list); + + #endif \ No newline at end of file diff --git a/sort3.c b/sort3.c index d8a24d2..db94417 100644 --- a/sort3.c +++ b/sort3.c @@ -14,9 +14,28 @@ void sort3(t_list *list) { + if (list->nbr < list->next->nbr && list->nbr < list->next->next->nbr) + { + ft_sa_sb(&list, process, 'a'); + ft_ra_rb(&list, process, 'a'); + } if (list->nbr > list->next->nbr && list->nbr > list->next->next->nbr) { - if (list->next->nbr < list->next->next->nbr) - + if (list->next->nbr > list->next->next->nbr) + { + ft_sa_sb(&list, process, 'a'); + ft_ra_rb(&list, process, 'a'); + ft_ra_rb(&list, process, 'a'); + } + else + ft_ra_rb(&list, process, 'a'); } + if (list->nbr > list->next->nbr && list->nbr < list->next->next->nbr) + ft_sa_sb(&list, process, 'a'); + if (list->nbr < list->next->nbr && list->next->next->nbr) + { + ft_ra_rb(&list, process, 'a'); + ft_ra_rb(&list, process, 'a'); + } + return ; } \ No newline at end of file