ca remarche et c compatible avec luke

This commit is contained in:
hugogogo
2021-06-14 11:22:45 +02:00
parent 7acfa0491e
commit ef7ffdb4dc
10 changed files with 53 additions and 83 deletions

View File

@@ -1,34 +1,20 @@
#include "pushswap.h"
/*
** this function is only used to print the stack and the action
** then it launch the action (swap, push, rotate)
*/
void action(t_stack *stack_a, t_stack *stack_b, t_list *solution, int flag,
void (*sp)(t_list *pack))
void hugo_sort(t_stack **a, t_list **solution)
{
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_putchar('t');
ft_printf("%s\n", (*solution)->content);
ft_putchar('u');
print_stack(stack_a, 'a');
print_stack(stack_b, '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, int f)
t_list *sort_algo(t_stack *a)
{
t_stack *b;
t_list *solution;
@@ -36,21 +22,7 @@ t_list *sort_algo(t_stack *a, int f)
solution = NULL;
if(!(b = ft_calloc(1, sizeof(t_stack))))
ps_stop(a, b, NULL, 2);
action(a, b, solution, f, sa);
action(a, b, solution, f, sa);
// sa(stack_a, stack_b, &solution);
// sb(stack_a, stack_b, &solution);
// ss(stack_a, stack_b, &solution);
// pa(stack_a, stack_b, &solution);
// pb(stack_a, stack_b, &solution);
// ra(stack_a, stack_b, &solution);
// rb(stack_a, stack_b, &solution);
// rr(stack_a, stack_b, &solution);
// rra(stack_a, stack_b, &solution);
// rrb(stack_a, stack_b, &solution);
// rrr(stack_a, stack_b, &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
hugo_sort(&a, &solution);
ps_stop(NULL, b, NULL, -1);
return (solution);
}

View File

@@ -1,13 +1,11 @@
#include "pushswap.h"
void print_result(t_list *result)
void fill_solution(t_stack **a, t_stack **b, t_list **solution, char *sp)
{
while (result)
{
ft_printf("%s\n", result->content);
result = result->next;
}
(void)a;
(void)b;
ft_lstadd_back(solution, ft_lstnew(sp));
}
void print_stack(t_stack *stack, char c)
@@ -23,3 +21,11 @@ void print_stack(t_stack *stack, char c)
ft_putchar('\n');
}
void print_result(t_list *result)
{
while (result)
{
ft_printf("%s\n", result->content);
result = result->next;
}
}

View File

@@ -44,8 +44,9 @@ int main(int ac, char **av)
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, flag);
// print_result(result);
result = sort_algo(stack);
(void)flag;
print_result(result);
ps_stop(stack, NULL, result, 0);
return(0);
}

View File

@@ -1,19 +1,18 @@
#include "pushswap.h"
void sa(t_list *pack)
t_list *sa(t_stack **stack, t_list **solution)
{
t_stack *stack_a;
t_list **solution;
int tmp;
t_stack *a;
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")));
a = *stack;
if (!a->next)
return (NULL);
tmp = a->n;
a->n = a->next->n;
a->next->n = tmp;
fill_solution(stack, NULL, solution, ft_strdup("sa"));
return (NULL);
}