adding all my files for my setup utils_struct_tab compare_value medium_algo utils_medium

This commit is contained in:
Maoake Teriierooiterai
2025-12-10 16:20:49 +01:00
parent c9066f4a9b
commit 977a4a4b1a
6 changed files with 226 additions and 20 deletions

37
algorithms/medium_algo.c Normal file
View File

@@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* medium_algo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/10 07:14:45 by mteriier #+# #+# */
/* Updated: 2025/12/10 07:14:51 by mteriier ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
t_tab *get_tabs(t_stack *first)
{
t_tab *tmp;
t_tab *first_tab;
int len_stack;
int scan_nb_in_tab;
len_stack = stack_len(first);
first_tab = first_tab(first);
if (!first_tab)
return (NULL);
scan_nb_in_tab = tab->nb_in;
tmp = first_tab;
while (scan_nb_in_tab < len_stack)
{
tmp->next = get_next_tab(first, tmp);
if (!(tmp->next))
return (free_tab(first_tab));
tmp = tmp->next;
scan_nb_in_tab += tmp->nb_in;
}
return (first_tab);
}

View File

@@ -0,0 +1,89 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils_medium.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/10 13:58:40 by mteriier #+# #+# */
/* Updated: 2025/12/10 13:58:42 by mteriier ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int get_first_lower(t_stack *first)
{
t_stack *tmp;
int lower;
tmp = first;
lower = tmp->value;
while (tmp->next != first)
{
if (lower < tmp->value)
lower = tmp->value;
tmp = tmp->next;
}
return (lower);
}
int get_next_lower(t_stack *first, int old_lower)
{
t_stack *tmp;
int next_lower;
tmp = first;
lower = tmp->value;
while (tmp->next != first)
{
if (tmp->value != old_lower && lower < tmp->value)
lower = tmp->value;
tmp = tmp->next;
}
return (lower);
}
int calcul_range(int value, int range)
{
int max_range;
max_range = 0;
if (value < 0)
while (max_range > value)
max_range -= range;
else
while (max_range <= value)
max_range += range;
if (max_range < 0)
return (max_range + range - 1);
return (max_range - 1);
}
int in_range(int value, int max_range)
{
int min_range;
min_range = max_range - 9;
if (value <= max_range && value >= min_range)
return (1);
return (0);
}
int get_number_in_range(int range, t_stacks *piles)
{
int nb_in;
t_stack *tmp;
t_stack *first;
nb_in = 0;
tmp = piles->a;
first = tmp;
while (tmp->next != first)
{
if (in_range(tmp->value, range))
nb_in++;
tmp = tmp->next;
}
return (nb_in);
}

View File

@@ -0,0 +1,69 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils_struct_tab.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/10 13:36:43 by mteriier #+# #+# */
/* Updated: 2025/12/10 13:36:48 by mteriier ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
t_tab *allocate_tab(int range_max, int nb)
{
t_tab tab;
tab = malloc(sizeof(t_tab));
if (!tab)
return (NULL);
tab->range_max = range_max;
tab->nb_in = nb;
return (tab);
}
void link_tab(t_tab *previous, t_tab *new)
{
previous->next = new;
}
t_tab *first_tab(t_stack *first)
{
t_tab *tab;
int lower;
int range_max;
lower = get_first_lower(first);
range_max = calcul_range(lower, 10);
tab = allocate_tab(range_max, get_number_in_range(range_max, first));
if (!tab)
return (NULL);
return (tab);
}
t_tab *get_next_tab(t_stack *first, t_tab tab)
{
int lower;
int range_max;
t_tab *next_tab;
lower = get_next_lower(first, tab->max_range);
range_max = calcul_range(lower, 10);
next_tab = allocate_tab(range_max, get_number_in_range(range_max, first));
return (next_tab);
}
t_tab *free_tab(t_tab *first)
{
t_tab *tmp;
while (first)
{
tmp = first->next;
free(first);
first = tmp;
}
return (NULL);
}

View File

@@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* compare_value.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/10 11:08:35 by mteriier #+# #+# */
/* Updated: 2025/12/10 11:28:58 by mteriier ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int is_upper_compare(t_stack t1, t_stack t2)
{
if (t1->value > t2->value)
return (1);
return (0);
}

22
main.c
View File

@@ -43,28 +43,12 @@ int main(int argc, char **argv)
stacks = init_big_stacks(argc, argv);
print_all_stack(stacks->a, stacks->a, 'A');
print_all_stack(stacks->b, stacks->b, 'B');
sa(stacks);
print_all_stack(stacks->a, stacks->a, 'A');
print_all_stack(stacks->b, stacks->b, 'B');
pb(stacks);
print_all_stack(stacks->a, stacks->a, 'A');
print_all_stack(stacks->b, stacks->b, 'B');
pa(stacks);
print_all_stack(stacks->a, stacks->a, 'A');
print_all_stack(stacks->b, stacks->b, 'B');
rra(stacks);
print_all_stack(stacks->a, stacks->a, 'A');
print_all_stack(stacks->b, stacks->b, 'B');
pb(stacks);
pb(stacks);
print_all_stack(stacks->a, stacks->a, 'A');
print_all_stack(stacks->b, stacks->b, 'B');
rrb(stacks);
print_all_stack(stacks->a, stacks->a, 'A');
print_all_stack(stacks->b, stacks->b, 'B');
rrr(stacks);
print_all_stack(stacks->a, stacks->a, 'A');
print_all_stack(stacks->b, stacks->b, 'B');
}
if (stacks->a)
stack_clear_all(stacks->a, stacks->a);
if (stacks->b)
stack_clear_all(stacks->b, stacks->b);
}

View File

@@ -26,6 +26,13 @@ typedef struct s_stacks
t_stack *b;
} t_stacks;
typedef struct s_tab
{
int range_max;
int nb_in;
struct s_tab *next;
} t_tab;
/*STACK_FUNCTIONS*/
void pa(t_stacks *stacks);
void pb(t_stacks *stacks);