ca marche trop bien

This commit is contained in:
Camille Chauvet 2022-12-11 15:49:45 +01:00
parent 224d9bb1a4
commit ea2251bed4
15 changed files with 113 additions and 31 deletions

View File

@ -6,11 +6,11 @@
# By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ # # By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2022/11/28 16:34:37 by cchauvet #+# #+# # # Created: 2022/11/28 16:34:37 by cchauvet #+# #+# #
# Updated: 2022/12/09 18:03:57 by cchauvet ### ########.fr # # Updated: 2022/12/11 15:02:59 by cchauvet ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
SRCS = ft_swap.c ft_putstr.c ft_p.c ft_sort.c ft_tablen.c main.c ft_atoi.c ft_radix.c ft_isnum.c ft_r.c ft_is_sorted.c ft_bozo_sort.c ft_get_max.c ft_rr.c ft_s.c SRCS = ft_swap.c ft_putstr.c ft_p.c ft_sort.c ft_tablen.c main.c ft_atoi.c ft_radix.c ft_isnum.c ft_r.c ft_is_sorted.c ft_bozo_sort.c ft_get_max.c ft_rr.c ft_s.c ft_split.c ft_strlen.c
OBJS = ${SRCS:.c=.o} OBJS = ${SRCS:.c=.o}

View File

@ -6,14 +6,21 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/09 14:24:03 by cchauvet #+# #+# */ /* Created: 2022/12/09 14:24:03 by cchauvet #+# #+# */
/* Updated: 2022/12/09 19:14:50 by cchauvet ### ########.fr */ /* Updated: 2022/12/11 15:13:33 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "pushswap.h" #include "pushswap.h"
static void ft_specific_bozo_sort(t_tab tab_a, t_tab tab_b) static void ft_specific_bozo_sort(t_tab tab_a)
{ {
if (ft_tablen(tab_a) < 3)
{
if (!ft_is_sorted(tab_a))
ft_ra(tab_a);
}
else
{
if (tab_a[0] == tab_a[1] - 1) if (tab_a[0] == tab_a[1] - 1)
ft_rra(tab_a); ft_rra(tab_a);
if (tab_a[0] == tab_a[1] + 1 && tab_a[1] == tab_a[2] + 1) if (tab_a[0] == tab_a[1] + 1 && tab_a[1] == tab_a[2] + 1)
@ -23,6 +30,7 @@ static void ft_specific_bozo_sort(t_tab tab_a, t_tab tab_b)
} }
if (tab_a[0] == tab_a[1] + 1) if (tab_a[0] == tab_a[1] + 1)
ft_sa(tab_a); ft_sa(tab_a);
}
} }
void ft_bozo_sort(t_tab tab_a, t_tab tab_b) void ft_bozo_sort(t_tab tab_a, t_tab tab_b)
@ -35,6 +43,7 @@ void ft_bozo_sort(t_tab tab_a, t_tab tab_b)
ft_ra(tab_a); ft_ra(tab_a);
ft_pb(tab_a, tab_b); ft_pb(tab_a, tab_b);
} }
ft_specific_bozo_sort(tab_a);
if (!ft_is_sorted(tab_a)) if (!ft_is_sorted(tab_a))
{ {
ft_pb(tab_a, tab_b); ft_pb(tab_a, tab_b);

Binary file not shown.

BIN
ft_s.o

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 19:47:56 by cchauvet #+# #+# */ /* Created: 2022/11/08 19:47:56 by cchauvet #+# #+# */
/* Updated: 2022/12/09 14:40:33 by cchauvet ### ########.fr */ /* Updated: 2022/12/11 15:25:10 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -33,7 +33,6 @@ static t_tab ft_values_simplifier(long int *tab_in, int size)
i++; i++;
} }
out[size] = STOP_VALUE; out[size] = STOP_VALUE;
free (tab_in);
return (out); return (out);
} }

BIN
ft_sort.o

Binary file not shown.

View File

@ -6,13 +6,13 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/05 19:04:34 by cchauvet #+# #+# */ /* Created: 2022/10/05 19:04:34 by cchauvet #+# #+# */
/* Updated: 2022/12/06 17:28:21 by cchauvet ### ########.fr */ /* Updated: 2022/12/11 15:10:47 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "pushswap.h" #include "pushswap.h"
static void *ft_cancel(char **tab, size_t len) void ft_cancel(char **tab, size_t len)
{ {
size_t i; size_t i;
@ -26,7 +26,6 @@ static void *ft_cancel(char **tab, size_t len)
} }
free(tab); free(tab);
} }
return (NULL);
} }
char *ft_substr(char const *s, unsigned int start, size_t len) char *ft_substr(char const *s, unsigned int start, size_t len)
@ -90,7 +89,10 @@ static char **ft_segsplitter(char **tab, size_t len, const char *s, char c)
let_index++; let_index++;
tab[tab_index] = ft_substr(s, start, let_index - start); tab[tab_index] = ft_substr(s, start, let_index - start);
if (tab[tab_index] == NULL) if (tab[tab_index] == NULL)
return (ft_cancel(tab, tab_index)); {
ft_cancel(tab, tab_index);
return (NULL);
}
tab_index++; tab_index++;
} }
return (tab); return (tab);

BIN
ft_split.o Normal file

Binary file not shown.

BIN
ft_strlen.o Normal file

Binary file not shown.

BIN
ft_substr.o Normal file

Binary file not shown.

93
main.c
View File

@ -6,33 +6,104 @@
/* 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/12/09 15:00:05 by cchauvet ### ########.fr */ /* Updated: 2022/12/11 15:48:20 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "pushswap.h" #include "pushswap.h"
int main(int argc, char *argv[]) int ft_isin(long int *tab, int len, long int value)
{ {
int i; int i;
long int *tab_a;
tab_a = malloc(argc * sizeof(long int)); i = 0;
if (tab_a == NULL) while (i < len)
{
if (tab[i] == value)
return (1); return (1);
i++;
}
return (0);
}
void ft_error(char **splitted)
{
int len;
write(2, "Error\n", 6);
len = 0;
while (splitted[len] != NULL)
len++;
ft_cancel(splitted, len);
}
int ft_parse(long int *tab, const char *s)
{
char **splitted;
int i;
splitted = ft_split(s, ' ');
if (splitted == NULL)
return (1);
i = 0;
while (splitted[i] != NULL)
{
if (!ft_isnum(splitted[i]))
{
ft_error(splitted);
return (0);
}
tab[i] = ft_atoi(splitted[i]);
if (ft_isin(tab, i, tab[i]))
{
ft_error(splitted);
return (0);
}
i++;
}
ft_cancel(splitted, i + 1);
return (i);
}
size_t ft_total_len(int argc, char **argv)
{
size_t len;
int i;
len = 0;
i = 1; i = 1;
while (i < argc) while (i < argc)
{ {
if (!ft_isnum(argv[i])) len += ft_seglen(argv[i], ' ');
i++;
}
return (len);
}
int main(int argc, char *argv[])
{
int i;
int y;
size_t len;
long int *tab_a;
tab_a = malloc(ft_total_len(argc, argv) * sizeof(long int));
if (tab_a == NULL)
return (1);
i = 0;
len = 0;
while (++i < argc)
{
y = ft_parse(tab_a + len, argv[i]);
if (y == 0)
{ {
write(2, "Error\n", 6);
free(tab_a); free(tab_a);
return (1); return (1);
} }
tab_a[i - 1] = ft_atoi(argv[i]); len += y;
i++;
} }
if (argc > 2) if (len >= 2)
ft_sort(tab_a, i - 1); ft_sort(tab_a, len);
free(tab_a);
return (0); return (0);
} }

BIN
main.o

Binary file not shown.

BIN
push_swap

Binary file not shown.

@ -1 +0,0 @@
Subproject commit 59628457df966b6a6a12e63cecaa29456d2ed250

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/12/09 18:51:58 by cchauvet ### ########.fr */ /* Updated: 2022/12/11 15:08:16 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,7 +22,9 @@ int ft_isnum(char *str);
size_t ft_strlen(const char *s); size_t ft_strlen(const char *s);
int ft_atoi(char *str); int ft_atoi(char *str);
void ft_putstr(char *str); void ft_putstr(char *str);
size_t ft_seglen(const char *s, char c);
char **ft_split(const char *s, char c); char **ft_split(const char *s, char c);
void ft_cancel(char **str, size_t len);
void ft_sort(long int *tab, int size); void ft_sort(long int *tab, int size);