This commit is contained in:
Camille Chauvet 2022-11-29 14:06:22 +01:00
parent bb6e9ddd85
commit f4bd2c9432
13 changed files with 167 additions and 70 deletions

39
Makefile Normal file
View File

@ -0,0 +1,39 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/11/28 16:34:37 by cchauvet #+# #+# #
# Updated: 2022/11/28 17:17:40 by cchauvet ### ########.fr #
# #
# **************************************************************************** #
SRCS = ft_swap.c ft_putstr.c ft_p.c ft_sort.c ft_tablen.c ft_get_max.c ft_get_min.c main.c ft_atoi.c ft_bit_finder.c ft_radix.c ft_isnum.c ft_get_bit_max.c ft_r.c
OBJS = ${SRCS:.c=.o}
NAME = push_swap
CC = clang
CFLAGS = -Wall -Werror -Wextra -g
%.o: %.c
${CC} ${CFLAGS} -c -o $@ $<
all: ${NAME}
${NAME}: ${OBJS}
${CC} ${OBJS} -o ${NAME}
clean:
rm -f ${OBJS}
fclean: clean
rm -f ${NAME}
re: fclean all
.PHONY: all clean fclean re

BIN
a.out

Binary file not shown.

31
ft_get_bit_max.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_get_bit_max.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/28 16:22:14 by cchauvet #+# #+# */
/* Updated: 2022/11/28 17:56:32 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "pushswap.h"
int ft_get_bit_max(int *tab)
{
int i;
int max;
if (tab[0] == -1)
return (INT_MIN);
max = tab[0];
i = 1;
while (tab[i] != -1)
{
if (ft_bit_finder(tab[i], 1) < ft_bit_finder(max, 1))
max = tab[i];
i++;
}
return (max);
}

View File

@ -6,19 +6,17 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/14 16:31:57 by cchauvet #+# #+# */ /* Created: 2022/11/14 16:31:57 by cchauvet #+# #+# */
/* Updated: 2022/11/17 00:59:37 by cchauvet ### ########.fr */ /* Updated: 2022/11/28 18:07:53 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "pushswap.h" #include "pushswap.h"
int ft_get_max_index(int *tab) int ft_get_max_index(int *tab, int size)
{ {
int max; int max;
int i; int i;
int size;
size = ft_get_last_index(tab);
i = 1; i = 1;
max = 0; max = 0;
while (i < size) while (i < size)
@ -30,7 +28,7 @@ int ft_get_max_index(int *tab)
return (max); return (max);
} }
int ft_get_max(int *tab) int ft_get_max(int *tab, int size)
{ {
return (tab[ft_get_max_index(tab)]); return (tab[ft_get_max_index(tab, size)]);
} }

View File

@ -6,19 +6,17 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/15 13:20:22 by cchauvet #+# #+# */ /* Created: 2022/11/15 13:20:22 by cchauvet #+# #+# */
/* Updated: 2022/11/17 01:00:22 by cchauvet ### ########.fr */ /* Updated: 2022/11/28 16:46:47 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "pushswap.h" #include "pushswap.h"
int get_min_index(int *tab) int get_min_index(int *tab, int size)
{ {
int i; int i;
int min; int min;
int size;
size = ft_get_last_index(tab);
min = 0; min = 0;
i = 1; i = 1;
while (i < size) while (i < size)
@ -32,5 +30,5 @@ int get_min_index(int *tab)
int get_min(int *tab, int size) int get_min(int *tab, int size)
{ {
return (tab[get_min_index(tab)]); return (tab[get_min_index(tab, size)]);
} }

26
ft_p.c
View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/09 16:38:34 by cchauvet #+# #+# */ /* Created: 2022/11/09 16:38:34 by cchauvet #+# #+# */
/* Updated: 2022/11/17 01:13:44 by cchauvet ### ########.fr */ /* Updated: 2022/11/28 18:15:41 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,11 +17,23 @@ void ft_p(int *tab_src, int *tab_dst)
int size_src; int size_src;
int size_dst; int size_dst;
size_src = ft_get_last_index(tab_src); size_src = ft_tablen(tab_src);
size_dst = ft_get_last_index(tab_dst); size_dst = ft_tablen(tab_dst);
if (size_dst == 0) if (size_src == 0)
return ; return ;
size_dst--; tab_dst[size_dst] = tab_src[size_src - 1];
tab_src[size_src] = tab_dst[size_dst]; tab_dst[size_dst + 1] = -1;
size_src++; tab_src[size_src - 1] = -1;
}
void ft_pb(int *tab_a, int *tab_b)
{
ft_p(tab_b, tab_a);
ft_putstr("pb");
}
void ft_pa(int *tab_a, int *tab_b)
{
ft_p(tab_a, tab_b);
ft_putstr("pa");
} }

View File

@ -1,34 +1,37 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* ft_s.c :+: :+: :+: */ /* ft_r.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/08 20:12:44 by cchauvet #+# #+# */ /* Created: 2022/11/28 16:50:03 by cchauvet #+# #+# */
/* Updated: 2022/11/17 01:06:50 by cchauvet ### ########.fr */ /* Updated: 2022/11/28 17:13:21 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "pushswap.h" #include "pushswap.h"
void ft_s(int *tab) static void ft_r(int *tab)
{ {
int size; int i;
size = ft_get_last_index(tab); i = 0;
ft_swap(tab + size - 1, tab + size - 2); while (tab[i + 1] != -1)
{
ft_swap(tab + i, tab + i + 1);
i++;
}
} }
void ft_ss(int *tab1, int *tab2) void ft_ra(int *tab)
{ {
int size1; ft_putstr("ra");
int size2; ft_r(tab);
}
size1 = ft_get_last_index(tab1);
size2 = ft_get_last_index(tab2); void ft_rb(int *tab)
if (size1 < 2 || size2 < 2) {
return ; ft_putstr("rb");
ft_s(tab1); ft_r(tab);
ft_s(tab2);
} }

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/15 18:16:31 by cchauvet #+# #+# */ /* Created: 2022/11/15 18:16:31 by cchauvet #+# #+# */
/* Updated: 2022/11/23 19:05:54 by cchauvet ### ########.fr */ /* Updated: 2022/11/28 16:29:46 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,18 +16,18 @@ void ft_radix_sort(int *tab_a, int *tab_b)
{ {
int i; int i;
while (ft_bit_finder(ft_get_bit_max(tab_a), 1) != 31) while (ft_bit_finder(ft_get_bit_max(tab_a), 1) != (int) sizeof(int) * 8 - 1)
{ {
i = 0; i = 0;
while (i < ft_tablen(tab_a)) while (i < ft_tablen(tab_a))
{ {
if (((tab_a[i] >> (sizeof(int) * 8 - 1)) & 1) == 0) if (((tab_a[i] >> (sizeof(int) * 8 - 1)) & 1) == 0)
ft_r(tab_a); ft_ra(tab_a);
else else
ft_p(tab_a, tab_b); ft_pa(tab_a, tab_b);
i++; i++;
} }
while (0 < ft_tablen(tab_b)) while (0 < ft_tablen(tab_b))
ft_p(tab_b, tab_a); ft_pb(tab_b, tab_a);
} }
} }

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/08 19:47:56 by cchauvet #+# #+# */ /* Created: 2022/11/08 19:47:56 by cchauvet #+# #+# */
/* Updated: 2022/11/23 18:47:11 by cchauvet ### ########.fr */ /* Updated: 2022/11/28 18:08:15 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -29,35 +29,42 @@ static int *ft_values_simplifier(int *tab, int size)
i = 0; i = 0;
while (i < size) while (i < size)
{ {
max_index = ft_get_max_index(tab, i); max_index = ft_get_max_index(tab, size);
out[max_index] = (size - i) << shift; out[max_index] = (size - i) << shift;
tab[max_index] = INT_MIN; tab[max_index] = INT_MIN;
i++; i++;
} }
out[size] = -1;
free (tab); free (tab);
return (out); return (out);
} }
static int *ft_tab_init(int *tab_a, int *tab_b, int size) static int *ft_tab_init(int **tab_a, int **tab_b, int size)
{ {
size++; int *out_b;
tab_b = malloc(sizeof(int) * (size + 1)); int *out_a;
tab_a = ft_values_simplifier(tab_a, size);
if (tab_a == NULL || tab_b == NULL) out_b = malloc(sizeof(int) * (size + 1));
out_a = ft_values_simplifier(*tab_a, size);
if (out_a == NULL || out_b == NULL)
{ {
free(tab_a); free(*tab_a);
free(tab_b); free(out_a);
free(out_b);
return (NULL); return (NULL);
} }
tab_b[size] = -1; out_b[0] = -1;
return (tab_a); *tab_a = out_a;
*tab_b = out_b;
return (out_a);
} }
void ft_sort(int *tab_a, int size_a) void ft_sort(int *tab_a, int size_a)
{ {
int *tab_b; int *tab_b;
if (ft_tab_init(tab_a, tab_b, size_a) == NULL) tab_b = NULL;
if (ft_tab_init(&tab_a, &tab_b, size_a) == NULL)
return ; return ;
ft_radix_sort(tab_a, tab_b); ft_radix_sort(tab_a, tab_b);
} }

13
main.c
View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/09 17:10:47 by cchauvet #+# #+# */ /* Created: 2022/11/09 17:10:47 by cchauvet #+# #+# */
/* Updated: 2022/11/09 17:12:25 by cchauvet ### ########.fr */ /* Updated: 2022/11/28 17:57:29 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,17 +20,18 @@ int main(int argc, char *argv[])
tab_a = malloc(argc * sizeof(int)); tab_a = malloc(argc * sizeof(int));
if (tab_a == NULL) if (tab_a == NULL)
return (1); return (1);
i = 0; i = 1;
while (i + 1 < argc) while (i < argc)
{ {
if (!ft_isnum(argv[i + 1])) if (!ft_isnum(argv[i]))
{ {
ft_putstr("Erreur: args doesn't containe only numbers !"); ft_putstr("Erreur: args doesn't containe only numbers !");
return (1); return (1);
} }
tab_a[i] = ft_atoi(argv[i + 1]); tab_a[i - 1] = ft_atoi(argv[i]);
i++; i++;
} }
ft_sort(tab_a, i); if (argc > 2)
ft_sort(tab_a, i - 1);
return (0); return (0);
} }

BIN
push_swap Executable file

Binary file not shown.

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/08 18:19:44 by cchauvet #+# #+# */ /* Created: 2022/11/08 18:19:44 by cchauvet #+# #+# */
/* Updated: 2022/11/23 19:06:46 by cchauvet ### ########.fr */ /* Updated: 2022/11/28 16:30:39 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -29,10 +29,13 @@ int ft_get_min_index(int *tab, int len);
int ft_tablen(int *tab); int ft_tablen(int *tab);
int ft_bit_finder(int big, int tofind); int ft_bit_finder(int big, int tofind);
char **ft_radix_sort(int *tab_a, int *tab_b); int ft_get_bit_max(int *tab);
void ft_radix_sort(int *tab_a, int *tab_b);
void ft_swap(int *a, int *b); void ft_swap(int *a, int *b);
void ft_p(int *tab_a, int *tab_b); void ft_pa(int *tab_a, int *tab_b);
void ft_s(int *tab); void ft_pb(int *tab_a, int *tab_b);
void ft_ss(int *tab1, int *tab2); void ft_ra(int *tab_a);
void ft_rb(int *tab_b);
#endif #endif

19
test.c
View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/23 16:43:32 by cchauvet #+# #+# */ /* Created: 2022/11/23 16:43:32 by cchauvet #+# #+# */
/* Updated: 2022/11/23 18:54:08 by cchauvet ### ########.fr */ /* Updated: 2022/11/28 17:15:24 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,11 +15,16 @@
int main() int main()
{ {
unsigned int a; int tab[] = {1,2,3,4,-1};
int shift; int i;
a = 0; i = -1;
shift = ft_bit_finder(a, 1); while (tab[++i] != -1)
printf("%u / %d\n", shift, 32); printf("%d, ", tab[i]);
printf("%u", a << shift); printf("\n");
ft_ra(tab);
printf("\n");
i = -1;
while (tab[++i] != -1)
printf("%d, ", tab[i]);
} }