diff --git a/Makefile b/Makefile index 686b455..8727333 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ # variables names # value | .h in includes/ # - - - - - - - - - - - - - - # ! name is case sensitive | ! use VPATH only for .c -NAME = pushswap +NAME = push_swap CC = gcc @@ -19,10 +19,12 @@ LDIR = ./libft _LIBS = libft.a LIBS = $(_LIBS:lib%.a=%) -SRCS = pushswap.c \ +SRCS = push_swap.c \ algo.c \ print.c \ swap.c \ + solve_sorting.c \ + algo_bubble_sort.c \ stop.c ODIR = ./builds diff --git a/README.md b/README.md index a6dfb3e..e42adc2 100644 --- a/README.md +++ b/README.md @@ -62,35 +62,45 @@ --- -# markdown rules : - -# big title - -## medium title - -or medium title -- - -### small title - -#### tiny title - -##### mini title - -###### all caps title - -**bold** *italic* [link](https://theuselessweb.com/) - -1. ordoned list -2. ordoned list -3. ordoned list - -- list -- list -- list - -line : - ---- +pour utiliser l'algorithme de tris de ce programme dans votre propre programme, il faut : +1. copier le fichier algo.c +2. mettre son prototype dans votre header.h : `void hugo_sort(t_stack **a, t_stack **b, t_list **solution)` +3. mettre le fichiers dans vos srcs dans Makefile +4. et verifier que vos functions d'actions (push, swap, rotate) ont bien les meme prototypes : + - `t_list *sa(t_stack **stack, t_list **lst);` + - `t_list *ss(t_stack **a, t_stack **b, t_list **lst);` + - `t_list *pa(t_stack **dst, t_stack **src, t_list **lst);` + - `t_list *pb(t_stack **dst, t_stack **src, t_list **lst);` + - `t_list *ra(t_stack **stack, t_list **lst);` + - `t_list *rb(t_stack **stack, t_list **lst);` + - `t_list *rr(t_stack **a, t_stack **b, t_list **lst);` + - `t_list *rra(t_stack **stack, t_list **lst);` + - `t_list *rrb(t_stack **stack, t_list **lst);` + - `t_list *rrr(t_stack **a, t_stack **b, t_list **lst);` + + + + +ce programme push_swap.c s'organise de la facon suivante : + +1. des fonctions pour faire fonctionner l'algorithme de tris (principalement parsing des donnees, et les actions utilisees par l'algorithme de tris : push, swap, rotate et reverse rotate) : +- push_swap.c : contient la fonction main et 4 autres fonctions : + - is_valid : verification d'erreur de parsing des listes données + - check_flag : regarde la presence des flags, pour l'instant uniquement -p pour print les etapes du tri + - init_stack : qui parse la pile a trier dans une liste chainee + - launch_algo : la fonction qui lance l'algorithme de tri, en lui envoyant ce qui est necessaire (les deux piles a et b et la liste de solutions) +- stop.c : qui gere les erreurs, l'usage, mais aussi qui free avant de fermer le programme + - ps_stop : recoit toutes les listes chainees du programme, envoyer NULL si on ne veut pas free la liste, et le dernier int permet de savoir si on veut arreter le programme (>= 0) et si on veut signaler une erreur (> 1) + - ps_error : la gestion des erreurs + - ps_usage : en cas d'erreur = 1, affiche l'usage du programme +- print.c : contient les fonctions necessaires pour afficher le resultat + - print_result : affiche le resultat, et si presence du flag -p affiche aussi les etapes de tris + - fill_solution : remplit la liste chainee solution au fur et a mesure de l'appel des fonctions push swap et rotate (pas optimisé pour raison de compatibilité avec l'appel des fonctions du programme de luke : les actions (push, swap, etc) ne recoivent pas de flag en argument, donc fill_solution remplis toutes les infos necessaires pour le flag, meme s'il n'y a pas de flag) +- toutes les fonctions d'action (push, swap, rotate, et reverse rotate) + +2. les fonctions d'algorithme, qui sont appellees par launch_solution() +- hugo_sort : pour l'instant une fonction test qui utilise toutes les actions (push, swap, rotate) + + diff --git a/builds/algo.o b/builds/algo.o index b1b7158..bf97471 100644 Binary files a/builds/algo.o and b/builds/algo.o differ diff --git a/builds/print.o b/builds/print.o index 7ce61fc..2459d08 100644 Binary files a/builds/print.o and b/builds/print.o differ diff --git a/builds/push_swap.o b/builds/push_swap.o new file mode 100644 index 0000000..c51e740 Binary files /dev/null and b/builds/push_swap.o differ diff --git a/builds/pushswap.o b/builds/pushswap.o index b3c2860..5b5a637 100644 Binary files a/builds/pushswap.o and b/builds/pushswap.o differ diff --git a/builds/stop.o b/builds/stop.o deleted file mode 100644 index 45d914f..0000000 Binary files a/builds/stop.o and /dev/null differ diff --git a/builds/swap.o b/builds/swap.o index 970d524..e64c942 100644 Binary files a/builds/swap.o and b/builds/swap.o differ diff --git a/includes/pushswap.h b/includes/push_swap.h similarity index 82% rename from includes/pushswap.h rename to includes/push_swap.h index 8f25f1b..642b288 100644 --- a/includes/pushswap.h +++ b/includes/push_swap.h @@ -12,11 +12,18 @@ typedef struct s_stack struct s_stack *next; } t_stack; +/* +** luke's algo +*/ +int bubble_sort(t_stack **a, t_stack **b, t_list *solution); + /* ** pushswap.c */ +int check_flag(int *ac, char ***av); void is_valid(int ac, char **av); t_stack *init_stack(int ac, char **av); +t_list *launch_algo(t_stack *stack); /* ** stop.c @@ -28,14 +35,13 @@ void ps_stop(t_stack *a, t_stack *b, t_list *lst, int i); /* ** algo.c */ -t_list *sort_algo(t_stack *stack); +void hugo_sort(t_stack **a, t_stack **b, t_list **lst); /* ** print.c */ void fill_solution(t_stack *a, t_stack *b, t_list **lst, char *c); -void print_stack(t_stack *stack, char c); -void print_result(t_list *lst); +void print_result(t_list *lst, int i); /* diff --git a/pushswap b/pushswap deleted file mode 100755 index 055eb92..0000000 Binary files a/pushswap and /dev/null differ diff --git a/srcs/algo.c b/srcs/algo.c index 387e70d..b998c92 100644 --- a/srcs/algo.c +++ b/srcs/algo.c @@ -1,28 +1,11 @@ -#include "pushswap.h" +#include "push_swap.h" -void hugo_sort(t_stack **a, t_list **solution) +void hugo_sort(t_stack **a, t_stack **b, t_list **solution) { + (void)b; sa(a, solution); sa(a, solution); sa(a, solution); } -/* -** this function creates the stack b and the list solution -** then it calls the sorting algorithm -** then it frees everything that needs to be freed -*/ - -t_list *sort_algo(t_stack *a) -{ - t_stack *b; - t_list *solution; - - solution = NULL; - if(!(b = ft_calloc(1, sizeof(t_stack)))) - ps_stop(a, b, NULL, 2); - hugo_sort(&a, &solution); - ps_stop(NULL, b, NULL, -1); - return (solution); -} diff --git a/srcs/algo_bubble_sort.c b/srcs/algo_bubble_sort.c new file mode 100644 index 0000000..01812fc --- /dev/null +++ b/srcs/algo_bubble_sort.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* algo_bubble_sort.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lperrey +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/06/10 21:52:43 by lperrey #+# #+# */ +/* Updated: 2021/06/10 22:49:04 by lperrey ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +static int search_smaller(t_stack *stack, int *smaller) +{ + int i; + int i_small; + + i_small = 0; + i = 0; + *smaller = (stack)->n; + while (stack->next) + { + stack = stack->next; + i++; + if (stack->n < *smaller) + { + *smaller = stack->n; + i_small = i; + } + } + return (i_small); +} + +int bubble_sort(t_stack **a, t_stack **b, t_list *solution) +{ + int smaller; + + while (*a) + { + // ft_lstsize() pour verification du sens de rotation + if (search_smaller(*a, &smaller) <= ft_lstsize((t_list*)*a) / 2) + { + while ((*a)->n != smaller) + ra(a, &solution); + } + else + { + while ((*a)->n != smaller) + rra(a, &solution); + } + pb(b, a, &solution); + } + while (*b) + pa(a, b, &solution); + return (1); +} diff --git a/srcs/print.c b/srcs/print.c index ea3f92a..2464411 100644 --- a/srcs/print.c +++ b/srcs/print.c @@ -1,19 +1,19 @@ -#include "pushswap.h" +#include "push_swap.h" void fill_solution(t_stack *a, t_stack *b, t_list **solution, char *sp) { char *stack; - (void)b; stack = ft_strdup(sp); - stack = ft_strjoinfree(stack, ft_strdup(" a")); + stack = ft_strjoinfree(stack, ft_strdup("!a:")); while(a) { stack = ft_strjoinfree(stack, ft_strdup(" ")); stack = ft_strjoinfree(stack, ft_itoa(a->n)); a = a->next; } + stack = ft_strjoinfree(stack, ft_strdup("!b:")); while(b) { stack = ft_strjoinfree(stack, ft_strdup(" ")); @@ -23,24 +23,21 @@ void fill_solution(t_stack *a, t_stack *b, t_list **solution, char *sp) ft_lstadd_back(solution, ft_lstnew(stack)); } -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_putchar('\n'); -} - -void print_result(t_list *result) +void print_result(t_list *result, int flag) { + char **part; + + if (!flag) + result = result->next; while (result) { - ft_printf("%s\n", result->content); + part = ft_split(result->content, '!'); + ft_printf("%s\n", part[0]); + if (flag) + { + ft_printf(" %s\n", part[1]); + ft_printf(" %s\n", part[2]); + } result = result->next; } } diff --git a/srcs/push_swap.c b/srcs/push_swap.c new file mode 100644 index 0000000..f46eae9 --- /dev/null +++ b/srcs/push_swap.c @@ -0,0 +1,91 @@ + +#include "push_swap.h" + +/* +** this function check errors in the arguments +** like, is there arguments, and are they a valid list without equals values +** if there are no arguments it calls the stop function with value 1 to print usage +*/ +void is_valid(int ac, char **av) +{ + if (ac < 2) + ps_stop(NULL, NULL, NULL, 1); + (void)av; + // check more error +} + +/* +** this function check if there are flags (currently only one flag) : +** -p to print the evolution of the list while the sorting is done +*/ +int check_flag(int *ac, char ***av) +{ + if (ft_strcmp((*av)[1], "-p") != 0) + return (0); + (*av)++;; + (*ac)--; + return (1); +} + +/* +** create the stack for list a, as a linked list, and fill it with the values given in arguments argv +*/ +t_stack *init_stack(int ac, char **av) +{ + t_stack *start; + t_stack *tmp; + + tmp = NULL; + while (--ac) + { + if (!(start = ft_calloc(1, sizeof(t_stack)))) + ps_stop(start, NULL, NULL, 2); + start->n = ft_atoi(av[ac]); + start->next = tmp; + tmp = start; + } + return (start); +} + +/* +** this function creates the stack b and the list solution +** then it init the list solution with initial values of stack a (in case of flag -p) +** then it calls the sorting algorithm +** then it calls ps_stop() to free everything that needs to be freed +*/ +t_list *launch_algo(t_stack *a) +{ + t_stack *b; + t_list *solution; + + if(!(b = ft_calloc(1, sizeof(t_stack)))) + ps_stop(a, b, NULL, 2); + solution = NULL; + fill_solution(a, NULL, &solution, ft_strdup("start")); + hugo_sort(&a, &b, &solution); + ps_stop(NULL, b, NULL, -1); + return (solution); +} + +/* +** this programme will sort a list +** this function first check the validity of the arguments +** then it checks for flags (currently only -p) +** then it parse the list into a linked list +** then it calls the sorting algorithm (it actually calls a launch function that will execute some actions and calls the sorting algorithm) +** and finally it frees everything and leaves +*/ +int main(int ac, char **av) +{ + t_stack *stack; + t_list *result; + int flag; + + is_valid(ac, av); + flag = check_flag(&ac, &av); + stack = init_stack(ac, av); + result = launch_algo(stack); + print_result(result, flag); + ps_stop(stack, NULL, result, 0); + return(0); +} diff --git a/srcs/pushswap.c b/srcs/pushswap.c deleted file mode 100644 index 67d95fd..0000000 --- a/srcs/pushswap.c +++ /dev/null @@ -1,53 +0,0 @@ - -#include "pushswap.h" -#include - -void is_valid(int ac, char **av) -{ - if (ac < 2) - ps_stop(NULL, NULL, NULL, 1); - (void)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; - t_stack *tmp; - - tmp = NULL; - while (--ac) - { - if (!(start = ft_calloc(1, sizeof(t_stack)))) - ps_stop(start, NULL, NULL, 2); - start->n = ft_atoi(av[ac]); - start->next = tmp; - tmp = start; - } - return (start); -} - -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); - (void)flag; - print_result(result); - ps_stop(stack, NULL, result, 0); - return(0); -} diff --git a/srcs/stop.c b/srcs/stop.c index 570b95a..88e682c 100644 --- a/srcs/stop.c +++ b/srcs/stop.c @@ -1,5 +1,5 @@ -#include "pushswap.h" +#include "push_swap.h" void ps_usage(void) { diff --git a/srcs/swap.c b/srcs/swap.c index 0f88029..e03320f 100644 --- a/srcs/swap.c +++ b/srcs/swap.c @@ -1,5 +1,5 @@ -#include "pushswap.h" +#include "push_swap.h" t_list *sa(t_stack **stack, t_list **solution) {