mini transformation dans les actions qui a tout casse

This commit is contained in:
hugogogo
2021-06-12 14:26:15 +02:00
parent ca0bf0cb61
commit 889c3f4a99
11 changed files with 86 additions and 48 deletions

View File

@@ -22,6 +22,7 @@ LIBS = $(_LIBS:lib%.a=%)
SRCS = pushswap.c \
algo.c \
print.c \
swap.c \
stop.c
ODIR = ./builds

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
builds/swap.o Normal file

Binary file not shown.

View File

@@ -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();
*/
/*

BIN
pushswap

Binary file not shown.

View File

@@ -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
}

View File

@@ -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');
}

View File

@@ -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);
}

19
srcs/swap.c Normal file
View File

@@ -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")));
}