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

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);
}
/*
** 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
srcs/algo_bubble_sort.c Normal file
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);
}

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)
{
char *stack;
(void)b;
stack = ft_strdup(sp);
stack = ft_strjoinfree(stack, ft_strdup(" a"));
stack = ft_strjoinfree(stack, ft_strdup("!a:"));
while(a)
{
stack = ft_strjoinfree(stack, ft_strdup(" "));
stack = ft_strjoinfree(stack, ft_itoa(a->n));
a = a->next;
}
stack = ft_strjoinfree(stack, ft_strdup("!b:"));
while(b)
{
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));
}
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_putchar('\n');
}
void print_result(t_list *result)
void print_result(t_list *result, int flag)
{
char **part;
if (!flag)
result = result->next;
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;
}
}

91
srcs/push_swap.c Normal file
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);
}

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

View File

@@ -1,5 +1,5 @@
#include "pushswap.h"
#include "push_swap.h"
void ps_usage(void)
{

View File

@@ -1,5 +1,5 @@
#include "pushswap.h"
#include "push_swap.h"
t_list *sa(t_stack **stack, t_list **solution)
{