diff --git a/Makefile b/Makefile index ca342cd..686b455 100644 --- a/Makefile +++ b/Makefile @@ -22,6 +22,7 @@ LIBS = $(_LIBS:lib%.a=%) SRCS = pushswap.c \ algo.c \ print.c \ + swap.c \ stop.c ODIR = ./builds diff --git a/builds/algo.o b/builds/algo.o index 106a6d4..69b77a3 100644 Binary files a/builds/algo.o and b/builds/algo.o differ diff --git a/builds/print.o b/builds/print.o index 5e01507..6b69c9a 100644 Binary files a/builds/print.o and b/builds/print.o differ diff --git a/builds/pushswap.o b/builds/pushswap.o index e4811c0..4817c02 100644 Binary files a/builds/pushswap.o and b/builds/pushswap.o differ diff --git a/builds/swap.o b/builds/swap.o new file mode 100644 index 0000000..d346899 Binary files /dev/null and b/builds/swap.o differ diff --git a/includes/pushswap.h b/includes/pushswap.h index ab00aef..6e35209 100644 --- a/includes/pushswap.h +++ b/includes/pushswap.h @@ -28,23 +28,21 @@ void ps_stop(t_stack *stb, t_stack *sta, t_list *lst, int i); /* ** algo.c */ -t_list *sort_algo(t_stack *stack); +t_list *sort_algo(t_stack *stack, int i); /* ** print.c */ -void print_stack(t_stack *stack); +void print_result(t_list *result); +void print_stack(t_stack *stack, char c); /* ** swap */ -/* -int swap(t_list **list); -void sa(); -void sb(); -void ss(); -*/ +void sa(t_list *pack); +void sb(t_list *pack); +void ss(t_list *pack); /* ** push @@ -53,7 +51,6 @@ void ss(); int push(t_list **dst, t_list **src); int pa(); int pb(); -int pp(); */ /* diff --git a/pushswap b/pushswap index 97457b7..86a1ee2 100755 Binary files a/pushswap and b/pushswap differ diff --git a/srcs/algo.c b/srcs/algo.c index 77a69c7..ba2468b 100644 --- a/srcs/algo.c +++ b/srcs/algo.c @@ -1,25 +1,9 @@ #include "pushswap.h" -void sa(t_list *pack) -{ - t_stack *stack_a; - t_list **solution; - int tmp; - - stack_a = pack->content; - solution = pack->next->next->content; - if (!stack_a->next) - return ; - tmp = stack_a->n; - stack_a->n = stack_a->next->n; - stack_a->next->n = tmp; - (*solution)->content = ft_strdup("sa"); - ft_lstadd_back(solution, ft_lstnew(NULL)); -} - /* -** this function +** this function is only used to print the stack and the action +** then it launch the action (swap, push, rotate) */ void action(t_list *pack, void (*sp)(t_list *pack)) @@ -27,16 +11,38 @@ void action(t_list *pack, void (*sp)(t_list *pack)) t_stack *stack_a; t_stack *stack_b; t_list **solution; + int flag; stack_a = pack->content; stack_b = pack->next->content; solution = pack->next->next->content; - print_stack(stack_a); - print_stack(stack_b); + flag = *(int *)(pack->next->next->next->content); // this is a really ugly line :p it's because lst->content is a void*, but we need an int + if (!flag) // if flag == 0, don't print anything + return ; + if (!*solution) // to print only at first call + { + print_stack(stack_a, 'a'); + print_stack(stack_b, 'b'); + } + sp(pack); // the action to execute ft_printf("%s\n", (*solution)->content); - sp(pack); + print_stack(stack_a, 'a'); + print_stack(stack_b, 'b'); } +t_list *do_pack(t_stack *stack_a, int flag) +{ + t_stack *stack_b; + t_list *pack; + + if(!(stack_b = ft_calloc(1, sizeof(t_stack)))) + ps_stop(stack_a, stack_b, NULL, 2); + pack = ft_lstnew(stack_a); + ft_lstadd_back(&pack, ft_lstnew(stack_b)); + ft_lstadd_back(&pack, ft_lstnew(NULL)); + ft_lstadd_back(&pack, ft_lstnew(&flag)); + return (pack); +} /* ** this function create the structures "solution" and "stack_b" @@ -48,21 +54,13 @@ void action(t_list *pack, void (*sp)(t_list *pack)) ** then it calls the algorithm */ -t_list *sort_algo(t_stack *stack_a) +t_list *sort_algo(t_stack *stack, int flag) { - t_stack *stack_b; - t_list *solution; t_list *pack; - solution = ft_lstnew(NULL); - if(!(stack_b = ft_calloc(1, sizeof(t_stack)))) - ps_stop(stack_a, stack_b, solution, 2); - pack = ft_lstnew(stack_a); - ft_lstadd_back(&pack, ft_lstnew(stack_b)); - ft_lstadd_back(&pack, ft_lstnew(&solution)); + pack = do_pack(stack, flag); + action(pack, sa); action(pack, sa); - print_stack(stack_a); - print_stack(stack_b); // sa(stack_a, stack_b, &solution); // sb(stack_a, stack_b, &solution); // ss(stack_a, stack_b, &solution); @@ -74,5 +72,8 @@ t_list *sort_algo(t_stack *stack_a) // rra(stack_a, stack_b, &solution); // rrb(stack_a, stack_b, &solution); // rrr(stack_a, stack_b, &solution); - return (solution); + if (flag) + ft_putchar('\n'); + ps_stop(NULL, pack->next->content, NULL, -1); // pack->next->content is stack_b + return (pack->next->next->content); // pack->next->next->content is t_list *solution } diff --git a/srcs/print.c b/srcs/print.c index ee38810..5fa676c 100644 --- a/srcs/print.c +++ b/srcs/print.c @@ -1,16 +1,25 @@ #include "pushswap.h" -void print_stack(t_stack *stack) +void print_result(t_list *result) { - ft_putstr("["); + while (result) + { + ft_printf("%s\n", result->content); + result = result->next; + } +} + +void print_stack(t_stack *stack, char c) +{ + ft_printf(" %c | ", c); while (stack) { ft_printf("%i", stack->n); stack = stack->next; if (stack) - ft_putstr("; "); + ft_putstr(" "); } - ft_putstr("]\n"); + ft_putchar('\n'); } diff --git a/srcs/pushswap.c b/srcs/pushswap.c index 2c37871..8eed16f 100644 --- a/srcs/pushswap.c +++ b/srcs/pushswap.c @@ -9,6 +9,15 @@ void is_valid(int ac, char **av) // check more error } +int check_flag(int *ac, char ***av) +{ + if (ft_strcmp((*av)[1], "-p") != 0) + return (0); + (*av)++;; + (*ac)--; + return (1); +} + t_stack *init_stack(int ac, char **av) { t_stack *start; @@ -30,11 +39,13 @@ int main(int ac, char **av) { t_stack *stack; t_list *result; + int flag; is_valid(ac, av); // check if usage and list are correct + flag = check_flag(&ac, &av); //check for flag, like print stack = init_stack(ac, av); // create the list from av[] - result = sort_algo(stack); - ft_printf("\n%s\n", result->content); + result = sort_algo(stack, flag); + print_result(result); ps_stop(stack, NULL, result, 0); return(0); } diff --git a/srcs/swap.c b/srcs/swap.c new file mode 100644 index 0000000..dded21e --- /dev/null +++ b/srcs/swap.c @@ -0,0 +1,19 @@ + +#include "pushswap.h" + +void sa(t_list *pack) +{ + t_stack *stack_a; +// t_list **solution; + int tmp; + + stack_a = pack->content; +// solution = pack->next->next->content; + if (!stack_a->next) + return ; + tmp = stack_a->n; + stack_a->n = stack_a->next->n; + stack_a->next->n = tmp; +// ft_lstadd_back(solution, ft_lstnew(ft_strdup("sa"))); +} +