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

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -22,55 +22,47 @@ t_stack *init_stack(int ac, char **av);
** stop.c ** stop.c
*/ */
void ps_usage(void); void ps_usage(void);
void ps_error(int err); void ps_error(int i);
void ps_stop(t_stack *stb, t_stack *sta, t_list *lst, int i); 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, int i); t_list *sort_algo(t_stack *stack);
/* /*
** print.c ** print.c
*/ */
void print_result(t_list *result); void fill_solution(t_stack **a, t_stack **b, t_list **lst, char *c);
void print_stack(t_stack *stack, char c); void print_stack(t_stack *stack, char c);
void print_result(t_list *lst);
/* /*
** swap ** swap
*/ */
void sa(t_list *pack); t_list *sa(t_stack **stack, t_list **lst);
void sb(t_list *pack); t_list *sb(t_stack **stack, t_list **lst);
void ss(t_list *pack); t_list *ss(t_stack **a, t_stack **b, t_list **lst);
/* /*
** push ** push
*/ */
/* t_list *pa(t_stack **dst, t_stack **src, t_list **lst);
int push(t_list **dst, t_list **src); t_list *pb(t_stack **dst, t_stack **src, t_list **lst);
int pa();
int pb();
*/
/* /*
** rotate ** rotate
*/ */
/* t_list *ra(t_stack **stack, t_list **lst);
int rotate(t_list **list); t_list *rb(t_stack **stack, t_list **lst);
int ra(); t_list *rr(t_stack **a, t_stack **b, t_list **lst);
int rb();
int rr();
*/
/* /*
** reverse rotate ** reverse rotate
*/ */
/* t_list *rra(t_stack **stack, t_list **lst);
int reverse_rotate(t_list **list); t_list *rrb(t_stack **stack, t_list **lst);
int rra(); t_list *rrr(t_stack **a, t_stack **b, t_list **lst);
int rrb();
int rrr();
*/
#endif #endif

BIN
pushswap

Binary file not shown.

View File

@@ -1,34 +1,20 @@
#include "pushswap.h" #include "pushswap.h"
/* void hugo_sort(t_stack **a, t_list **solution)
** 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))
{ {
if (!flag) // if flag == 0, don't print anything sa(a, solution);
return ; sa(a, solution);
if (!(*solution)) // to print only at first call sa(a, solution);
{
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');
} }
/* /*
** ** 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_stack *b;
t_list *solution; t_list *solution;
@@ -36,21 +22,7 @@ t_list *sort_algo(t_stack *a, int f)
solution = NULL; solution = NULL;
if(!(b = ft_calloc(1, sizeof(t_stack)))) if(!(b = ft_calloc(1, sizeof(t_stack))))
ps_stop(a, b, NULL, 2); ps_stop(a, b, NULL, 2);
action(a, b, solution, f, sa); hugo_sort(&a, &solution);
action(a, b, solution, f, sa); ps_stop(NULL, b, NULL, -1);
// sa(stack_a, stack_b, &solution); return (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
} }

View File

@@ -1,13 +1,11 @@
#include "pushswap.h" #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) (void)a;
{ (void)b;
ft_printf("%s\n", result->content); ft_lstadd_back(solution, ft_lstnew(sp));
result = result->next;
}
} }
void print_stack(t_stack *stack, char c) void print_stack(t_stack *stack, char c)
@@ -23,3 +21,11 @@ void print_stack(t_stack *stack, char c)
ft_putchar('\n'); 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 is_valid(ac, av); // check if usage and list are correct
flag = check_flag(&ac, &av); //check for flag, like print flag = check_flag(&ac, &av); //check for flag, like print
stack = init_stack(ac, av); // create the list from av[] stack = init_stack(ac, av); // create the list from av[]
result = sort_algo(stack, flag); result = sort_algo(stack);
// print_result(result); (void)flag;
print_result(result);
ps_stop(stack, NULL, result, 0); ps_stop(stack, NULL, result, 0);
return(0); return(0);
} }

View File

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