pile b ne fonctionne pas

This commit is contained in:
hugogogo
2021-06-15 02:54:06 +02:00
parent 825b82905b
commit aeb5b57725
21 changed files with 110 additions and 13 deletions

View File

@@ -22,8 +22,13 @@ LIBS = $(_LIBS:lib%.a=%)
SRCS = push_swap.c \ SRCS = push_swap.c \
algo.c \ algo.c \
print.c \ print.c \
stop.c \
swap.c \ swap.c \
stop.c push.c \
rotate.c \
reverse_rotate.c \
algo_bubble_sort.c
ODIR = ./builds ODIR = ./builds
OBJS = $(SRCS:%.c=$(ODIR)/%.o) OBJS = $(SRCS:%.c=$(ODIR)/%.o)

View File

@@ -82,6 +82,12 @@ source des comparaisons ci-dessous : [https://www.youtube.com/watch?v=xoR-1KwQh2
- `t_list *rrb(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);` - `t_list *rrr(t_stack **a, t_stack **b, t_list **lst);`
## points particuliers :
- les fonctions d'actions (puches, swapes, rotates) recoivent une ou les deux piles a et b, et la liste chainee *solution* qui doit accueillir les noms des actions successives
- mais pour pouvoir imprimer l'état des deux piles a et b a chaque etape il faudrait que chaques fonctions recoivent les deux piles, ce qui n'est pas le cas : sa() par exemple ne recoit pas la pile b
- donc j'ai stocké les deux piles dans les deux premiers maillons de la chaine solution, car toutes les fonctions d'actions la recoivent
## ce programme push_swap.c s'organise de la facon suivante : ## 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) : 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) :

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
builds/push.o Normal file

Binary file not shown.

Binary file not shown.

BIN
builds/reverse_rotate.o Normal file

Binary file not shown.

BIN
builds/rotate.o Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -40,10 +40,9 @@ 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_list **lst, char *c);
void print_result(t_list *lst, int i); void print_result(t_list *lst, int i);
/* /*
** swap ** swap
*/ */

BIN
push_swap

Binary file not shown.

View File

@@ -5,7 +5,7 @@ void hugo_sort(t_stack **a, t_stack **b, t_list **solution)
{ {
(void)b; (void)b;
sa(a, solution); sa(a, solution);
sa(a, solution); pb(b, a, solution);
sa(a, solution); sb(a, solution);
} }

View File

@@ -12,6 +12,7 @@
#include "push_swap.h" #include "push_swap.h"
/*
static int search_smaller(t_stack *stack, int *smaller) static int search_smaller(t_stack *stack, int *smaller)
{ {
int i; int i;
@@ -56,3 +57,4 @@ int bubble_sort(t_stack **a, t_stack **b, t_list *solution)
pa(a, b, &solution); pa(a, b, &solution);
return (1); return (1);
} }
*/

View File

@@ -1,10 +1,28 @@
#include "push_swap.h" #include "push_swap.h"
void fill_solution(t_stack *a, t_stack *b, t_list **solution, char *sp) /*
** this function is called at each action (pushes, swapes, rotates)
** pointers to the stacks a and b are stored into two first element of list solution (because both lists a and b are not available from every functions, like sa() doesn't receive b in argument)
** the function access the list to fill a new element with three things :
** - the name of the action (sa, sp, rra, etc)
** - the stack a
** - the stack b
** like this :
** name!a: nb nb nb nb nb!b: nb nb nb
** "!" are used to split, it will be turn into :
** name
** a: nb nb nb nb nb
** b: nb nb nb
*/
void fill_solution(t_list **solution, char *sp)
{ {
t_stack *a;
t_stack *b;
char *stack; char *stack;
a = (*solution)->content;
b = (*solution)->next->content;
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)
@@ -17,7 +35,7 @@ void fill_solution(t_stack *a, t_stack *b, t_list **solution, char *sp)
while(b) while(b)
{ {
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(b->n));
b = b->next; b = b->next;
} }
ft_lstadd_back(solution, ft_lstnew(stack)); ft_lstadd_back(solution, ft_lstnew(stack));
@@ -26,7 +44,8 @@ void fill_solution(t_stack *a, t_stack *b, t_list **solution, char *sp)
void print_result(t_list *result, int flag) void print_result(t_list *result, int flag)
{ {
char **part; char **part;
result = result->next->next;
if (!flag) if (!flag)
result = result->next; result = result->next;
while (result) while (result)

37
srcs/push.c Normal file
View File

@@ -0,0 +1,37 @@
#include "push_swap.h"
t_list *pa(t_stack **a, t_stack **b, t_list **solution)
{
t_stack *tmp;
if (!(tmp = ft_calloc(1, sizeof(t_stack))))
ps_stop(*a, *b, *solution, 2);
tmp->n = (*b)->n;
tmp->next = NULL;
(*a)->next = tmp;
fill_solution(solution, ft_strdup("pa"));
return (NULL);
}
t_list *pb(t_stack **dst, t_stack **src, t_list **solution)
{
t_stack *a;
t_stack *b;
t_stack *tmp;
a = *src;
b = *dst;
if (!(b = ft_calloc(1, sizeof(t_stack))))
ps_stop(a, b, *solution, 2);
b->n = a->n;
b->next = NULL;
tmp = a->next;
a->n = a->next->n;
a->next = a->next->next;
free(tmp);
fill_solution(solution, ft_strdup("pa"));
return (NULL);
}

View File

@@ -49,7 +49,10 @@ t_stack *init_stack(int ac, char **av)
/* /*
** this function creates the stack b and the list solution ** 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 init the list solution with :
** - first element : pointer to stack a
** - secnd element : pointer to stack b
** - third element : initial values of stack a (in case of flag -p)
** then it calls the sorting algorithm ** then it calls the sorting algorithm
** then it calls ps_stop() to free everything that needs to be freed ** then it calls ps_stop() to free everything that needs to be freed
*/ */
@@ -58,11 +61,13 @@ t_list *launch_algo(t_stack *a)
t_stack *b; t_stack *b;
t_list *solution; t_list *solution;
if(!(b = ft_calloc(1, sizeof(t_stack)))) b = NULL;
ps_stop(a, b, NULL, 2);
solution = NULL; solution = NULL;
fill_solution(a, NULL, &solution, ft_strdup("start")); ft_lstadd_back(&solution, ft_lstnew(a));
ft_lstadd_back(&solution, ft_lstnew(b));
fill_solution(&solution, ft_strdup("start"));
hugo_sort(&a, &b, &solution); hugo_sort(&a, &b, &solution);
//bubble_sort(&a, &b, solution);
ps_stop(NULL, b, NULL, -1); ps_stop(NULL, b, NULL, -1);
return (solution); return (solution);
} }

6
srcs/reverse_rotate.c Normal file
View File

@@ -0,0 +1,6 @@
#include "push_swap.h"
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);

7
srcs/rotate.c Normal file
View File

@@ -0,0 +1,7 @@
#include "push_swap.h"
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);

View File

@@ -12,7 +12,18 @@ t_list *sa(t_stack **stack, t_list **solution)
tmp = a->n; tmp = a->n;
a->n = a->next->n; a->n = a->next->n;
a->next->n = tmp; a->next->n = tmp;
fill_solution(*stack, NULL, solution, ft_strdup("sa")); fill_solution(solution, ft_strdup("sa"));
return (NULL); return (NULL);
} }
t_list *sb(t_stack **b, t_list **solution)
{
return (sa(b, solution));
}
t_list *ss(t_stack **a, t_stack **b, t_list **solution)
{
sa(a, solution);
sa(b, solution);
return (NULL);
}