11 Commits

19 changed files with 136 additions and 40 deletions

31
.clang-format Normal file
View File

@@ -0,0 +1,31 @@
Language: C
AlignConsecutiveDeclarations: false
AlignConsecutiveMacros: false
AlignEscapedNewlinesLeft: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
BinPackArguments: true
BinPackParameters: true
BreakBeforeBraces: Allman
BreakBeforeTernaryOperators: false
ColumnLimit: 80
IndentWidth: 4
KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 1
PointerAlignment: Right
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakString: 1
PenaltyExcessCharacter: 0
SpaceAfterCStyleCast: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
TabWidth: 4
UseTab: Always

View File

@@ -33,7 +33,7 @@ INSERTION = insertion.c
FLAGS_FILES = algorithms_sort.c flag.c bench.c
PARSING = ft_atoi.c parsing.c ft_strncmp.c ft_split.c ft_strlen.c ft_substr.c ft_itoa.c ft_isdigit.c \
ft_strjoin.c ft_strlcat.c ft_strlcpy.c parsing_2.c disorder.c
ft_strjoin.c ft_strlcat.c ft_strlcpy.c parsing_2.c disorder.c indexer.c
CHECKER_FILES = check_error.c verif_flag.c verif_is_digit.c verif_overflow.c verif_double.c

View File

@@ -12,17 +12,17 @@
#include "push_swap.h"
static int still_unit_value(t_stacks *stacks, int unit)
static int still_unit_index(t_stacks *stacks, int unit)
{
t_stack *temp;
temp = stacks->a;
if (!temp || temp->value >= unit)
if (!temp || temp->index >= unit)
return (1);
temp = temp->next;
while (temp != stacks->a)
{
if (temp->value >= unit)
if (temp->index >= unit)
return (1);
temp = temp->next;
}
@@ -43,9 +43,9 @@ static void push_by_number_to_b(t_stacks *stacks, int unit, int nb)
temp = temp->next;
}
i = 0;
while (i < s_len && still_unit_value(stacks, unit))
while (i < s_len && still_unit_index(stacks, unit))
{
if (stacks->a && (stacks->a->value % (unit * 10)) / unit == nb)
if (stacks->a && (stacks->a->index % (unit * 10)) / unit == nb)
pb(stacks);
else
ra(stacks);
@@ -58,7 +58,7 @@ static void rec_sort(t_stacks *stacks, int unit)
int i;
i = 0;
if (!still_unit_value(stacks, unit))
if (!still_unit_index(stacks, unit))
return ;
while (i <= 9)
{

View File

@@ -18,27 +18,27 @@
static int apply_operation(t_stacks *stacks, char buf[1024])
{
if (!ft_strncmp("sa", buf, ft_strlen(buf)))
if (ft_strncmp("sa\n", buf, ft_strlen(buf)))
return (sa(stacks), 1);
if (!ft_strncmp("sb", buf, ft_strlen(buf)))
if (ft_strncmp("sb\n", buf, ft_strlen(buf)))
return (sb(stacks), 1);
if (!ft_strncmp("ss", buf, ft_strlen(buf)))
if (ft_strncmp("ss\n", buf, ft_strlen(buf)))
return (ss(stacks), 1);
if (!ft_strncmp("pa", buf, ft_strlen(buf)))
if (ft_strncmp("pa\n", buf, ft_strlen(buf)))
return (pa(stacks), 1);
if (!ft_strncmp("pb", buf, ft_strlen(buf)))
if (ft_strncmp("pb\n", buf, ft_strlen(buf)))
return (pb(stacks), 1);
if (!ft_strncmp("ra", buf, ft_strlen(buf)))
if (ft_strncmp("ra\n", buf, ft_strlen(buf)))
return (ra(stacks), 1);
if (!ft_strncmp("rb", buf, ft_strlen(buf)))
if (ft_strncmp("rb\n", buf, ft_strlen(buf)))
return (rb(stacks), 1);
if (!ft_strncmp("rr", buf, ft_strlen(buf)))
if (ft_strncmp("rr\n", buf, ft_strlen(buf)))
return (rr(stacks), 1);
if (!ft_strncmp("rra", buf, ft_strlen(buf)))
if (ft_strncmp("rra\n", buf, ft_strlen(buf)))
return (rra(stacks), 1);
if (!ft_strncmp("rrb", buf, ft_strlen(buf)))
if (ft_strncmp("rrb\n", buf, ft_strlen(buf)))
return (rrb(stacks), 1);
if (!ft_strncmp("rrr", buf, ft_strlen(buf)))
if (ft_strncmp("rrr\n", buf, ft_strlen(buf)))
return (rrr(stacks), 1);
return (0);
}
@@ -63,10 +63,10 @@ static int tester(t_stacks *stacks)
buf = get_next_line(0);
}
if (!is_stacks_b_empty(stacks))
return (write(1, "KO\n", 3));
return (secure_write(1, "KO\n", 3));
if (!check_order(stacks->a))
return (write(1, "KO\n", 3));
write(1, "OK\n", 3);
return (secure_write(1, "KO\n", 3));
secure_write(1, "OK\n", 3);
return (0);
}
@@ -96,7 +96,7 @@ int main(int argc, char **argv)
if (!tab)
return (0);
if (!check_error_bonus(tab))
write(2, "Error\n", 7);
secure_write(2, "Error\n", 7);
else
bonus(tab);
free_tab(tab);

BIN
checker_linux Executable file

Binary file not shown.

View File

@@ -19,7 +19,6 @@ void simple(t_stacks *stacks)
int len;
len = stack_a_len(stacks);
stacks->algo = 1;
insertion(stacks, len);
}
@@ -29,7 +28,6 @@ void medium(t_stacks *stacks)
int range;
int len;
stacks->algo = 2;
len = stack_a_len(stacks);
if (len == 2)
sort_two(stacks);
@@ -45,7 +43,6 @@ void medium(t_stacks *stacks)
void complex(t_stacks *stacks)
{
stacks->algo = 3;
radix(stacks);
}
@@ -55,6 +52,7 @@ void adaptive(t_stacks *stacks, char **tab)
float disorder;
i = 0;
stacks->algo = 4;
while (!ft_isdigit(tab[i][0]) && tab[i])
i++;
disorder = stacks->disorder;

View File

@@ -31,7 +31,7 @@ static void print_disorder(t_stacks *stacks)
secure_write(2, ".", 1);
if (ft_strlen(str) == 1)
secure_write(2, "0", 1);
secure_write(2, &str[ft_strlen(str) - 2], 2);
secure_write(2, &str[ft_strlen(str) - 2], ft_strlen(str) - 2);
secure_write(2, "%\n", 2);
free(str);
}

View File

@@ -62,11 +62,20 @@ int pos_bench(char **argv, int mod)
void flags(int pos, int pos_b, char **argv, t_stacks *stacks)
{
if (ft_strncmp(argv[pos], "--simple", 30) && pos > 0)
{
stacks->algo = 1;
simple(stacks);
}
else if (ft_strncmp(argv[pos], "--medium", 30) && pos > 0)
{
stacks->algo = 2;
medium(stacks);
}
else if (ft_strncmp(argv[pos], "--complex", 30) && pos > 0)
{
stacks->algo = 3;
complex(stacks);
}
else if (ft_strncmp(argv[pos], "--adaptive", 30) && pos > 0)
adaptive(stacks, argv);
else

View File

@@ -34,5 +34,6 @@ int ft_strlcat(char *dst, const char *src, int size);
char *join_all(int argc, char **argv);
char **split_all(char *tab);
float compute_disorder(char **strs, int pos);
void indexer(t_stacks *stacks);
#endif

View File

@@ -16,6 +16,7 @@
typedef struct s_stack
{
int value;
long long index;
struct s_stack *next;
struct s_stack *previous;
} t_stack;

2
main.c
View File

@@ -32,7 +32,7 @@ int main(int argc, char **argv)
if (check_error(tab, mod))
test1(tab, len, mod);
else
write(2, "Error\n", 7);
write(2, "Error\n", 6);
free_tab(tab);
return (0);
}

54
parsing/indexer.c Normal file
View File

@@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* indexer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/14 10:14:06 by dgaillet #+# #+# */
/* Updated: 2026/01/14 10:14:08 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static t_stack *first_without_index(t_stack *stack)
{
t_stack *first;
if (stack->index < 0)
return (stack);
first = stack;
stack = stack->next;
while (stack != first)
{
if (stack->index < 0)
return (stack);
stack = stack->next;
}
return ((void *) 0);
}
void indexer(t_stacks *stacks)
{
int i;
int len;
t_stack *lower;
t_stack *temp;
i = 0;
len = stack_a_len(stacks);
while (i < len)
{
lower = first_without_index(stacks->a);
temp = lower->next;
while (temp != stacks->a)
{
if (lower->value > temp->value && temp->index < 0)
lower = temp;
temp = temp->next;
}
lower->index = i;
i++;
}
}

View File

@@ -6,7 +6,7 @@
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/08 14:30:38 by dgaillet #+# #+# */
/* Updated: 2026/01/09 12:05:56 by dgaillet ### ########lyon.fr */
/* Updated: 2026/01/09 14:55:16 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
@@ -17,6 +17,6 @@ void secure_write(int fd, char *str, int len)
{
if (len < 0)
return ;
if (write(fd, str, len) < 0)
if (!str || write(fd, str, len) < 0)
exit (EXIT_FAILURE);
}

View File

@@ -32,7 +32,7 @@ void pa(t_stacks *stacks)
stack_add_front(&(stacks->a), b_push);
stacks->pa++;
if (stacks->print)
write(1, "pa\n", 3);
secure_write(1, "pa\n", 3);
}
void pb(t_stacks *stacks)
@@ -53,5 +53,5 @@ void pb(t_stacks *stacks)
stack_add_front(&(stacks->b), a_push);
stacks->pb++;
if (stacks->print)
write(1, "pb\n", 3);
secure_write(1, "pb\n", 3);
}

View File

@@ -18,7 +18,7 @@ void rra(t_stacks *stacks)
if (stacks && stacks->a && stacks->a->previous)
stacks->a = stacks->a->previous;
stacks->rra++;
write(1, "rra\n", 4);
secure_write(1, "rra\n", 4);
}
void rrb(t_stacks *stacks)
@@ -27,7 +27,7 @@ void rrb(t_stacks *stacks)
stacks->b = stacks->b->previous;
stacks->rrb++;
if (stacks->print)
write(1, "rrb\n", 4);
secure_write(1, "rrb\n", 4);
}
void rrr(t_stacks *stacks)
@@ -38,5 +38,5 @@ void rrr(t_stacks *stacks)
stacks->a = stacks->a->previous;
stacks->rrr++;
if (stacks->print)
write(1, "rrr\n", 4);
secure_write(1, "rrr\n", 4);
}

View File

@@ -19,7 +19,7 @@ void ra(t_stacks *stacks)
stacks->a = stacks->a->next;
stacks->ra++;
if (stacks->print)
write(1, "ra\n", 3);
secure_write(1, "ra\n", 3);
}
void rb(t_stacks *stacks)
@@ -28,7 +28,7 @@ void rb(t_stacks *stacks)
stacks->b = stacks->b->next;
stacks->rb++;
if (stacks->print)
write(1, "rb\n", 3);
secure_write(1, "rb\n", 3);
}
void rr(t_stacks *stacks)
@@ -39,5 +39,5 @@ void rr(t_stacks *stacks)
stacks->b = stacks->b->next;
stacks->rr++;
if (stacks->print)
write(1, "rr\n", 3);
secure_write(1, "rr\n", 3);
}

View File

@@ -21,6 +21,7 @@ t_stack *new_stack(int value)
if (!new)
return (NULL);
new->value = value;
new->index = -1;
new->next = NULL;
new->previous = NULL;
return (new);

View File

@@ -26,7 +26,7 @@ void sa(t_stacks *stacks)
a->next->value = stock;
stacks->sa++;
if (stacks->print)
write(1, "sa\n", 3);
secure_write(1, "sa\n", 3);
}
void sb(t_stacks *stacks)
@@ -42,7 +42,7 @@ void sb(t_stacks *stacks)
b->next->value = stock;
stacks->sb++;
if (stacks->print)
write(1, "sb\n", 3);
secure_write(1, "sb\n", 3);
}
void ss(t_stacks *stacks)
@@ -69,5 +69,5 @@ void ss(t_stacks *stacks)
}
stacks->ss++;
if (stacks->print)
write(1, "ss\n", 3);
secure_write(1, "ss\n", 3);
}

View File

@@ -23,6 +23,7 @@ int test1(char **tab, int len, int mod)
if (mod == -1)
return (0);
stacks = init_stacks(len, tab, mod);
indexer(stacks);
if (!stacks)
return (0);
if (check_order(stacks->a))