diff --git a/Makefile b/Makefile index b2ca9d1..8794ca8 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: tmaze +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/02/23 14:30:57 by tmaze #+# #+# # -# Updated: 2019/03/06 11:37:28 by tmaze ### ########.fr # +# Updated: 2019/03/15 16:39:08 by tmaze ### ########.fr # # # #******************************************************************************# @@ -28,7 +28,7 @@ endif # Compilator CC = gcc -FLAGS = -Wall -Wextra -g +FLAGS = -Wall -Wextra -Werror # Folders LIBDIR = libft @@ -60,7 +60,7 @@ OBJP = $(addprefix $(OBJDIR)/, $(SRC:.c=.o)) OBJP1 = $(addprefix $(OBJDIR)/, $(MAIN1:.c=.o)) OBJP2 = $(addprefix $(OBJDIR)/, $(MAIN2:.c=.o)) INCP = $(foreach dir, $(INCDIR), -I$(dir)) -OBJS_DIRS = $(sort $(dir $(OBJP))) + # Default Rule DRULE = all @@ -72,17 +72,14 @@ default : all : $(NAME1) $(NAME2) -re : fclean default - # Compilation rules $(OBJDIR)/%.o : $(SRCDIR)/%.c $(OBJDIR) $(CC) $(FLAGS) -c -o $@ $< $(INCP) $(OBJDIR) : - @echo -e "$(YEL)===> $(GRE)$(NAME) : $(YEL) Objects Compilation <===$(DEF)" - @mkdir -p $(OBJDIR) - @mkdir -p $(OBJS_DIRS) + @echo -e "$(YEL)===> $(GRE)$(NAME) : $(YEL) Directory Creation <===$(DEF)" + mkdir -p $(OBJDIR) $(LIBDIR)/$(LIBFILE) : @echo -e "$(YEL)===> $(GRE)$(NAME) : $(YEL) Librairy Compilation <===$(DEF)" @@ -105,8 +102,10 @@ fclean : clean @echo -e "$(RED)===> $(GRE)$(NAME) : $(RED) Delete Binary File <===$(DEF)" @rm -f $(NAME1) $(NAME2) +re : fclean default + # Phony -.PHONY = default all re clean fclean $(OBJDIR) $(NAME) +.PHONY = default all re clean fclean # Color DEF = \033[0m BLA = \033[30m diff --git a/includes/push_swap.h b/includes/push_swap.h index 75b1800..6ff1330 100644 --- a/includes/push_swap.h +++ b/includes/push_swap.h @@ -6,7 +6,7 @@ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/02/21 14:13:53 by tmaze #+# #+# */ -/* Updated: 2019/03/10 12:14:19 by tmaze ### ########.fr */ +/* Updated: 2019/03/15 15:33:18 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,8 +17,6 @@ # include # include "libft.h" -# include - typedef struct s_stack { int nb; @@ -33,11 +31,9 @@ typedef struct s_psdata size_t size_a; size_t size_b; t_list *op; + char viz; } t_psdata; -char **ft_strsplitwhitespace(char *s); -int ft_iswhitespace(char c); - t_stack *ps_stknew(int nb); void ps_stkpsh(t_psdata *data, char c, t_stack *new); t_stack *ps_stkpop(t_psdata *data, char c); @@ -54,7 +50,7 @@ void ps_rrerot(t_psdata *data); int is_op(char *buff); t_list *add_op(t_psdata *data, char *op); -int get_params(t_psdata *data, int ac, char **av); +int get_params(t_psdata *data, int ac, char **av, int viz); int check_input(char *in, int *ret); #endif diff --git a/srcs/checker.c b/srcs/checker.c index 6b3b887..9cb754d 100644 --- a/srcs/checker.c +++ b/srcs/checker.c @@ -6,36 +6,36 @@ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/02/23 13:35:15 by tmaze #+# #+# */ -/* Updated: 2019/03/11 13:41:27 by tmaze ### ########.fr */ +/* Updated: 2019/03/15 16:52:54 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ #include "push_swap.h" -#define VISU_SPEED 100000 +#define VISU_SPEED 500000 -void debug(t_psdata *data, char *com) +void debug(t_psdata *data, char *com) { - t_stack *as; - t_stack *bs; + t_stack *as; + t_stack *bs; - as = data->a; - bs = data->b; - ft_printf(FT_CLEAR); - ft_printf("============== %4s ==============\n", com); - while (as || bs) - { - if (as && bs) - ft_printf("%5d || %5d\n", as->nb, bs->nb); - else if (!as && bs) - ft_printf("%5c || %5d\n", ' ', bs->nb); - else if (as && !bs) - ft_printf("%5d || %5c\n", as->nb, ' '); - as = (as) ? as->next : as; - bs = (bs) ? bs->next : bs; - } - ft_printf("==================================\n"); - usleep(VISU_SPEED); + as = data->a; + bs = data->b; + ft_printf(FT_CLEAR); + ft_printf("============== %4s ==============\n", com); + while (as || bs) + { + if (as && bs) + ft_printf("%5d || %5d\n", as->nb, bs->nb); + else if (!as && bs) + ft_printf("%5c || %5d\n", ' ', bs->nb); + else if (as && !bs) + ft_printf("%5d || %5c\n", as->nb, ' '); + as = (as) ? as->next : as; + bs = (bs) ? bs->next : bs; + } + ft_printf("==================================\n"); + usleep(VISU_SPEED); } void exec_actions(t_psdata *data) @@ -46,24 +46,22 @@ void exec_actions(t_psdata *data) nop = data->op; while (nop && (buff = (char*)nop->content)) { - if (ft_strlen(buff) == 2) - { - if (buff[0] == 's' && buff[1] != 's') - ps_swap(data, buff[1]); - else if (buff[0] == 's' && buff[1] == 's') - ps_sswap(data); - else if (buff[0] == 'r' && buff[1] != 'r') - ps_rot(data, buff[1]); - else if (buff[0] == 'r' && buff[1] == 'r') - ps_rrot(data); - else if (buff[0] == 'p') - ps_push(data, buff[1]); - } - else if (buff[2] != 'r') + if (ft_strcmp(buff, "rra") == 0 || ft_strcmp(buff, "rrn") == 0) ps_rerot(data, buff[2]); - else if (buff[2] == 'r') + else if (ft_strcmp(buff, "rrr") == 0) ps_rrerot(data); -// debug(data, buff); + else if (buff[0] == 's' && buff[1] != 's') + ps_swap(data, buff[1]); + else if (buff[0] == 's' && buff[1] == 's') + ps_sswap(data); + else if (buff[0] == 'r' && buff[1] != 'r') + ps_rot(data, buff[1]); + else if (buff[0] == 'r' && buff[1] == 'r') + ps_rrot(data); + else if (buff[0] == 'p') + ps_push(data, buff[1]); + if (data->viz) + debug(data, buff); nop = nop->next; } } @@ -77,10 +75,10 @@ int read_ops(t_psdata *data) buff = NULL; while ((ret = get_next_line(0, &buff)) > 0) { - if (ft_strlen(buff) > 4 || !is_op(buff) || (nop = ft_lstnew(buff, 4)) == NULL) + if (ft_strlen(buff) > 4 || !is_op(buff) + || (nop = ft_lstnew(buff, 4)) == NULL) { - ft_printf("'%s' %d %d %d\n", buff, ft_strlen(buff) > 4, !is_op(buff), nop == NULL); - ft_putendl_fd("Error 2", 2); + ft_putendl_fd("Error", 2); return (1); } ft_lstaddend(&(data->op), nop); @@ -117,11 +115,11 @@ int main(int ac, char **av) ps_initdata(&data); ret = 0; - if (!get_params(&data, ac, av)) + if (!get_params(&data, ac, av, 1)) return (0); new = data.a; if (new == NULL) - ft_putendl("Empty"); + ft_putendl_fd("Error", 2); if (new == NULL || read_ops(&data)) { ps_stkclean(&data); diff --git a/srcs/ps_stktools2.c b/srcs/ps_stktools2.c index 8c5c5df..3cdfb74 100644 --- a/srcs/ps_stktools2.c +++ b/srcs/ps_stktools2.c @@ -6,7 +6,7 @@ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/02/25 12:44:08 by tmaze #+# #+# */ -/* Updated: 2019/03/10 12:27:41 by tmaze ### ########.fr */ +/* Updated: 2019/03/15 15:36:06 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,7 @@ void ps_initdata(t_psdata *data) data->size_a = 0; data->size_b = 0; data->op = NULL; + data->viz = '\0'; } int check_input(char *in, int *ret) @@ -35,34 +36,30 @@ int check_input(char *in, int *ret) return (0); } -int get_params(t_psdata *data, int ac, char **av) +int get_params(t_psdata *data, int ac, char **av, int viz) { t_stack *new; - char **tab; int nb; int i; - int j; i = ac; nb = 0; - while (--i > 0 && !(j = 0)) - if ((tab = ft_strsplitwhitespace(av[i])) != NULL) + while (--i > 0) + { + if (i == 1 && viz && ft_strcmp(av[i], "-v") == 0) + data->viz = 'a'; + else if (check_input(av[i], &nb) && (new = ps_stknew(nb)) != NULL) { - while (tab[j]) - j++; - while (--j >= 0) - { - if (check_input(tab[j], &nb) && (new = ps_stknew(nb)) != NULL) - ps_stkpsh(data, 'a', new); - else - { - ft_putendl_fd("Error 1", 2); - ps_stkclean(data); - return (0); - } - } - ft_del_words_tables(&tab); + new->ind = 0; + ps_stkpsh(data, 'a', new); } + else + { + ft_putendl_fd("Error", 2); + ps_stkclean(data); + return (0); + } + } return (1); } diff --git a/srcs/push_swap.c b/srcs/push_swap.c index c204402..1f86dc6 100644 --- a/srcs/push_swap.c +++ b/srcs/push_swap.c @@ -6,7 +6,7 @@ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/03/03 11:41:49 by tmaze #+# #+# */ -/* Updated: 2019/03/11 16:30:23 by tmaze ### ########.fr */ +/* Updated: 2019/03/15 16:17:37 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ @@ -90,11 +90,9 @@ void sortfor5(t_psdata *data) data->a->next->next->ind = 2; while (data->a->ind == 2 || data->size_b > 0) { - if (data->a->ind != 2 - || (data->size_b > 0 && data->b->nb < data->a->nb)) + if (data->a->ind != 2 || (data->b && data->b->nb < data->a->nb)) ft_printf("pa\n"); - if (data->a->ind != 2 - || (data->size_b > 0 && data->b->nb < data->a->nb)) + if (data->a->ind != 2 || (data->b && data->b->nb < data->a->nb)) ps_push(data, 'a'); data->a->ind = 3; ft_printf("ra\n"); @@ -173,6 +171,52 @@ int get_nb_groups(t_psdata *data) return (ret); } +int join_lists(t_psdata *data, t_stack **last, int inds, int inds2) +{ + while ((*last)->ind == inds2 || data->size_b > 0) + if (((*last)->ind != inds2 && data->size_b > 0) + || (data->size_b > 0 && data->b->nb > (*last)->nb)) + { + if (add_op(data, "pa\0") == NULL) + return (0); + ps_push(data, 'a'); + data->a->ind = inds; + } + else if (data->size_b == 0 || data->b->nb < (*last)->nb) + { + if (add_op(data, "rra") == NULL) + return (0); + ps_rerot(data, 'a'); + data->a->ind = inds; + (*last) = get_last_a(data); + } + return (1); +} + +int rotate_list(t_psdata *data, t_stack **last) +{ + t_list *tmp; + + if (get_nb_groups(data) > 2) + { + if (get_nb_groups(data) != 2 + && data->a->ind != (*last)->ind && add_op(data, "rra") == NULL) + return (0); + else if (get_nb_groups(data) != 2 && data->a->ind != (*last)->ind) + { + ps_rerot(data, 'a'); + (*last) = get_last_a(data); + while ((*last)->ind == data->a->ind + && (tmp = add_op(data, "rra")) != NULL) + { + ps_rerot(data, 'a'); + (*last) = get_last_a(data); + } + } + } + return (1); +} + int sort(t_psdata *data) { t_list *tmp; @@ -185,44 +229,16 @@ int sort(t_psdata *data) while (data->a->ind != last->ind) { inds2 = data->a->ind; - while (data->a->ind == inds2 && (tmp = add_op(data, "pb\0")) != NULL) + while (data->a && data->a->ind == inds2 + && (tmp = add_op(data, "pb\0")) != NULL) ps_push(data, 'b'); if (tmp == NULL) return (0); inds2 = last->ind; - while (last->ind == inds2 || data->size_b > 0) - { - if ((last->ind != inds2 && data->size_b > 0) || (data->b && data->b->nb > last->nb)) - { - if (add_op(data, "pa\0") == NULL) - return (0); - ps_push(data, 'a'); - data->a->ind = inds; - } - else if (data->size_b == 0 || data->b->nb < last->nb) - { - if (add_op(data, "rra") == NULL) - return (0); - ps_rerot(data, 'a'); - data->a->ind = inds; - last = get_last_a(data); - } - } -// if (get_nb_groups(data) > 2) -// { - if (get_nb_groups(data) != 2 && data->a->ind != last->ind && add_op(data, "rra") == NULL) - return (0); - else if (get_nb_groups(data) != 2 && data->a->ind != last->ind) - { - ps_rerot(data, 'a'); - last = get_last_a(data); - while (last->ind == data->a->ind && (tmp = add_op(data, "rra")) != NULL) - { - ps_rerot(data, 'a'); - last = get_last_a(data); - } - } -// } + if (join_lists(data, &last, inds, inds2) == 0) + return (0); + if (rotate_list(data, &last) == 0) + return (0); inds++; } return (1); @@ -242,7 +258,7 @@ void clean_op(t_psdata *data) { ct1 = (char*)(it->content); ct2 = (char*)(it->next->content); - if (ct1 && ct2 && ct1[0] == 'p' && ct2[0] == 'p' && ct1[1] != ct2[1]) + if (ct1[0] == 'p' && ct2[0] == 'p' && ct1[1] != ct2[1]) { *prec = it->next->next; ft_memdel(&(it->next->content)); @@ -255,7 +271,8 @@ void clean_op(t_psdata *data) else if (it->next->next) { ct3 = (char*)(it->next->next->content); - if (ct1[0] == 'p' && ct1[1] == 'b' && ct3[0] == 'p' && ct3[1] == 'a' && ft_strcmp(ct2, "rra") == 0) + if (ct1[0] == 'p' && ct1[1] == 'b' && ct3[0] == 'p' + && ct3[1] == 'a' && ft_strcmp(ct2, "rra") == 0) { *prec = it->next; ft_strcpy((char*)(it->next->next->content), "sa"); @@ -278,46 +295,40 @@ void clean_op(t_psdata *data) } } +void print_ops(t_psdata *data) +{ + t_list *it; + + it = data->op; + while (it) + { + ft_printf("%s\n", (char*)(it->content)); + it = it->next; + } +} + int main(int ac, char **av) { - t_list *it; - t_stack *ptr; t_psdata data; ps_initdata(&data); - if (!get_params(&data, ac, av)) + if (!get_params(&data, ac, av, 0)) return (0); - ptr = data.a; - if (ptr == NULL) - ft_putendl("Empty"); - while (ptr) - { - ptr->ind = 0; - ptr = ptr->next; - } + if (data.a == NULL) + ft_putendl_fd("Error", 2); if (data.size_a == 3) - { sortfor3(&data); - ps_stkclean(&data); - return (0); - } else if (data.size_a == 5) - { sortfor5(&data); - ps_stkclean(&data); - return (0); - } - mark_groups(&data); - if (sort(&data) == 0) - ft_putendl_fd("Error\n", 2); - else + if (data.size_a != 0 && data.size_a != 3 && data.size_a != 5) { - clean_op(&data); - it = data.op; - while (it) + mark_groups(&data); + if (sort(&data) == 0) + ft_putendl_fd("Error\n", 2); + else { - ft_printf("%s\n", (char*)(it->content)); - it = it->next; + clean_op(&data); + print_ops(&data); } } ps_stkclean(&data);