functions standardisees, annotees et readme en cours decriture

This commit is contained in:
hugogogo
2021-06-14 21:40:05 +02:00
parent 7a0f44c676
commit 5050e05816
17 changed files with 222 additions and 128 deletions
+4 -2
View File
@@ -3,7 +3,7 @@
# variables names # value | .h in includes/ # variables names # value | .h in includes/
# - - - - - - - - - - - - - - # ! name is case sensitive | ! use VPATH only for .c # - - - - - - - - - - - - - - # ! name is case sensitive | ! use VPATH only for .c
NAME = pushswap NAME = push_swap
CC = gcc CC = gcc
@@ -19,10 +19,12 @@ LDIR = ./libft
_LIBS = libft.a _LIBS = libft.a
LIBS = $(_LIBS:lib%.a=%) LIBS = $(_LIBS:lib%.a=%)
SRCS = pushswap.c \ SRCS = push_swap.c \
algo.c \ algo.c \
print.c \ print.c \
swap.c \ swap.c \
solve_sorting.c \
algo_bubble_sort.c \
stop.c stop.c
ODIR = ./builds ODIR = ./builds
+40 -30
View File
@@ -62,35 +62,45 @@
--- ---
# markdown rules : pour utiliser l'algorithme de tris de ce programme dans votre propre programme, il faut :
1. copier le fichier algo.c
# big title 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
## medium title 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);`
or medium title - `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);`
### small title - `t_list *ra(t_stack **stack, t_list **lst);`
- `t_list *rb(t_stack **stack, t_list **lst);`
#### tiny title - `t_list *rr(t_stack **a, t_stack **b, t_list **lst);`
- `t_list *rra(t_stack **stack, t_list **lst);`
##### mini title - `t_list *rrb(t_stack **stack, t_list **lst);`
- `t_list *rrr(t_stack **a, t_stack **b, t_list **lst);`
###### all caps title
**bold** *italic* [link](https://theuselessweb.com/)
1. ordoned list ce programme push_swap.c s'organise de la facon suivante :
2. ordoned list
3. ordoned list 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 :
- list - is_valid : verification d'erreur de parsing des listes données
- list - check_flag : regarde la presence des flags, pour l'instant uniquement -p pour print les etapes du tri
- list - 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)
line : - 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)
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
Binary file not shown.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+9 -3
View File
@@ -12,11 +12,18 @@ typedef struct s_stack
struct s_stack *next; struct s_stack *next;
} t_stack; } t_stack;
/*
** luke's algo
*/
int bubble_sort(t_stack **a, t_stack **b, t_list *solution);
/* /*
** pushswap.c ** pushswap.c
*/ */
int check_flag(int *ac, char ***av);
void is_valid(int ac, char **av); void is_valid(int ac, char **av);
t_stack *init_stack(int ac, char **av); t_stack *init_stack(int ac, char **av);
t_list *launch_algo(t_stack *stack);
/* /*
** stop.c ** stop.c
@@ -28,14 +35,13 @@ void ps_stop(t_stack *a, t_stack *b, t_list *lst, int i);
/* /*
** algo.c ** algo.c
*/ */
t_list *sort_algo(t_stack *stack); void hugo_sort(t_stack **a, t_stack **b, t_list **lst);
/* /*
** print.c ** print.c
*/ */
void fill_solution(t_stack *a, t_stack *b, t_list **lst, char *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, int i);
void print_result(t_list *lst);
/* /*
BIN
View File
Binary file not shown.
+3 -20
View File
@@ -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); 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);
}
+58
View File
@@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* algo_bubble_sort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}
+14 -17
View File
@@ -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) void fill_solution(t_stack *a, t_stack *b, t_list **solution, char *sp)
{ {
char *stack; char *stack;
(void)b;
stack = ft_strdup(sp); stack = ft_strdup(sp);
stack = ft_strjoinfree(stack, ft_strdup(" a")); stack = ft_strjoinfree(stack, ft_strdup("!a:"));
while(a) while(a)
{ {
stack = ft_strjoinfree(stack, ft_strdup(" ")); stack = ft_strjoinfree(stack, ft_strdup(" "));
stack = ft_strjoinfree(stack, ft_itoa(a->n)); stack = ft_strjoinfree(stack, ft_itoa(a->n));
a = a->next; a = a->next;
} }
stack = ft_strjoinfree(stack, ft_strdup("!b:"));
while(b) while(b)
{ {
stack = ft_strjoinfree(stack, ft_strdup(" ")); 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)); ft_lstadd_back(solution, ft_lstnew(stack));
} }
void print_stack(t_stack *stack, char c) void print_result(t_list *result, int flag)
{ {
ft_printf(" %c | ", c); char **part;
while (stack)
{
ft_printf("%i", stack->n);
stack = stack->next;
if (stack)
ft_putstr(" ");
}
ft_putchar('\n');
}
void print_result(t_list *result) if (!flag)
{ result = result->next;
while (result) 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; result = result->next;
} }
} }
+91
View File
@@ -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);
}
-53
View File
@@ -1,53 +0,0 @@
#include "pushswap.h"
#include <fcntl.h>
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);
}
+1 -1
View File
@@ -1,5 +1,5 @@
#include "pushswap.h" #include "push_swap.h"
void ps_usage(void) void ps_usage(void)
{ {
+1 -1
View File
@@ -1,5 +1,5 @@
#include "pushswap.h" #include "push_swap.h"
t_list *sa(t_stack **stack, t_list **solution) t_list *sa(t_stack **stack, t_list **solution)
{ {