d
This commit is contained in:
commit
bb6e9ddd85
36
ft_atoi.c
Normal file
36
ft_atoi.c
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_atoi.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/11/08 18:27:58 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2022/11/09 17:13:11 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "pushswap.h"
|
||||||
|
|
||||||
|
int ft_atoi(char *str)
|
||||||
|
{
|
||||||
|
int nb;
|
||||||
|
int i;
|
||||||
|
int sign;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
sign = 1;
|
||||||
|
nb = 0;
|
||||||
|
while (str[i] == '+' || str[i] == '-')
|
||||||
|
{
|
||||||
|
if (str[i] == '-')
|
||||||
|
sign *= -1;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
while (str[i] != '\0')
|
||||||
|
{
|
||||||
|
nb = nb * 10 + str[i] - '0';
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (nb * sign);
|
||||||
|
}
|
23
ft_bit_finder.c
Normal file
23
ft_bit_finder.c
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_bit_finder.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/11/23 17:31:10 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2022/11/23 18:35:41 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "pushswap.h"
|
||||||
|
|
||||||
|
int ft_bit_finder(int big, int tofind)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = (int) sizeof(int) * 8 - 1;
|
||||||
|
while (i > 0 && ((big >> i) & 1) != tofind)
|
||||||
|
i--;
|
||||||
|
return (sizeof(int) * 8 - i - 1);
|
||||||
|
}
|
36
ft_get_max.c
Normal file
36
ft_get_max.c
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_get_max.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/11/14 16:31:57 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2022/11/17 00:59:37 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "pushswap.h"
|
||||||
|
|
||||||
|
int ft_get_max_index(int *tab)
|
||||||
|
{
|
||||||
|
int max;
|
||||||
|
int i;
|
||||||
|
int size;
|
||||||
|
|
||||||
|
size = ft_get_last_index(tab);
|
||||||
|
i = 1;
|
||||||
|
max = 0;
|
||||||
|
while (i < size)
|
||||||
|
{
|
||||||
|
if (tab[max] < tab[i])
|
||||||
|
max = i;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (max);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_get_max(int *tab)
|
||||||
|
{
|
||||||
|
return (tab[ft_get_max_index(tab)]);
|
||||||
|
}
|
36
ft_get_min.c
Normal file
36
ft_get_min.c
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_get_min.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/11/15 13:20:22 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2022/11/17 01:00:22 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "pushswap.h"
|
||||||
|
|
||||||
|
int get_min_index(int *tab)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int min;
|
||||||
|
int size;
|
||||||
|
|
||||||
|
size = ft_get_last_index(tab);
|
||||||
|
min = 0;
|
||||||
|
i = 1;
|
||||||
|
while (i < size)
|
||||||
|
{
|
||||||
|
if (tab[min] > tab[i])
|
||||||
|
min = i;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (min);
|
||||||
|
}
|
||||||
|
|
||||||
|
int get_min(int *tab, int size)
|
||||||
|
{
|
||||||
|
return (tab[get_min_index(tab)]);
|
||||||
|
}
|
24
ft_isnum.c
Normal file
24
ft_isnum.c
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_isnum.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/11/08 18:43:06 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2022/11/08 19:52:48 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "pushswap.h"
|
||||||
|
|
||||||
|
int ft_isnum(char *str)
|
||||||
|
{
|
||||||
|
while (*str != '\0')
|
||||||
|
{
|
||||||
|
if (!((*str >= '0' && *str <= '9') || *str == '-' || *str == '+'))
|
||||||
|
return (0);
|
||||||
|
str++;
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
27
ft_p.c
Normal file
27
ft_p.c
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_p.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/11/09 16:38:34 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2022/11/17 01:13:44 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "pushswap.h"
|
||||||
|
|
||||||
|
void ft_p(int *tab_src, int *tab_dst)
|
||||||
|
{
|
||||||
|
int size_src;
|
||||||
|
int size_dst;
|
||||||
|
|
||||||
|
size_src = ft_get_last_index(tab_src);
|
||||||
|
size_dst = ft_get_last_index(tab_dst);
|
||||||
|
if (size_dst == 0)
|
||||||
|
return ;
|
||||||
|
size_dst--;
|
||||||
|
tab_src[size_src] = tab_dst[size_dst];
|
||||||
|
size_src++;
|
||||||
|
}
|
19
ft_putstr.c
Normal file
19
ft_putstr.c
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_putstr.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/11/08 18:24:45 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2022/11/08 18:26:33 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "pushswap.h"
|
||||||
|
|
||||||
|
void ft_putstr(char *str)
|
||||||
|
{
|
||||||
|
while (*str != '\0')
|
||||||
|
write(1, str++, 1);
|
||||||
|
}
|
33
ft_radix.c
Normal file
33
ft_radix.c
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_radix.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/11/15 18:16:31 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2022/11/23 19:05:54 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "pushswap.h"
|
||||||
|
|
||||||
|
void ft_radix_sort(int *tab_a, int *tab_b)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
while (ft_bit_finder(ft_get_bit_max(tab_a), 1) != 31)
|
||||||
|
{
|
||||||
|
i = 0;
|
||||||
|
while (i < ft_tablen(tab_a))
|
||||||
|
{
|
||||||
|
if (((tab_a[i] >> (sizeof(int) * 8 - 1)) & 1) == 0)
|
||||||
|
ft_r(tab_a);
|
||||||
|
else
|
||||||
|
ft_p(tab_a, tab_b);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
while (0 < ft_tablen(tab_b))
|
||||||
|
ft_p(tab_b, tab_a);
|
||||||
|
}
|
||||||
|
}
|
34
ft_s.c
Normal file
34
ft_s.c
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_s.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/11/08 20:12:44 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2022/11/17 01:06:50 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "pushswap.h"
|
||||||
|
|
||||||
|
void ft_s(int *tab)
|
||||||
|
{
|
||||||
|
int size;
|
||||||
|
|
||||||
|
size = ft_get_last_index(tab);
|
||||||
|
ft_swap(tab + size - 1, tab + size - 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_ss(int *tab1, int *tab2)
|
||||||
|
{
|
||||||
|
int size1;
|
||||||
|
int size2;
|
||||||
|
|
||||||
|
size1 = ft_get_last_index(tab1);
|
||||||
|
size2 = ft_get_last_index(tab2);
|
||||||
|
if (size1 < 2 || size2 < 2)
|
||||||
|
return ;
|
||||||
|
ft_s(tab1);
|
||||||
|
ft_s(tab2);
|
||||||
|
}
|
63
ft_sort.c
Normal file
63
ft_sort.c
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_sort.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/11/08 19:47:56 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2022/11/23 18:47:11 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "pushswap.h"
|
||||||
|
|
||||||
|
static int *ft_values_simplifier(int *tab, int size)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int *out;
|
||||||
|
int max_index;
|
||||||
|
int shift;
|
||||||
|
|
||||||
|
out = malloc(sizeof(int) * (size + 1));
|
||||||
|
if (out == NULL)
|
||||||
|
{
|
||||||
|
free(tab);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
shift = ft_bit_finder(size, 1);
|
||||||
|
i = 0;
|
||||||
|
while (i < size)
|
||||||
|
{
|
||||||
|
max_index = ft_get_max_index(tab, i);
|
||||||
|
out[max_index] = (size - i) << shift;
|
||||||
|
tab[max_index] = INT_MIN;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
free (tab);
|
||||||
|
return (out);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int *ft_tab_init(int *tab_a, int *tab_b, int size)
|
||||||
|
{
|
||||||
|
size++;
|
||||||
|
tab_b = malloc(sizeof(int) * (size + 1));
|
||||||
|
tab_a = ft_values_simplifier(tab_a, size);
|
||||||
|
if (tab_a == NULL || tab_b == NULL)
|
||||||
|
{
|
||||||
|
free(tab_a);
|
||||||
|
free(tab_b);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
tab_b[size] = -1;
|
||||||
|
return (tab_a);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_sort(int *tab_a, int size_a)
|
||||||
|
{
|
||||||
|
int *tab_b;
|
||||||
|
|
||||||
|
if (ft_tab_init(tab_a, tab_b, size_a) == NULL)
|
||||||
|
return ;
|
||||||
|
ft_radix_sort(tab_a, tab_b);
|
||||||
|
}
|
22
ft_swap.c
Normal file
22
ft_swap.c
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_swap.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/11/08 20:16:30 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2022/11/08 20:17:58 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "pushswap.h"
|
||||||
|
|
||||||
|
void ft_swap(int *a, int *b)
|
||||||
|
{
|
||||||
|
int temp;
|
||||||
|
|
||||||
|
temp = *a;
|
||||||
|
*a = *b;
|
||||||
|
*b = temp;
|
||||||
|
}
|
23
ft_tablen.c
Normal file
23
ft_tablen.c
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_tablen.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/11/23 16:15:15 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2022/11/23 16:23:26 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "pushswap.h"
|
||||||
|
|
||||||
|
int ft_tablen(int *tab)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (tab[i] != -1)
|
||||||
|
i++;
|
||||||
|
return (i);
|
||||||
|
}
|
36
main.c
Normal file
36
main.c
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* main.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/11/09 17:10:47 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2022/11/09 17:12:25 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "pushswap.h"
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int *tab_a;
|
||||||
|
|
||||||
|
tab_a = malloc(argc * sizeof(int));
|
||||||
|
if (tab_a == NULL)
|
||||||
|
return (1);
|
||||||
|
i = 0;
|
||||||
|
while (i + 1 < argc)
|
||||||
|
{
|
||||||
|
if (!ft_isnum(argv[i + 1]))
|
||||||
|
{
|
||||||
|
ft_putstr("Erreur: args doesn't containe only numbers !");
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
tab_a[i] = ft_atoi(argv[i + 1]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
ft_sort(tab_a, i);
|
||||||
|
return (0);
|
||||||
|
}
|
38
pushswap.h
Normal file
38
pushswap.h
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* pushswap.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/11/08 18:19:44 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2022/11/23 19:06:46 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef PUSHSWAP_H
|
||||||
|
# define PUSHSWAP_H
|
||||||
|
# include <unistd.h>
|
||||||
|
# include <stdlib.h>
|
||||||
|
# include <limits.h>
|
||||||
|
|
||||||
|
int ft_isnum(char *str);
|
||||||
|
int ft_atoi(char *str);
|
||||||
|
void ft_putstr(char *str);
|
||||||
|
|
||||||
|
void ft_sort(int *tab, int size);
|
||||||
|
int ft_get_max_index(int *tab, int len);
|
||||||
|
int ft_get_max_index(int *tab, int len);
|
||||||
|
int ft_get_min_index(int *tab, int len);
|
||||||
|
int ft_get_min_index(int *tab, int len);
|
||||||
|
|
||||||
|
int ft_tablen(int *tab);
|
||||||
|
|
||||||
|
int ft_bit_finder(int big, int tofind);
|
||||||
|
char **ft_radix_sort(int *tab_a, int *tab_b);
|
||||||
|
|
||||||
|
void ft_swap(int *a, int *b);
|
||||||
|
void ft_p(int *tab_a, int *tab_b);
|
||||||
|
void ft_s(int *tab);
|
||||||
|
void ft_ss(int *tab1, int *tab2);
|
||||||
|
#endif
|
25
test.c
Normal file
25
test.c
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* test.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/11/23 16:43:32 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2022/11/23 18:54:08 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "pushswap.h"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
unsigned int a;
|
||||||
|
int shift;
|
||||||
|
|
||||||
|
a = 0;
|
||||||
|
shift = ft_bit_finder(a, 1);
|
||||||
|
printf("%u / %d\n", shift, 32);
|
||||||
|
printf("%u", a << shift);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user