commit 4c83562ca98e752ba2f94da6a6bc82567feb7c4b Author: HexPloR Date: Mon Jun 24 00:05:59 2024 +0200 ez diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..51a3fa2 --- /dev/null +++ b/Makefile @@ -0,0 +1,57 @@ +NAME = push_swap + +DIR_OBJ = obj +OBJ = $(addprefix $(DIR_OBJ)/,*.o) + +DIR_SRC = src +SRC = $(addprefix $(DIR_SRC)/,*.c) + +FLAGS = -Werror -Wall -Wextra -g +CC = cc + +DIR_LIB = lib + +DIR_PRINTF = $(addprefix $(DIR_LIB)/,ft_printf) +FT_PRINTF_LIB = libftprintf.a +FT_PRINTF = $(addprefix $(DIR_PRINTF)/,$(FT_PRINTF_LIB)) + +DIR_LIBFT = $(addprefix $(DIR_LIB)/,libft) +LIBFT_LIB = libft.a +LIBFT = $(addprefix $(DIR_LIBFT)/,$(LIBFT_LIB)) + +$(NAME): $(OBJ) $(FT_PRINTF) $(LIBFT) + $(CC) $(OBJ) -o $(NAME) $(FT_PRINTF) $(LIBFT) + +$(OBJ): $(DIR_OBJ) $(SRC) + $(CC) -c $(FLAGS) $(SRC) + @mv *.o $(DIR_OBJ) + +$(FT_PRINTF): + make -C $(DIR_PRINTF) + +$(LIBFT): + make -C $(DIR_LIBFT) + +$(DIR_OBJ): + @mkdir -p $(DIR_OBJ) + +all: $(NAME) $(FT_PRINTF) $(LIBFT) + make -C $(DIR_PRINTF) + make -C $(DIR_LIBFT) + +clean: + rm -rf $(DIR_OBJ) + make clean -C $(DIR_PRINTF) + make clean -C $(DIR_LIBFT) + +fclean: clean + rm $(NAME) + make fclean -C $(DIR_PRINTF) + make fclean -C $(DIR_LIBFT) + +re: fclean all + +run: + ./push_swap + +.PHONY: all re fclean clean run diff --git a/checker_linux b/checker_linux new file mode 100755 index 0000000..002d593 Binary files /dev/null and b/checker_linux differ diff --git a/includes/push_swap.h b/includes/push_swap.h new file mode 100644 index 0000000..35a61fc --- /dev/null +++ b/includes/push_swap.h @@ -0,0 +1,87 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* push_swap.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/26 16:42:02 by yantoine #+# #+# */ +/* Updated: 2024/03/23 23:04:24 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ +#ifndef PUSH_SWAP_H +# define PUSH_SWAP_H +# include "../lib/ft_printf/header/ft_printf.h" +# include "../lib/libft/libft.h" + +typedef struct Element +{ + struct Element *suivant; + struct Element *precedant; + struct Element *precedant_boucle; + int index; + int instruction; + int instruction_ra; + int instruction_rb; + int instruction_rab; + int wich_instr; + int ptr_num; + long number; +} t_Element; + +typedef struct Liste +{ + t_Element *premier; + t_Element *dernier; + t_Element *lower_rank; + int max; + int min; + int length; + int moyenne; +} t_Liste; + +void set_precedant(t_Liste *liste); +void find_quicker(t_Liste *stack_a, t_Liste *stack_b); +void turc(t_Liste *stack_a, t_Liste *stack_b); +void push_two_number(t_Liste *stack_a, t_Liste *stack_b); +void find_moyenne(t_Liste *liste); +void sort_3(t_Liste *stack_a); +void sort_4(t_Liste *stack_a, t_Liste *stack_b); +void sort_5(t_Liste *stack_a, t_Liste *stack_b); +void big_sort(t_Liste *stack_a, t_Liste *stack_b); +void ft_list_push_back(t_Liste *liste, int valeur); + +void normal_trie(t_Liste *stack_a, t_Liste *stack_b); +void reverse_a_trie(t_Liste *stack_a, t_Liste *stack_b); +void reverse_b_trie(t_Liste *stack_a, t_Liste *stack_b); +void reverse_ab_trie(t_Liste *stack_a, t_Liste *stack_b); + +void swap(t_Liste *liste, char c); +void swap_both(t_Liste *stack_a, t_Liste *stack_b); +void push(t_Liste *stack_a, t_Liste *stack_b, char c); +void rotate(t_Liste *liste, char c); +void rotate_both(t_Liste *stack_a, t_Liste *stack_b); +void reverse_rotate(t_Liste *liste, char c); +void reverse_rotate_both(t_Liste *stack_a, t_Liste *stack_b); + +void free_stack(t_Liste *stack_a, t_Liste *stack_b); +void list_lenght(t_Liste *liste); +void find_max(t_Liste *liste); +void find_min(t_Liste *liste); +void find_last(t_Liste *liste); +void assign_param_list(t_Liste *liste); +void print_stacks(t_Liste *stack_a, t_Liste *stack_b); +void list_index(t_Liste *liste); +void super_algo(t_Liste *stack_a, t_Liste *stack_b); +void find_lower_rank(t_Liste *stack); + +int condition(t_Element *actuel_a, t_Element *actuel_b, \ + t_Liste *stack_b); +int check_following(t_Liste *stack); +int parse_arg(t_Liste *stack_a, char *arg); +int parse_args(t_Liste *stack_a, int argc, char **argv); +int is_sorted(t_Liste *liste); + +t_Element *ft_list_find(t_Liste *liste, int nb); + +#endif diff --git a/lib/ft_printf/Makefile b/lib/ft_printf/Makefile new file mode 100755 index 0000000..17a88e6 --- /dev/null +++ b/lib/ft_printf/Makefile @@ -0,0 +1,30 @@ +NAME = libftprintf.a +CC = cc +CFLAGS = -Wall -Wextra -Werror -c -I$(HEADER_DIR) -g3 +RM = rm -f +AR = ar rcs +SOURCES = ft_printf.c ft_putchar_fd.c ft_putnbr_fd.c ft_putptr_fd.c ft_put_unsigned_nbr_fd.c ft_puthex_upper_fd.c ft_puthex_lower_fd.c ft_putstr_fd.c +SOURCES_DIR = source/ +HEADER = ft_printf.h +HEADER_DIR = header/ +OBJECTS = $(addprefix $(SOURCES_DIR), $(SOURCES:.c=.o)) + +all: $(NAME) + +$(NAME): $(OBJECTS) + $(AR) $(NAME) $(OBJECTS) + +$(OBJECTS): $(SOURCES_DIR)%.o :$(SOURCES_DIR)%.c + $(CC) $(CFLAGS) $< -o $@ + +clean: + $(RM) $(OBJECTS) + +fclean: clean + $(RM) $(NAME) + +re: fclean + @make all + +.PHONY: all clean fclean re + diff --git a/lib/ft_printf/header/ft_printf.h b/lib/ft_printf/header/ft_printf.h new file mode 100755 index 0000000..19ef898 --- /dev/null +++ b/lib/ft_printf/header/ft_printf.h @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/22 10:18:58 by yantoine #+# #+# */ +/* Updated: 2023/12/26 16:09:17 by hexplor ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_PRINTF_H +# define FT_PRINTF_H + +# include +# include +# include +# include +# include + +int pf_puthex_lower_fd(unsigned int n, int fd); +int pf_puthex_upper_fd(unsigned int n, int fd); +int ft_printf(const char *format, ...); +int pf_putchar_fd(char c, int fd); +int pf_putstr_fd(char *s, int fd); +int pf_putnbr_fd(long int n, int fd); +int pf_put_unsigned_nbr_fd(unsigned int n, int fd); +int pf_putptr_fd(unsigned long long n, int fd); + +#endif diff --git a/lib/ft_printf/source/ft_printf.c b/lib/ft_printf/source/ft_printf.c new file mode 100755 index 0000000..f4a7052 --- /dev/null +++ b/lib/ft_printf/source/ft_printf.c @@ -0,0 +1,78 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hexplor +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/29 19:11:52 by hexplor #+# #+# */ +/* Updated: 2023/12/26 16:06:47 by hexplor ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "../header/ft_printf.h" + +static int ft_ptr_format(va_list parameters_info) +{ + unsigned long long ptr_check; + int count; + + count = 0; + ptr_check = va_arg(parameters_info, unsigned long long); + if (ptr_check == 0) + count += pf_putstr_fd("(nil)", 1); + else + { + count += pf_putstr_fd("0x", 1); + count += pf_putptr_fd(ptr_check, 1); + } + return (count); +} + +static int ft_format_c(char s, va_list parameters_info) +{ + int count; + + count = 0; + if (s == 'c') + count += pf_putchar_fd(va_arg(parameters_info, int), 1); + else if (s == 'i' || s == 'd') + count += pf_putnbr_fd(va_arg(parameters_info, long), 1); + else if (s == 'u') + count += pf_put_unsigned_nbr_fd(va_arg(parameters_info, \ + unsigned int), 1); + else if (s == 's') + count += pf_putstr_fd(va_arg(parameters_info, char *), 1); + else if (s == 'x') + count += pf_puthex_lower_fd(va_arg(parameters_info, long), 1); + else if (s == 'X') + count += pf_puthex_upper_fd(va_arg(parameters_info, long), 1); + else if (s == 'p') + count += ft_ptr_format(parameters_info); + else if (s == '%') + count += pf_putchar_fd('%', 1); + return (count); +} + +int ft_printf(const char *s, ...) +{ + va_list parameters_info; + unsigned int index; + int count; + + index = 0; + count = 0; + va_start(parameters_info, s); + while (s[index]) + { + if (s[index] == '%') + { + index++; + count += ft_format_c(s[index], parameters_info); + } + else if (s[index] != '%' && s[index]) + count += pf_putchar_fd(s[index], 1); + index++; + } + va_end(parameters_info); + return (count); +} diff --git a/lib/ft_printf/source/ft_put_unsigned_nbr_fd.c b/lib/ft_printf/source/ft_put_unsigned_nbr_fd.c new file mode 100755 index 0000000..c808b54 --- /dev/null +++ b/lib/ft_printf/source/ft_put_unsigned_nbr_fd.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_put_unsigned_nbr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hexplor +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/02 23:56:02 by hexplor #+# #+# */ +/* Updated: 2023/12/26 15:23:28 by hexplor ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "../header/ft_printf.h" + +int pf_put_unsigned_nbr_fd(unsigned int n, int fd) +{ + int count; + + count = 0; + if (n >= 10) + { + count += pf_put_unsigned_nbr_fd(n / 10, fd); + count += pf_put_unsigned_nbr_fd(n % 10, fd); + } + else + { + count += pf_putchar_fd(n + '0', fd); + } + return (count); +} diff --git a/lib/ft_printf/source/ft_putchar_fd.c b/lib/ft_printf/source/ft_putchar_fd.c new file mode 100755 index 0000000..f7aaf34 --- /dev/null +++ b/lib/ft_printf/source/ft_putchar_fd.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hexplor +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/23 14:08:31 by hexplor #+# #+# */ +/* Updated: 2023/12/26 15:23:41 by hexplor ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "../header/ft_printf.h" + +int pf_putchar_fd(char c, int fd) +{ + write(fd, &c, 1); + return (1); +} diff --git a/lib/ft_printf/source/ft_puthex_lower_fd.c b/lib/ft_printf/source/ft_puthex_lower_fd.c new file mode 100755 index 0000000..674c37d --- /dev/null +++ b/lib/ft_printf/source/ft_puthex_lower_fd.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_puthex_lower_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hexplor +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/02 15:23:28 by hexplor #+# #+# */ +/* Updated: 2023/12/26 15:24:01 by hexplor ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "../header/ft_printf.h" + +int pf_puthex_lower_fd(unsigned int n, int fd) +{ + int i; + char *hex; + + i = 0; + hex = "0123456789abcdef"; + if (n >= 16) + { + i += pf_puthex_lower_fd(n / 16, fd); + i += pf_puthex_lower_fd(n % 16, fd); + } + else + i += pf_putchar_fd(hex[n], fd); + return (i); +} diff --git a/lib/ft_printf/source/ft_puthex_upper_fd.c b/lib/ft_printf/source/ft_puthex_upper_fd.c new file mode 100755 index 0000000..d6a6d05 --- /dev/null +++ b/lib/ft_printf/source/ft_puthex_upper_fd.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_puthex_upper_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hexplor +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/02 15:27:12 by hexplor #+# #+# */ +/* Updated: 2023/12/26 15:24:21 by hexplor ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "../header/ft_printf.h" + +int pf_puthex_upper_fd(unsigned int n, int fd) +{ + int i; + char *hex; + + i = 0; + hex = "0123456789ABCDEF"; + if (n >= 16) + { + i += pf_puthex_upper_fd(n / 16, fd); + i += pf_puthex_upper_fd(n % 16, fd); + } + else + i += pf_putchar_fd(hex[n], fd); + return (i); +} diff --git a/lib/ft_printf/source/ft_putnbr_fd.c b/lib/ft_printf/source/ft_putnbr_fd.c new file mode 100755 index 0000000..6a51f3e --- /dev/null +++ b/lib/ft_printf/source/ft_putnbr_fd.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hexplor +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/01 23:41:52 by hexplor #+# #+# */ +/* Updated: 2023/12/26 15:24:42 by hexplor ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../header/ft_printf.h" + +int pf_putnbr_fd(long int n, int fd) +{ + int count; + + count = 0; + if (n < 0) + { + n = -n; + count += pf_putchar_fd('-', fd); + } + if ((n / 10) > 0) + count += pf_putnbr_fd(n / 10, fd); + pf_putchar_fd((n % 10) + 48, fd); + return (count + 1); +} diff --git a/lib/ft_printf/source/ft_putptr_fd.c b/lib/ft_printf/source/ft_putptr_fd.c new file mode 100755 index 0000000..09139bb --- /dev/null +++ b/lib/ft_printf/source/ft_putptr_fd.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putptr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hexplor +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/02 15:14:08 by hexplor #+# #+# */ +/* Updated: 2023/12/26 15:25:02 by hexplor ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "../header/ft_printf.h" + +int pf_putptr_fd(unsigned long long n, int fd) +{ + int len; + + len = 0; + if (n >= 16) + { + len += pf_putptr_fd(n / 16, fd); + len += pf_putptr_fd(n % 16, fd); + } + else + { + if (n < 10) + len += pf_putchar_fd(n + '0', fd); + else + len += pf_putchar_fd(n + 'a' - 10, fd); + } + return (len); +} diff --git a/lib/ft_printf/source/ft_putstr_fd.c b/lib/ft_printf/source/ft_putstr_fd.c new file mode 100755 index 0000000..12f4fb9 --- /dev/null +++ b/lib/ft_printf/source/ft_putstr_fd.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hexplor +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/23 14:24:20 by hexplor #+# #+# */ +/* Updated: 2023/12/26 15:25:19 by hexplor ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../header/ft_printf.h" + +int pf_putstr_fd(char *s, int fd) +{ + unsigned int i; + int count; + + if (!s) + { + pf_putstr_fd("(null)", fd); + return (6); + } + i = 0; + count = 0; + while (*(s + i)) + { + count += pf_putchar_fd(*(s + i), fd); + i++; + } + return (count); +} diff --git a/lib/libft/Makefile b/lib/libft/Makefile new file mode 100755 index 0000000..77d0b84 --- /dev/null +++ b/lib/libft/Makefile @@ -0,0 +1,31 @@ +SOURCES = ft_putnbr_fd.c ft_putendl_fd.c ft_putstr_fd.c ft_putchar_fd.c ft_striteri.c ft_strmapi.c ft_itoa.c ft_split.c ft_strtrim.c ft_strjoin.c ft_substr.c ft_strdup.c ft_calloc.c ft_atoi.c ft_strnstr.c ft_memcmp.c ft_memchr.c ft_strncmp.c ft_strrchr.c ft_strchr.c ft_tolower.c ft_toupper.c ft_strlcpy.c ft_strlen.c ft_isprint.c ft_strlcat.c ft_memmove.c ft_memcpy.c ft_bzero.c ft_isalpha.c ft_isdigit.c ft_memset.c ft_isalnum.c ft_isascii.c + +OBJECTS = $(SOURCES:.c=.o) + +EXTRA = a.out a.out.dSYM main.c + +NAME = libft.a + +COMPILER = cc + +FLAGS = -c -Wall -Werror -Wextra -I -g3 + +all : $(NAME) + +$(NAME) : $(OBJECTS) + @ar -rcs libft.a *.o + +$(OBJECTS) : $(SOURCES) + $(COMPILER) $(FLAGS) $(SOURCES) + +clean : + @rm -f $(OBJECTS) + +fclean : clean + @rm -rf $(NAME) $(EXTRA) + +re : + @make fclean + @make all + +.PHONY: fclean clean all re diff --git a/lib/libft/ft_atoi.c b/lib/libft/ft_atoi.c new file mode 100755 index 0000000..0c58134 --- /dev/null +++ b/lib/libft/ft_atoi.c @@ -0,0 +1,106 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hexplor +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/13 15:57:24 by hexplor #+# #+# */ +/* Updated: 2024/04/26 15:06:06 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int ft_isspace(char c) +{ + if (c == '\t' || c == '\n' || c == '\v' \ + || c == '\f' || c == '\r' || c == ' ') + return (1); + return (0); +} + +static int ft_fsign(char c) +{ + if (c == '-') + return (-1); + return (1); +} + +long ft_atoi(const char *text) +{ + long sign; + long number; + + number = 0; + sign = 1; + while (ft_isspace(*text) == 1) + text++; + if (*text == '-' || *text == '+') + { + sign = ft_fsign(*text); + text++; + } + while (ft_isdigit(*text) == 1) + { + number *= 10; + number += *text - '0'; + text++; + } + return (sign * number); +} +/* +#include +#include +int main(void) +{ + + char *texte1 = "12"; + char *texte2 = "+12"; + char *texte3 = "-12"; + + char *texte4 = " \t\n\v\f\r12"; + char *texte5 = " \t\n\v\f\r+12"; + char *texte6 = " \t\n\v\f\r-12"; + + char *texte7 = "--12"; + char *texte8 = "++12"; + char *texte9 = "abcd7"; + + char *texte10 = "12abs"; + char *texte11 = ""; + char *texte12 = "0"; + char *texte13 = "-2147483649"; + char *texte14 = "2147483649"; + + if (ft_atoi(texte1) != 12) + printf("1: NON\n"); + if(ft_atoi(texte2) != 12) + printf("2: NON\n"); + if(ft_atoi(texte3) != -12) + printf("3: NON\n"); + if(ft_atoi(texte4) != 12) + printf("4: NON\n"); + if(ft_atoi(texte5) != 12) + printf("5: NON\n"); + if(ft_atoi(texte6) != -12) + printf("6: NON\n"); + if(ft_atoi(texte7) != 0) + printf("7: NON\n"); + if(ft_atoi(texte8) != 0) + printf("8: NON\n"); + if(ft_atoi(texte9) != 0) + printf("9: NON\n"); + if(ft_atoi(texte10) != 12) + printf("10: NON\n"); + if(ft_atoi(texte11) != 0) + printf("11: NON\n"); + if(ft_atoi(texte12) != 0) + printf("12: NON\n"); + if(ft_atoi(texte13) != -2147483649L) + printf("13: NON\n"); + if(ft_atoi(texte14) != 2147483649L) + printf("14: NON\n"); + write(1, "\n", 1); + return (0); +}*/ diff --git a/lib/libft/ft_bzero.c b/lib/libft/ft_bzero.c new file mode 100755 index 0000000..a02eb4c --- /dev/null +++ b/lib/libft/ft_bzero.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_bzero.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 10:47:55 by yantoine #+# #+# */ +/* Updated: 2023/11/09 15:02:31 by hexplor ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include +#include +#include "libft.h" + +void ft_bzero(void *s, size_t n) +{ + if (n <= 0) + return ; + ft_memset(s, 0, n); +} + +/*int main() +{ + void mess[10] = "oui"; + + ft_bzero() + return (0); +}*/ diff --git a/lib/libft/ft_calloc.c b/lib/libft/ft_calloc.c new file mode 100755 index 0000000..152ee4c --- /dev/null +++ b/lib/libft/ft_calloc.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_calloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hexplor +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/14 15:24:33 by hexplor #+# #+# */ +/* Updated: 2023/11/30 08:53:17 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_calloc(size_t n, size_t size) +{ + char *tab; + size_t sizemax; + + sizemax = n * size; + if (size && sizemax / size < n) + return (NULL); + if (n == 0 || size == 0) + { + tab = malloc(1); + return (tab); + } + tab = malloc(n * size); + if (!tab) + return (NULL); + ft_bzero(tab, n * size); + return (tab); +} +/*#include +int main() +{ + void *p; + + p = ft_calloc(SIZE_MAX, SIZE_MAX); + free(p); + return (0); +}*/ diff --git a/lib/libft/ft_isalnum.c b/lib/libft/ft_isalnum.c new file mode 100755 index 0000000..cbaa49b --- /dev/null +++ b/lib/libft/ft_isalnum.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 10:17:01 by yantoine #+# #+# */ +/* Updated: 2023/11/08 08:05:51 by hexplor ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +int ft_isalnum(int c) +{ + if (ft_isalpha(c) == 1 || ft_isdigit(c) == 1) + return (1); + else + return (0); +} diff --git a/lib/libft/ft_isalpha.c b/lib/libft/ft_isalpha.c new file mode 100755 index 0000000..b7095b0 --- /dev/null +++ b/lib/libft/ft_isalpha.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 10:02:38 by yantoine #+# #+# */ +/* Updated: 2023/11/08 08:06:13 by hexplor ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +int ft_isalpha(int c) +{ + if ((c >= 65 && c <= 90) \ + || (c >= 97 && c <= 122)) + return (1); + else + return (0); +} diff --git a/lib/libft/ft_isascii.c b/lib/libft/ft_isascii.c new file mode 100755 index 0000000..b9521fa --- /dev/null +++ b/lib/libft/ft_isascii.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 10:17:41 by yantoine #+# #+# */ +/* Updated: 2023/11/08 08:06:30 by hexplor ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +int ft_isascii(int c) +{ + if (c >= 0 && c <= 127) + return (1); + else + return (0); +} diff --git a/lib/libft/ft_isdigit.c b/lib/libft/ft_isdigit.c new file mode 100755 index 0000000..b78fdd8 --- /dev/null +++ b/lib/libft/ft_isdigit.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 10:06:07 by yantoine #+# #+# */ +/* Updated: 2023/11/08 08:06:49 by hexplor ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +int ft_isdigit(int c) +{ + if ((c >= 48 && c <= 57)) + return (1); + else + return (0); +} diff --git a/lib/libft/ft_isprint.c b/lib/libft/ft_isprint.c new file mode 100755 index 0000000..9d79e61 --- /dev/null +++ b/lib/libft/ft_isprint.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 12:37:20 by yantoine #+# #+# */ +/* Updated: 2023/11/30 09:02:47 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +int ft_isprint(int c) +{ + if (c >= 32 && c <= 126) + return (1); + return (0); +} diff --git a/lib/libft/ft_itoa.c b/lib/libft/ft_itoa.c new file mode 100755 index 0000000..e325b44 --- /dev/null +++ b/lib/libft/ft_itoa.c @@ -0,0 +1,104 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hexplor +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/22 15:16:11 by hexplor #+# #+# */ +/* Updated: 2023/11/30 07:44:52 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int ft_n_digit(int n) +{ + long index; + + if (n == 0) + return (1); + index = 0; + while (n != 0) + { + n /= 10; + index++; + } + return (index); +} + +static int ft_pow(int n) +{ + long index; + + index = 1; + while (n != 0) + { + index *= 10; + n--; + } + return (index / 10); +} + +static void ft_set_number(char *tab) +{ + long index; + + index = 0; + while (index != 11) + { + tab[index] = index + '0'; + index++; + } + tab[index] = '\0'; +} + +static void ft_set_digit(long nbr_digit, long index, char *nbr, long n) +{ + char digit[15]; + long pow; + + if (n == 0) + { + *nbr = '0'; + *(nbr + 1) = 0; + return ; + } + ft_set_number(digit); + pow = ft_pow(nbr_digit); + while (nbr_digit != 0) + { + *(nbr + index++) = digit[(n / pow) % 10]; + nbr_digit--; + pow /= 10; + } + *(nbr + index++) = '\0'; +} + +char *ft_itoa(int n) +{ + char *nbr; + long index; + long n2; + long nbr_digit; + + n2 = (long) n; + nbr_digit = ft_n_digit(n); + index = 0; + if (n2 < 0) + { + nbr = ft_calloc((nbr_digit + 2), 1); + if (!nbr) + return (NULL); + *(nbr + index++) = '-'; + n2 *= -1; + } + else + { + nbr = ft_calloc((nbr_digit + 1), 1); + if (!nbr) + return (NULL); + } + ft_set_digit(nbr_digit, index, nbr, n2); + return (nbr); +} diff --git a/lib/libft/ft_memchr.c b/lib/libft/ft_memchr.c new file mode 100755 index 0000000..5e80b6a --- /dev/null +++ b/lib/libft/ft_memchr.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/11 09:03:40 by yantoine #+# #+# */ +/* Updated: 2023/11/13 16:24:52 by hexplor ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memchr(const void *s, int c, size_t n) +{ + unsigned char *p; + size_t index; + + p = (unsigned char *)s; + index = 0; + while (p[index] && index < n) + { + if (*(p + index) == (unsigned char) c) + return ((void *)(p + index)); + index++; + } + return (NULL); +} diff --git a/lib/libft/ft_memcmp.c b/lib/libft/ft_memcmp.c new file mode 100755 index 0000000..16a42a6 --- /dev/null +++ b/lib/libft/ft_memcmp.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/11 10:08:42 by yantoine #+# #+# */ +/* Updated: 2023/11/13 16:23:41 by hexplor ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_memcmp(const void *s1, const void *s2, size_t n) +{ + unsigned char *p1; + unsigned char *p2; + size_t index; + + index = 0; + p1 = (unsigned char *)s1; + p2 = (unsigned char *)s2; + while (index < n) + { + if (*(p1 + index) != *(p2 + index)) + return ((*(p1 + index) - *(p2 + index))); + index++; + } + return (0); +} + +/*int main() +{ + ft_memcmp("001270", "coucou", 7); + return (0); +}*/ diff --git a/lib/libft/ft_memcpy.c b/lib/libft/ft_memcpy.c new file mode 100755 index 0000000..030e42e --- /dev/null +++ b/lib/libft/ft_memcpy.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 11:03:13 by yantoine #+# #+# */ +/* Updated: 2023/11/09 12:15:15 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memcpy(void *dest, const void *src, size_t n) +{ + size_t index; + + index = 0; + while (index < n) + { + *(unsigned char *)(dest + index) = *(unsigned char *)(src + index); + index++; + } + return (dest); +} diff --git a/lib/libft/ft_memmove.c b/lib/libft/ft_memmove.c new file mode 100755 index 0000000..1e79e41 --- /dev/null +++ b/lib/libft/ft_memmove.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memmove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hexplor +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 15:13:56 by hexplor #+# #+# */ +/* Updated: 2023/11/09 12:45:23 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memmove(void *dest, const void *src, size_t n) +{ + size_t index; + int index2; + + if (dest > src) + { + index2 = n - 1; + while (index2 >= 0) + { + *(unsigned char *)(dest + index2) = \ + *(unsigned char *)(src + index2); + index2--; + } + } + else + { + index = 0; + while (index < n) + { + *(unsigned char *)(dest + index) = *(unsigned char *)(src + index); + index++; + } + } + return (dest); +} diff --git a/lib/libft/ft_memset.c b/lib/libft/ft_memset.c new file mode 100755 index 0000000..ee95790 --- /dev/null +++ b/lib/libft/ft_memset.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:33:18 by yantoine #+# #+# */ +/* Updated: 2023/11/09 14:55:20 by hexplor ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include +#include +#include "libft.h" + +void *ft_memset(void *s, int c, size_t n) +{ + size_t index; + + index = 0; + while (index < n) + { + *(unsigned char *)(s + index) = c; + index++; + } + return (s); +} diff --git a/lib/libft/ft_putchar_fd.c b/lib/libft/ft_putchar_fd.c new file mode 100755 index 0000000..9666580 --- /dev/null +++ b/lib/libft/ft_putchar_fd.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hexplor +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/23 14:08:31 by hexplor #+# #+# */ +/* Updated: 2023/11/23 15:04:00 by hexplor ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putchar_fd(char c, int fd) +{ + write(fd, &c, 1); +} diff --git a/lib/libft/ft_putendl_fd.c b/lib/libft/ft_putendl_fd.c new file mode 100755 index 0000000..4797c84 --- /dev/null +++ b/lib/libft/ft_putendl_fd.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hexplor +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/23 14:34:12 by hexplor #+# #+# */ +/* Updated: 2023/11/24 17:45:40 by hexplor ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putendl_fd(char *s, int fd) +{ + ft_putstr_fd(s, fd); + ft_putchar_fd('\n', fd); +} diff --git a/lib/libft/ft_putnbr_fd.c b/lib/libft/ft_putnbr_fd.c new file mode 100755 index 0000000..6e3cf3e --- /dev/null +++ b/lib/libft/ft_putnbr_fd.c @@ -0,0 +1,87 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hexplor +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/23 14:41:48 by hexplor #+# #+# */ +/* Updated: 2023/11/27 22:27:06 by hexplor ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int ft_n_digit(int n) +{ + long index; + + index = 0; + while (n != 0) + { + n /= 10; + index++; + } + return (index); +} + +static int ft_pow(int n) +{ + long index; + + index = 1; + while (n != 0) + { + index *= 10; + n--; + } + return (index / 10); +} + +static void ft_set_number(char *tab) +{ + long index; + + index = 0; + while (index != 11) + { + tab[index] = index + '0'; + index++; + } + tab[index] = '\0'; +} + +static void ft_set_digit(long nbr_digit, long n, int fd) +{ + char digit[15]; + long pow; + + if (n == 0) + { + ft_putchar_fd('0', fd); + return ; + } + ft_set_number(digit); + pow = ft_pow(nbr_digit); + while (nbr_digit != 0) + { + ft_putchar_fd(digit[(n / pow) % 10], fd); + nbr_digit--; + pow /= 10; + } +} + +void ft_putnbr_fd(int n, int fd) +{ + long n2; + long nbr_digit; + + n2 = (long) n; + nbr_digit = ft_n_digit(n); + if (n2 < 0) + { + ft_putchar_fd('-', fd); + n2 *= -1; + } + ft_set_digit(nbr_digit, n2, fd); +} diff --git a/lib/libft/ft_putstr_fd.c b/lib/libft/ft_putstr_fd.c new file mode 100755 index 0000000..e6cf7d7 --- /dev/null +++ b/lib/libft/ft_putstr_fd.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hexplor +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/23 14:24:20 by hexplor #+# #+# */ +/* Updated: 2023/11/23 14:32:00 by hexplor ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putstr_fd(char *s, int fd) +{ + unsigned int i; + + i = 0; + while (*(s + i)) + { + ft_putchar_fd(*(s + i), fd); + i++; + } +} diff --git a/lib/libft/ft_split.c b/lib/libft/ft_split.c new file mode 100755 index 0000000..7ea16c3 --- /dev/null +++ b/lib/libft/ft_split.c @@ -0,0 +1,108 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hexplor +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/16 17:06:15 by hexplor #+# #+# */ +/* Updated: 2023/11/29 13:19:56 by hexplor ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static size_t count_word(char *str, char c) +{ + int count; + + count = 0; + while (*str) + { + while (*str == c) + str++; + if (*str != c && *str) + count++; + while (*str && *str != c) + str++; + } + return (count); +} + +static size_t len_word(char *s, char c, size_t start) +{ + size_t i; + + i = 0; + while (s[start] && s[start] != c) + { + i++; + start++; + } + return (i); +} + +static void free_array(char **s1) +{ + size_t i; + + i = 0; + while (s1[i]) + { + free(s1[i]); + i++; + } + free(s1); +} + +static char **divisor(char *str1, char c, char **array, size_t len) +{ + size_t i; + size_t j; + + i = 0; + j = 0; + while (i < len) + { + while (str1[j] && str1[j] == c) + j++; + array[i] = ft_substr(str1, j, len_word(str1, c, j)); + if (!array[i]) + { + free_array(array); + return (NULL); + } + while (str1[j] && str1[j] != c) + j++; + i++; + } + array[i] = NULL; + return (array); +} + +char **ft_split(char const *s, char c) +{ + size_t words; + char **array; + + if (!s) + return (NULL); + words = count_word((char *)s, c); + array = (char **)malloc((words + 1) * (sizeof(char *))); + if (!array) + return (NULL); + array = divisor((char *)s, c, array, words); + return (array); +} +/* +int main() +{ + char **list; + + list = ft_split("--1-2--3---4----5-----42", '-'); + for (int i = 0; list[i] != NULL; ++i) + free(list[i]); + free(list); + return (0); +} +*/ diff --git a/lib/libft/ft_strchr.c b/lib/libft/ft_strchr.c new file mode 100755 index 0000000..f16e965 --- /dev/null +++ b/lib/libft/ft_strchr.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hexplor +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/10 12:07:02 by hexplor #+# #+# */ +/* Updated: 2023/11/29 15:22:10 by hexplor ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strchr(const char *s, int c) +{ + char *chaine; + + chaine = (char *)s; + while (*chaine) + { + if (*chaine == (unsigned char)c) + return (chaine); + chaine++; + } + if (c == '\0') + return (chaine); + return (NULL); +} diff --git a/lib/libft/ft_strdup.c b/lib/libft/ft_strdup.c new file mode 100755 index 0000000..562f9a7 --- /dev/null +++ b/lib/libft/ft_strdup.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hexplor +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/15 15:58:53 by hexplor #+# #+# */ +/* Updated: 2023/11/29 14:52:09 by hexplor ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strdup(const char *s1) +{ + char *s2; + int index; + + s2 = (char *)ft_calloc(ft_strlen(s1) + 1, 1); + if (s2 == NULL) + return (NULL); + index = 0; + while (*(s1 + index)) + { + *(s2 + index) = *(s1 + index); + index++; + } + *(s2 + index) = '\0'; + return (s2); +} + +/*int main() +{ + char *dest; + + dest = ft_strdup("BONJOUR"); + dest = ft_strdup(""); + dest = ft_strdup("\0"); + return (0); +}*/ diff --git a/lib/libft/ft_striteri.c b/lib/libft/ft_striteri.c new file mode 100755 index 0000000..68de181 --- /dev/null +++ b/lib/libft/ft_striteri.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striteri.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hexplor +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/23 12:50:39 by hexplor #+# #+# */ +/* Updated: 2023/11/23 13:54:15 by hexplor ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_striteri(char *s, void (*f)(unsigned int, char *)) +{ + unsigned int i; + + i = 0; + while (*s) + { + f(i, s); + i++; + s++; + } +} diff --git a/lib/libft/ft_strjoin.c b/lib/libft/ft_strjoin.c new file mode 100755 index 0000000..06d5c68 --- /dev/null +++ b/lib/libft/ft_strjoin.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hexplor +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/16 12:21:11 by hexplor #+# #+# */ +/* Updated: 2023/11/30 08:49:26 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strjoin(char const *s1, char const *s2) +{ + size_t len; + char *str; + + len = ft_strlen(s1); + len += ft_strlen(s2); + str = (char *)ft_calloc((len + 1), 1); + if (!str) + return (NULL); + ft_memmove(str, s1, ft_strlen(s1)); + ft_memmove((str + ft_strlen(s1)), s2, ft_strlen(s2)); + return (str); +} diff --git a/lib/libft/ft_strlcat.c b/lib/libft/ft_strlcat.c new file mode 100755 index 0000000..1e4b455 --- /dev/null +++ b/lib/libft/ft_strlcat.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hexplor +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 09:02:08 by hexplor #+# #+# */ +/* Updated: 2023/11/10 09:54:44 by hexplor ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +size_t ft_strlcat(char *dest, const char *src, size_t size) +{ + size_t i; + size_t k; + + i = 0; + k = 0; + while (dest[i] && i < size) + i++; + while (src[k] && (i + k + 1) < size) + { + dest[i + k] = src[k]; + k++; + } + if (i < size) + dest[i + k] = '\0'; + return (i + ft_strlen(src)); +} + +/*int main() +{ + char mess1[10] = "oui"; + char mess2[10] = "non"; + + ft_strlcat(mess1, mess2, 3); + return (0); +}*/ diff --git a/lib/libft/ft_strlcpy.c b/lib/libft/ft_strlcpy.c new file mode 100755 index 0000000..4cedd6b --- /dev/null +++ b/lib/libft/ft_strlcpy.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hexplor +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 17:27:30 by hexplor #+# #+# */ +/* Updated: 2023/11/09 20:22:10 by hexplor ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlcpy(char *dest, const char *src, size_t size) +{ + size_t c; + size_t i; + + c = 0; + while (src[c] != '\0') + c++; + i = 0; + if (size != 0) + { + while (src[i] != '\0' && i < (size - 1)) + { + dest[i] = src[i]; + i++; + } + dest[i] = '\0'; + } + return (c); +} + +/*int main() +{ + char dest[10] = "oui"; + char src[3] = "non"; + + ft_strlcpy(dest, src, 10); + return (0); +}*/ diff --git a/lib/libft/ft_strlen.c b/lib/libft/ft_strlen.c new file mode 100755 index 0000000..7ed76b2 --- /dev/null +++ b/lib/libft/ft_strlen.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 10:18:42 by yantoine #+# #+# */ +/* Updated: 2023/11/09 12:31:17 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +size_t ft_strlen(const char *c) +{ + int index; + + index = 0; + while (c[index] != 0) + index += 1; + return (index); +} diff --git a/lib/libft/ft_strmapi.c b/lib/libft/ft_strmapi.c new file mode 100755 index 0000000..83db4d1 --- /dev/null +++ b/lib/libft/ft_strmapi.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmapi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hexplor +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/23 11:52:03 by hexplor #+# #+# */ +/* Updated: 2023/11/30 09:04:01 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) +{ + unsigned int i; + char *str; + + i = 0; + str = ft_calloc((ft_strlen(s) + 1), 1); + if (!str) + return (0); + while (*(s + i)) + { + *(str + i) = f(i, *(s + i)); + i++; + } + *(str + i) = '\0'; + return (str); +} diff --git a/lib/libft/ft_strncmp.c b/lib/libft/ft_strncmp.c new file mode 100755 index 0000000..4c48c8e --- /dev/null +++ b/lib/libft/ft_strncmp.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/11 08:46:16 by yantoine #+# #+# */ +/* Updated: 2023/11/30 09:28:36 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strncmp(const char *s1, const char *s2, size_t n) +{ + size_t index; + + index = 0; + while (*(unsigned char *)(s1 + index) && \ + *(unsigned char *)(s2 + index) && index < n) + { + if (*(s1 + index) != *(s2 + index)) + return (*(s1 + index) - *(s2 + index)); + index++; + } + return (0); +} diff --git a/lib/libft/ft_strnstr.c b/lib/libft/ft_strnstr.c new file mode 100755 index 0000000..dc17b15 --- /dev/null +++ b/lib/libft/ft_strnstr.c @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/12 12:16:17 by yantoine #+# #+# */ +/* Updated: 2023/11/27 22:29:37 by hexplor ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +char *ft_strnstr(const char *big, const char *little, size_t len) +{ + size_t i; + size_t j; + char *b; + + b = (char *) big; + i = 0; + if (little[i] == '\0' || little == NULL) + return (b); + if (len == 0) + return (0); + while (b[i] != '\0' && i < len) + { + j = 0; + while (little[j] && (i + j) < len) + { + if (b[i + j] == little[j]) + j++; + else + break ; + } + if (little[j] == 0) + return (&b[i]); + i++; + } + return (0); +} +/*int main() +{ + ft_strnstr("aaabcabcd", "abcd", -1); + return (0); +}*/ diff --git a/lib/libft/ft_strrchr.c b/lib/libft/ft_strrchr.c new file mode 100755 index 0000000..dfc1be3 --- /dev/null +++ b/lib/libft/ft_strrchr.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/11 08:02:01 by yantoine #+# #+# */ +/* Updated: 2023/11/29 14:34:30 by hexplor ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +char *ft_strrchr(const char *s, int c) +{ + int len; + + len = 0; + while (*s) + { + len++; + s++; + } + if (c == '\0' && *s == '\0') + return ((char *)s); + while (len >= 0) + { + if (*s == (unsigned char)c) + return ((char *)s); + s--; + len--; + } + return (NULL); +} diff --git a/lib/libft/ft_strtrim.c b/lib/libft/ft_strtrim.c new file mode 100755 index 0000000..d23003e --- /dev/null +++ b/lib/libft/ft_strtrim.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtrim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hexplor +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/16 13:48:16 by hexplor #+# #+# */ +/* Updated: 2023/11/29 13:09:29 by hexplor ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strtrim(char const *s1, char const *set) +{ + int i; + int j; + + i = 0; + j = ft_strlen(s1) - 1; + while (s1[i] && ft_strchr(set, s1[i])) + i++; + while (s1[j] && ft_strchr(set, s1[j])) + j--; + return (ft_substr(s1, i, (j - i + 1))); +} diff --git a/lib/libft/ft_substr.c b/lib/libft/ft_substr.c new file mode 100755 index 0000000..89ada2c --- /dev/null +++ b/lib/libft/ft_substr.c @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_substr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hexplor +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/15 20:04:44 by hexplor #+# #+# */ +/* Updated: 2023/11/30 08:45:42 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_substr(char const *s, unsigned int start, size_t len) +{ + char *str; + size_t i; + + if (!s) + return (NULL); + i = 0; + if (len > ft_strlen(s) - start) + { + len = ft_strlen(s) - start; + } + if (start >= ft_strlen(s)) + return (ft_strdup("")); + str = (char *)malloc(((sizeof(char) * (len + 1)))); + if (!str) + return (NULL); + while (i < len && s[start] != '\0') + { + str[i++] = s[start++]; + } + str[i] = '\0'; + return (str); +} +/* +int main() +{ + char *text; + + text = ft_substr("tripouille", 100, 1); + free(text); + return (0); +}*/ diff --git a/lib/libft/ft_tolower.c b/lib/libft/ft_tolower.c new file mode 100755 index 0000000..604ca24 --- /dev/null +++ b/lib/libft/ft_tolower.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_tolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hexplor +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/10 10:09:46 by hexplor #+# #+# */ +/* Updated: 2023/11/10 10:13:40 by hexplor ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_tolower(int c) +{ + if (c >= 97 - 32 && c <= 122 - 32) + return (c + 32); + return (c); +} diff --git a/lib/libft/ft_toupper.c b/lib/libft/ft_toupper.c new file mode 100755 index 0000000..1f09844 --- /dev/null +++ b/lib/libft/ft_toupper.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_toupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hexplor +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/10 10:05:42 by hexplor #+# #+# */ +/* Updated: 2023/11/10 10:09:20 by hexplor ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_toupper(int c) +{ + if (c >= 97 && c <= 122) + return (c - 32); + return (c); +} diff --git a/lib/libft/libft.h b/lib/libft/libft.h new file mode 100755 index 0000000..763c396 --- /dev/null +++ b/lib/libft/libft.h @@ -0,0 +1,88 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 09:46:33 by yantoine #+# #+# */ +/* Updated: 2024/04/29 11:09:29 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_H +# define LIBFT_H + +# include +# include +# include +# include + +void *ft_calloc(size_t count, size_t size); + +void ft_putnbr_fd(int n, int fd); + +void ft_putendl_fd(char *s, int fd); + +void ft_striteri(char *s, void (*f)(unsigned int, char*)); + +void *ft_memchr(const void *s, int c, size_t n); + +void *ft_memmove(void *dest, const void *src, size_t n); + +void ft_putchar_fd(char c, int fd); + +void ft_putstr_fd(char *s, int fd); + +void *ft_memset(void *s, int c, size_t n); + +void ft_bzero(void *s, size_t n); + +void *ft_memcpy(void *dest, const void *src, size_t n); + +size_t ft_strlcat(char *dest, const char *src, size_t size); + +size_t ft_strlcpy(char *dest, const char *src, size_t size); + +size_t ft_strlen(const char *c); + +char **ft_split(char const *s, char c); + +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); + +char *ft_itoa(int n); + +char *ft_strtrim(char const *s1, char const *set); + +char *ft_strjoin(char const *s1, char const *s2); + +char *ft_substr(char const *s, unsigned int start, size_t len); + +char *ft_strdup(const char *s1); + +char *ft_strchr(const char *s, int c); + +char *ft_strrchr(const char *s, int c); + +char *ft_strnstr(const char *big, const char *little, size_t len); + +int ft_memcmp(const void *s1, const void *s2, size_t n); + +int ft_strncmp(const char *s1, const char *s2, size_t n); + +int ft_toupper(int c); + +int ft_tolower(int c); + +int ft_isprint(int c); + +int ft_isalpha(int c); + +int ft_isdigit(int c); + +int ft_isalnum(int c); + +int ft_isascii(int c); + +long ft_atoi(const char *str); +#endif diff --git a/src/.find_last.c.swp b/src/.find_last.c.swp new file mode 100644 index 0000000..5fe7810 Binary files /dev/null and b/src/.find_last.c.swp differ diff --git a/src/.find_max.c.swp b/src/.find_max.c.swp new file mode 100644 index 0000000..01d36b7 Binary files /dev/null and b/src/.find_max.c.swp differ diff --git a/src/.find_min.c.swp b/src/.find_min.c.swp new file mode 100644 index 0000000..2b3b36f Binary files /dev/null and b/src/.find_min.c.swp differ diff --git a/src/assign_param_list.c b/src/assign_param_list.c new file mode 100644 index 0000000..b723448 --- /dev/null +++ b/src/assign_param_list.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* assign_param_list.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/13 19:51:13 by yantoine #+# #+# */ +/* Updated: 2024/03/20 13:26:37 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "../includes/push_swap.h" + +void assign_param_list(t_Liste *liste) +{ + if (!liste->premier) + return ; + list_lenght(liste); + list_index(liste); + find_min(liste); + find_max(liste); + find_last(liste); + find_moyenne(liste); + set_precedant(liste); +} diff --git a/src/big_sort.c b/src/big_sort.c new file mode 100644 index 0000000..64877ac --- /dev/null +++ b/src/big_sort.c @@ -0,0 +1,91 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* big_sort.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/15 21:04:05 by yantoine #+# #+# */ +/* Updated: 2024/03/21 18:11:09 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ +/* +#include "../includes/push_swap.h" + +static int wich_algo(t_Liste *stack_a, t_Liste *stack_b) +{ + if (stack_a->premier->number > stack_b->max) + return (1); + else if (stack_a->premier->number < stack_b->min) + return (2); + else + return (3); +} + +static void exec_algo(t_Liste *stack_a, t_Liste *stack_b, int algo) +{ + int i; + + i = 0; + if (algo == 1) + push(stack_a, stack_b, 'b'); + else if (algo == 2) + { + push(stack_a, stack_b, 'b'); + rotate(stack_b, 'b'); + } + else + { + if (stack_a->premier->number >= stack_b->moyenne) + { + while (stack_a->premier->number < stack_b->premier->number) + { + rotate(stack_b, 'b'); + i++; + } + while (stack_a->premier->number >= stack_b->premier->number \ + && stack_a->premier->number <= stack_b->dernier->number) + push(stack_a, stack_b, 'b'); + while (i > 0) + { + if (stack_a->premier->number > stack_b->premier->number \ + && stack_a->premier->number < stack_b->dernier->number) + push(stack_a, stack_b, 'b'); + reverse_rotate(stack_b, 'b'); + i--; + } + } + else + { + while (stack_a->premier->number < stack_b->premier->number \ + || stack_a->premier->number > stack_b->dernier->number) + { + reverse_rotate(stack_b, 'b'); + i++; + } + while (stack_a->premier->number >= stack_b->premier->number \ + && stack_a->premier->number <= stack_b->dernier->number) + push(stack_a, stack_b, 'b'); + while (i >= 0) + { + if (stack_a->premier->number < stack_b->premier->number \ + && stack_a->premier->number > stack_b->dernier->number) + push(stack_a, stack_b, 'b'); + rotate(stack_b, 'b'); + i--; + } + } + } +} + +void big_sort(t_Liste *stack_a, t_Liste *stack_b) +{ + int size; + + size = stack_a->length; + while (stack_a->length > 1) + exec_algo(stack_a, stack_b, wich_algo(stack_a, stack_b)); + while (stack_a->length != size) + push(stack_a, stack_b, 'a'); +} +*/ diff --git a/src/condition.c b/src/condition.c new file mode 100644 index 0000000..32217d5 --- /dev/null +++ b/src/condition.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* condition.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/21 18:03:40 by yantoine #+# #+# */ +/* Updated: 2024/03/21 23:43:04 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "../includes/push_swap.h" + +int condition(t_Element *actuel_a, t_Element *actuel_b, t_Liste *stack_b) +{ + if (actuel_a->number < stack_b->min) + { + if (actuel_b->number == stack_b->max) + { + return (1); + } + } + else if (actuel_a->number > stack_b->max) + { + if (actuel_b->number == stack_b->max) + { + return (1); + } + } + else if (actuel_a->number > actuel_b->number \ + && actuel_a->number < actuel_b->precedant_boucle->number) + { + return (1); + } + return (0); +} diff --git a/src/find_last.c b/src/find_last.c new file mode 100644 index 0000000..8a52292 --- /dev/null +++ b/src/find_last.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* find_last.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/18 20:15:26 by yantoine #+# #+# */ +/* Updated: 2024/03/18 20:16:57 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "../includes/push_swap.h" + +void find_last(t_Liste *liste) +{ + t_Element *tmp; + + if (!liste->premier) + return ; + tmp = liste->premier; + while (tmp->suivant) + tmp = tmp->suivant; + liste->dernier = tmp; +} diff --git a/src/find_lower_rank.c b/src/find_lower_rank.c new file mode 100644 index 0000000..ff869a9 --- /dev/null +++ b/src/find_lower_rank.c @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* find_lower_rank.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/21 18:07:46 by yantoine #+# #+# */ +/* Updated: 2024/03/21 23:43:16 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "../includes/push_swap.h" + +static int lower_instruction(t_Element *element) +{ + int lower; + + lower = 0; + lower = element->instruction; + element->wich_instr = 0; + if (lower > element->instruction_ra) + { + lower = element->instruction_ra; + element->wich_instr = 1; + } + if (lower > element->instruction_rb) + { + lower = element->instruction_rb; + element->wich_instr = 2; + } + if (lower > element->instruction_rab) + { + lower = element->instruction_rab; + element->wich_instr = 3; + } + return (lower); +} + +void find_lower_rank(t_Liste *stack) +{ + t_Element *actuel; + + actuel = stack->premier; + stack->lower_rank = actuel; + while (actuel) + { + if (lower_instruction(actuel) < lower_instruction(stack->lower_rank)) + stack->lower_rank = actuel; + actuel = actuel->suivant; + } +} diff --git a/src/find_max.c b/src/find_max.c new file mode 100644 index 0000000..2c57312 --- /dev/null +++ b/src/find_max.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* find_max.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/18 20:13:28 by yantoine #+# #+# */ +/* Updated: 2024/03/18 20:14:44 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "../includes/push_swap.h" + +void find_max(t_Liste *liste) +{ + t_Element *tmp; + int max; + + if (!liste->premier) + return ; + tmp = liste->premier; + max = tmp->number; + while (tmp) + { + if (tmp->number > max) + max = tmp->number; + tmp = tmp->suivant; + } + liste->max = max; +} diff --git a/src/find_min.c b/src/find_min.c new file mode 100644 index 0000000..1efa3c1 --- /dev/null +++ b/src/find_min.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* find_min.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/13 18:10:13 by yantoine #+# #+# */ +/* Updated: 2024/03/13 20:18:53 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "../includes/push_swap.h" + +void find_min(t_Liste *liste) +{ + t_Element *tmp; + int min; + + if (!liste->premier) + return ; + tmp = liste->premier; + min = tmp->number; + while (tmp) + { + if (tmp->number < min) + min = tmp->number; + tmp = tmp->suivant; + } + liste->min = min; +} diff --git a/src/find_moyenne.c b/src/find_moyenne.c new file mode 100644 index 0000000..b793ca7 --- /dev/null +++ b/src/find_moyenne.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* find_moyenne.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/18 01:14:19 by yantoine #+# #+# */ +/* Updated: 2024/03/18 01:47:52 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "../includes/push_swap.h" + +void find_moyenne(t_Liste *liste) +{ + t_Element *actuel; + int moyenne; + int nbr; + + moyenne = 0; + nbr = 0; + actuel = liste->premier; + while (actuel) + { + moyenne += actuel->number; + actuel = actuel->suivant; + nbr++; + } + liste->moyenne = moyenne / nbr; +} diff --git a/src/find_quicker.c b/src/find_quicker.c new file mode 100644 index 0000000..32a9eac --- /dev/null +++ b/src/find_quicker.c @@ -0,0 +1,134 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* find_quicker.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/19 12:31:16 by yantoine #+# #+# */ +/* Updated: 2024/04/29 11:10:44 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "../includes/push_swap.h" +//faire pour moyenne haute et moyenne basse + +static void boucle(t_Liste *stack_a, t_Liste *stack_b) +{ + int i; + int j; + t_Element *actuel_a; + t_Element *actuel_b; + + i = 0; + actuel_a = stack_a->premier; + while (actuel_a) + { + j = 0; + actuel_b = stack_b->premier; + while (actuel_b) + { + if (condition(actuel_a, actuel_b, stack_b)) + { + actuel_a->instruction = j + i ; + actuel_a->ptr_num = actuel_b->number; + } + actuel_b = actuel_b->suivant; + j++; + } + actuel_a = actuel_a->suivant; + i++; + } +} + +static void boucle_inverse_a(t_Liste *stack_a, t_Liste *stack_b) +{ + int i; + int j; + t_Element *actuel_a; + t_Element *actuel_b; + + i = 0; + actuel_a = stack_a->dernier; + while (actuel_a) + { + j = 0; + actuel_b = stack_b->premier; + while (actuel_b) + { + if (condition(actuel_a, actuel_b, stack_b)) + { + actuel_a->instruction_ra = j + i; + actuel_a->ptr_num = actuel_b->number; + } + actuel_b = actuel_b->suivant; + j++; + } + actuel_a = actuel_a->precedant; + i++; + } +} + +static void boucle_inverse_b(t_Liste *stack_a, t_Liste *stack_b) +{ + int i; + int j; + t_Element *actuel_a; + t_Element *actuel_b; + + i = 0; + actuel_a = stack_a->premier; + while (actuel_a) + { + j = 0; + actuel_b = stack_b->dernier; + while (actuel_b) + { + if (condition(actuel_a, actuel_b, stack_b)) + { + actuel_a->instruction_rb = j + i; + actuel_a->ptr_num = actuel_b->number; + } + actuel_b = actuel_b->precedant; + j++; + } + actuel_a = actuel_a->suivant; + i++; + } +} + +static void boucle_inverse_ab(t_Liste *stack_a, t_Liste *stack_b) +{ + int i; + int j; + t_Element *actuel_a; + t_Element *actuel_b; + + i = 0; + actuel_a = stack_a->dernier; + while (actuel_a) + { + j = 0; + actuel_b = stack_b->dernier; + while (actuel_b) + { + if (condition(actuel_a, actuel_b, stack_b)) + { + actuel_a->instruction_rab = j + i; + actuel_a->ptr_num = actuel_b->number; + } + actuel_b = actuel_b->precedant; + j++; + } + actuel_a = actuel_a->precedant; + i++; + } +} + +void find_quicker(t_Liste *stack_a, t_Liste *stack_b) +{ + boucle(stack_a, stack_b); + boucle_inverse_a(stack_a, stack_b); + boucle_inverse_b(stack_a, stack_b); + boucle_inverse_ab(stack_a, stack_b); + find_lower_rank(stack_a); +} diff --git a/src/free_stack.c b/src/free_stack.c new file mode 100644 index 0000000..ae8afc8 --- /dev/null +++ b/src/free_stack.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* free_stack.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/13 18:26:53 by yantoine #+# #+# */ +/* Updated: 2024/03/14 22:58:30 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "../includes/push_swap.h" + +void free_stack(t_Liste *stack_a, t_Liste *stack_b) +{ + t_Element *tmp; + + if (stack_a) + { + while (stack_a->premier) + { + tmp = stack_a->premier; + stack_a->premier = stack_a->premier->suivant; + free(tmp); + } + } + if (stack_b) + { + while (stack_b->premier) + { + tmp = stack_b->premier; + stack_b->premier = stack_b->premier->suivant; + free(tmp); + } + } + if (stack_a) + free(stack_a); + if (stack_b) + free(stack_b); +} diff --git a/src/ft_list_find.c b/src/ft_list_find.c new file mode 100644 index 0000000..31db9f6 --- /dev/null +++ b/src/ft_list_find.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list_find.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/13 00:28:07 by yantoine #+# #+# */ +/* Updated: 2024/03/13 00:30:59 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "../includes/push_swap.h" + +t_Element *ft_list_find(t_Liste *liste, int nb) +{ + t_Element *element; + + if (!liste) + return (NULL); + element = liste->premier; + while (element) + { + if (element->number == nb) + return (element); + element = element->suivant; + } + return (NULL); +} diff --git a/src/ft_list_push_back.c b/src/ft_list_push_back.c new file mode 100644 index 0000000..7d35387 --- /dev/null +++ b/src/ft_list_push_back.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list_push_back.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/13 16:46:06 by yantoine #+# #+# */ +/* Updated: 2024/03/21 22:49:32 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "../includes/push_swap.h" + +void ft_list_push_back(t_Liste *liste, int valeur) +{ + t_Element *element; + t_Element *current; + + element = malloc(sizeof(*element)); + if (!element) + return ; + element->number = valeur; + element->ptr_num = 0; + element->instruction = 0; + element->instruction_ra = 0; + element->instruction_rb = 0; + element->instruction_rab = 0; + element->suivant = NULL; + if (liste->premier == NULL) + liste->premier = element; + else + { + current = liste->premier; + while (current->suivant) + current = current->suivant; + current->suivant = element; + } +} diff --git a/src/is_sorted.c b/src/is_sorted.c new file mode 100644 index 0000000..3b0b88e --- /dev/null +++ b/src/is_sorted.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* is_sorted.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/12 21:19:44 by yantoine #+# #+# */ +/* Updated: 2024/04/26 16:42:18 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "../includes/push_swap.h" + +int is_sorted(t_Liste *stack_a) +{ + t_Element *current; + + if (!stack_a) + return (0); + if (stack_a->length == 0) + return (1); + current = stack_a->premier; + while (current->suivant) + { + if (current->number > current->suivant->number) + return (0); + current = current->suivant; + } + return (1); +} diff --git a/src/list_index.c b/src/list_index.c new file mode 100644 index 0000000..cd6e0d4 --- /dev/null +++ b/src/list_index.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_index.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/13 20:41:18 by yantoine #+# #+# */ +/* Updated: 2024/03/13 20:41:42 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "../includes/push_swap.h" + +void list_index(t_Liste *liste) +{ + t_Element *tmp; + int i; + + i = 0; + tmp = liste->premier; + while (tmp) + { + tmp->index = i; + tmp = tmp->suivant; + i++; + } +} diff --git a/src/list_lenght.c b/src/list_lenght.c new file mode 100644 index 0000000..548c69f --- /dev/null +++ b/src/list_lenght.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_lenght.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/12 23:57:34 by yantoine #+# #+# */ +/* Updated: 2024/03/13 20:20:04 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "../includes/push_swap.h" + +void list_lenght(t_Liste *liste) +{ + t_Element *current; + int i; + + if (!liste || !liste->premier) + return ; + i = 0; + current = liste->premier; + while (current) + { + current = current->suivant; + i++; + } + liste->length = i; +} diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..c3473e5 --- /dev/null +++ b/src/main.c @@ -0,0 +1,82 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/12 14:15:12 by yantoine #+# #+# */ +/* Updated: 2024/04/26 15:14:46 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "../includes/push_swap.h" + +static int manage_arg(t_Liste *stack_a, int argc, char **argv) +{ + if (argc == 2) + { + if (!parse_arg(stack_a, argv[1])) + return (0); + } + else + { + if (!parse_args(stack_a, argc, argv)) + return (0); + } + list_lenght(stack_a); + return (1); +} + +static t_Liste *init_stacks(t_Liste *stack_a) +{ + stack_a = malloc(sizeof(t_Liste)); + if (!stack_a) + { + write(2, "Error\n", 6); + return (NULL); + } + stack_a->premier = NULL; + stack_a->length = 0; + stack_a->moyenne = 0; + stack_a->lower_rank = NULL; + return (stack_a); +} + +static void anti_25_ligne(t_Liste *stack_a, t_Liste *stack_b) +{ + if (!is_sorted(stack_a)) + { + if (stack_a->length == 2) + swap(stack_a, 'a'); + else if (stack_a->length == 3) + sort_3(stack_a); + else if (stack_a->length == 4) + sort_4(stack_a, stack_b); + else if (stack_a->length == 5) + sort_5(stack_a, stack_b); + else + turc(stack_a, stack_b); + } +} + +int main(int argc, char **argv) +{ + t_Liste *stack_a; + t_Liste *stack_b; + + stack_a = NULL; + stack_b = NULL; + stack_a = init_stacks(stack_a); + stack_b = init_stacks(stack_b); + if (argc < 2 || !manage_arg(stack_a, argc, argv)) + { + free_stack(stack_a, stack_b); + if (argc != 1) + write(2, "Error\n", 6); + return (0); + } + assign_param_list(stack_a); + anti_25_ligne(stack_a, stack_b); + free_stack(stack_a, stack_b); + return (0); +} diff --git a/src/normal_trie.c b/src/normal_trie.c new file mode 100644 index 0000000..95965f2 --- /dev/null +++ b/src/normal_trie.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* normal_trie.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/21 19:47:09 by yantoine #+# #+# */ +/* Updated: 2024/04/26 16:47:10 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "../includes/push_swap.h" + +void normal_trie(t_Liste *stack_a, t_Liste *stack_b) +{ + if (!stack_a || !stack_b) + return ; + while (stack_a->premier != stack_a->lower_rank \ + && stack_b->premier->number != stack_a->lower_rank->ptr_num) + rotate_both(stack_a, stack_b); + while (stack_a->premier != stack_a->lower_rank) + rotate(stack_a, 'a'); + while (stack_b->premier->number != stack_a->lower_rank->ptr_num) + rotate(stack_b, 'b'); + push(stack_a, stack_b, 'b'); +} + +void reverse_a_trie(t_Liste *stack_a, t_Liste *stack_b) +{ + while (stack_a->premier != stack_a->lower_rank) + reverse_rotate(stack_a, 'a'); + while (stack_b->premier->number != stack_a->lower_rank->ptr_num) + rotate(stack_b, 'b'); + push(stack_a, stack_b, 'b'); +} + +void reverse_b_trie(t_Liste *stack_a, t_Liste *stack_b) +{ + while (stack_a->premier != stack_a->lower_rank) + rotate(stack_a, 'a'); + while (stack_b->premier->number != stack_a->lower_rank->ptr_num) + reverse_rotate(stack_b, 'b'); + push(stack_a, stack_b, 'b'); +} + +void reverse_ab_trie(t_Liste *stack_a, t_Liste *stack_b) +{ + while (stack_a->premier != stack_a->lower_rank \ + && stack_b->premier->number != stack_a->lower_rank->ptr_num) + reverse_rotate_both(stack_a, stack_b); + while (stack_a->premier != stack_a->lower_rank) + reverse_rotate(stack_a, 'a'); + while (stack_b->premier->number != stack_a->lower_rank->ptr_num) + reverse_rotate(stack_b, 'b'); + push(stack_a, stack_b, 'b'); +} diff --git a/src/parse_arg.c b/src/parse_arg.c new file mode 100644 index 0000000..242b75f --- /dev/null +++ b/src/parse_arg.c @@ -0,0 +1,82 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_arg.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/12 14:31:18 by yantoine #+# #+# */ +/* Updated: 2024/04/29 11:11:48 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "../includes/push_swap.h" + +static void ft_free_split(char **split) +{ + int i; + + i = 0; + if (!split) + return ; + while (split[i]) + { + free(split[i]); + i++; + } + free(split); +} + +static int verif_string(char *string) +{ + int i; + + i = 0; + while (string[i]) + { + if (!ft_isdigit(string[i]) && \ + string[i] != '-' && string[i] != '+') + return (0); + i++; + } + return (1); +} + +static int ft_atoi_custom(char *string, t_Liste *stack) +{ + long int nb; + + if (!string || stack == NULL) + return (0); + nb = ft_atoi(string); + if (nb > INT_MAX || nb < INT_MIN) + return (0); + if (ft_list_find(stack, nb)) + return (0); + ft_list_push_back(stack, nb); + return (1); +} + +int parse_arg(t_Liste *stack_a, char *arg) +{ + int i; + char **split; + + i = 0; + split = ft_split(arg, ' '); + while (split[i]) + { + if (!verif_string(split[i])) + { + ft_free_split(split); + return (0); + } + if (!ft_atoi_custom(split[i], stack_a)) + { + ft_free_split(split); + return (0); + } + i++; + } + ft_free_split(split); + return (1); +} diff --git a/src/parse_args.c b/src/parse_args.c new file mode 100644 index 0000000..ad228e5 --- /dev/null +++ b/src/parse_args.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_args.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/12 14:36:07 by yantoine #+# #+# */ +/* Updated: 2024/03/14 22:09:01 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "../includes/push_swap.h" + +int parse_args(t_Liste *stack_a, int argc, char **argv) +{ + int i; + + i = 1; + while (i < argc) + { + if (!parse_arg(stack_a, argv[i])) + return (0); + i++; + } + return (1); +} diff --git a/src/push.c b/src/push.c new file mode 100644 index 0000000..bbee494 --- /dev/null +++ b/src/push.c @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* push.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/13 18:01:44 by yantoine #+# #+# */ +/* Updated: 2024/04/29 11:12:14 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "../includes/push_swap.h" + +static void anti_25(t_Liste *stack_a, t_Liste *stack_b) +{ + assign_param_list(stack_a); + assign_param_list(stack_b); +} + +void push(t_Liste *stack_a, t_Liste *stack_b, char c) +{ + t_Element *tmp; + + if (c == 'a') + { + tmp = stack_b->premier; + if (tmp) + { + stack_b->premier = stack_b->premier->suivant; + tmp->suivant = stack_a->premier; + stack_a->premier = tmp; + } + write(1, "pa\n", 3); + } + else if (c == 'b') + { + tmp = stack_a->premier; + if (tmp) + { + stack_a->premier = stack_a->premier->suivant; + tmp->suivant = stack_b->premier; + stack_b->premier = tmp; + } + write(1, "pb\n", 3); + } + anti_25(stack_a, stack_b); +} diff --git a/src/push_two_number.c b/src/push_two_number.c new file mode 100644 index 0000000..26a3c6b --- /dev/null +++ b/src/push_two_number.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* push_two_number.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/19 12:28:15 by yantoine #+# #+# */ +/* Updated: 2024/03/19 12:34:04 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "../includes/push_swap.h" + +void push_two_number(t_Liste *stack_a, t_Liste *stack_b) +{ + push(stack_a, stack_b, 'b'); + push(stack_a, stack_b, 'b'); + if (is_sorted(stack_b)) + swap(stack_b, 'b'); +} diff --git a/src/reverse_rotate.c b/src/reverse_rotate.c new file mode 100644 index 0000000..2242c9d --- /dev/null +++ b/src/reverse_rotate.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* reverse_rotate.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/13 17:43:31 by yantoine #+# #+# */ +/* Updated: 2024/03/13 20:05:48 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "../includes/push_swap.h" + +void reverse_rotate(t_Liste *liste, char c) +{ + t_Element *tmp; + t_Element *tmp2; + + if (liste->length < 2) + return ; + tmp = liste->premier; + while (tmp->suivant->suivant) + tmp = tmp->suivant; + tmp2 = tmp->suivant; + tmp->suivant = NULL; + tmp2->suivant = liste->premier; + liste->premier = tmp2; + if (c == 'a') + write(1, "rra\n", 4); + else if (c == 'b') + write(1, "rrb\n", 4); + assign_param_list(liste); +} + +void reverse_rotate_both(t_Liste *stack_a, t_Liste *stack_b) +{ + reverse_rotate(stack_a, 0); + reverse_rotate(stack_b, 0); + write(1, "rrr\n", 4); +} diff --git a/src/rotate.c b/src/rotate.c new file mode 100644 index 0000000..ca94a71 --- /dev/null +++ b/src/rotate.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rotate.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/13 17:16:48 by yantoine #+# #+# */ +/* Updated: 2024/03/13 20:06:58 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "../includes/push_swap.h" + +void rotate(t_Liste *liste, char c) +{ + t_Element *tmp; + + if (liste->length < 2) + return ; + tmp = liste->premier; + liste->premier = liste->premier->suivant; + liste->dernier->suivant = tmp; + liste->dernier = tmp; + tmp->suivant = NULL; + if (c == 'a') + write(1, "ra\n", 3); + else if (c == 'b') + write(1, "rb\n", 3); + assign_param_list(liste); +} + +void rotate_both(t_Liste *stack_a, t_Liste *stack_b) +{ + rotate(stack_a, 0); + rotate(stack_b, 0); + write(1, "rr\n", 3); +} diff --git a/src/set_precedant.c b/src/set_precedant.c new file mode 100644 index 0000000..473a01a --- /dev/null +++ b/src/set_precedant.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* set_precedant.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/20 13:22:29 by yantoine #+# #+# */ +/* Updated: 2024/03/21 23:48:37 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "../includes/push_swap.h" + +void set_precedant(t_Liste *liste) +{ + t_Element *actuel; + t_Element *precedant; + + actuel = liste->premier; + while (actuel) + { + precedant = actuel; + actuel = actuel->suivant; + if (!actuel) + break ; + actuel->precedant = precedant; + actuel->precedant_boucle = precedant; + if (actuel->suivant == NULL) + break ; + } + liste->premier->precedant = NULL; + liste->premier->precedant_boucle = liste->dernier; +} diff --git a/src/sort_3.c b/src/sort_3.c new file mode 100644 index 0000000..3580e51 --- /dev/null +++ b/src/sort_3.c @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sort_3.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/13 00:49:31 by yantoine #+# #+# */ +/* Updated: 2024/03/14 23:18:56 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "../includes/push_swap.h" + +static void anti_25(t_Liste *stack_a, int min, int max) +{ + if (stack_a->premier->suivant->number == max) + { + if (stack_a->premier->suivant->suivant->number == min) + reverse_rotate(stack_a, 'a'); + else + { + reverse_rotate(stack_a, 'a'); + swap(stack_a, 'a'); + } + } + else + { + if (stack_a->premier->suivant->suivant->number == max) + swap(stack_a, 'a'); + else + { + reverse_rotate(stack_a, 'a'); + swap(stack_a, 'a'); + } + } +} + +void sort_3(t_Liste *stack_a) +{ + int min; + int max; + + if (is_sorted(stack_a)) + return ; + min = stack_a->min; + max = stack_a->max; + if (stack_a->premier->number == max) + { + if (stack_a->premier->suivant->number == min) + rotate(stack_a, 'a'); + else + { + swap(stack_a, 'a'); + reverse_rotate(stack_a, 'a'); + } + } + else + anti_25(stack_a, min, max); +} diff --git a/src/sort_4.c b/src/sort_4.c new file mode 100644 index 0000000..0a9ae0b --- /dev/null +++ b/src/sort_4.c @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sort_4.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/13 20:34:09 by yantoine #+# #+# */ +/* Updated: 2024/03/14 23:22:34 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "../includes/push_swap.h" + +void suite(t_Liste *stack_a, t_Liste *stack_b) +{ + sort_3(stack_a); + push(stack_a, stack_b, 'a'); + if (!is_sorted(stack_a)) + rotate(stack_a, 'a'); +} + +void sort_4(t_Liste *stack_a, t_Liste *stack_b) +{ + int min; + int max; + int i; + + if (is_sorted(stack_a)) + return ; + min = stack_a->min; + max = stack_a->max; + i = 0; + while (i < 4) + { + if (stack_a->premier->number == min \ + || stack_a->premier->number == max) + { + push(stack_a, stack_b, 'b'); + break ; + } + else + rotate(stack_a, 'a'); + i++; + } + suite(stack_a, stack_b); +} diff --git a/src/sort_5.c b/src/sort_5.c new file mode 100644 index 0000000..9d35ee0 --- /dev/null +++ b/src/sort_5.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sort_5.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/13 16:58:35 by yantoine #+# #+# */ +/* Updated: 2024/03/14 22:57:44 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "../includes/push_swap.h" + +void sort_5(t_Liste *stack_a, t_Liste *stack_b) +{ + int min; + int max; + int i; + + if (is_sorted(stack_a)) + return ; + min = stack_a->min; + max = stack_a->max; + i = 0; + while (i < 5) + { + if (stack_a->premier->number == min \ + || stack_a->premier->number == max) + { + push(stack_a, stack_b, 'b'); + break ; + } + else + rotate(stack_a, 'a'); + i++; + } + sort_4(stack_a, stack_b); + push(stack_a, stack_b, 'a'); + if (!is_sorted(stack_a)) + rotate(stack_a, 'a'); +} diff --git a/src/swap.c b/src/swap.c new file mode 100644 index 0000000..a9c1f21 --- /dev/null +++ b/src/swap.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* swap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/13 17:34:45 by yantoine #+# #+# */ +/* Updated: 2024/03/14 22:27:09 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "../includes/push_swap.h" + +void swap(t_Liste *liste, char c) +{ + t_Element *tmp; + + if (liste->length < 2) + return ; + tmp = liste->premier; + liste->premier = liste->premier->suivant; + tmp->suivant = liste->premier->suivant; + liste->premier->suivant = tmp; + if (c == 'a') + write(1, "sa\n", 3); + else if (c == 'b') + write(1, "sb\n", 3); + assign_param_list(liste); +} + +void swap_both(t_Liste *stack_a, t_Liste *stack_b) +{ + swap(stack_a, 0); + swap(stack_b, 0); + write(1, "ss\n", 3); +} diff --git a/src/turc.c b/src/turc.c new file mode 100644 index 0000000..4db5376 --- /dev/null +++ b/src/turc.c @@ -0,0 +1,100 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* turc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/19 12:14:47 by yantoine #+# #+# */ +/* Updated: 2024/03/23 23:05:29 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "../includes/push_swap.h" +/* +void print_stacks(t_Liste *stack_a, t_Liste *stack_b) +{ + t_Element *actuel_a; + t_Element *actuel_b; + + actuel_a = stack_a->premier; + actuel_b = stack_b->premier; + printf("\n"); + printf("\n"); + while (actuel_a || actuel_b) + { + if (actuel_a) + { + printf("instruction %d__", actuel_a->instruction); + printf("%d__", actuel_a->instruction_ra); + printf("%d__", actuel_a->instruction_rb); + printf("%d__|", actuel_a->instruction_rab); + printf("%ld ", actuel_a->number); + actuel_a = actuel_a->suivant; + } + else + printf(" "); + if (actuel_b) + { + printf("%ld\n", actuel_b->number); + actuel_b = actuel_b->suivant; + } + else + printf("\n"); + } + printf("\n"); +} +*/ +static void wich_algo(t_Liste *stack_a, t_Liste *stack_b) +{ + if (stack_a->lower_rank->wich_instr == 0) + { + normal_trie(stack_a, stack_b); + } + else if (stack_a->lower_rank->wich_instr == 1) + { + reverse_a_trie(stack_a, stack_b); + } + else if (stack_a->lower_rank->wich_instr == 2) + { + reverse_b_trie(stack_a, stack_b); + } + else + { + reverse_ab_trie(stack_a, stack_b); + } +} + +static void anti_25_ligne(t_Liste *stack_a, t_Liste *stack_b, long length) +{ + while (stack_a->length > 1) + { + find_quicker(stack_a, stack_b); + wich_algo(stack_a, stack_b); + } + if (stack_b->premier->number >= stack_b->moyenne) + { + while (stack_b->premier->number != stack_b->max) + rotate(stack_b, 'b'); + } + else + { + while (stack_b->premier->number != stack_b->max) + reverse_rotate(stack_b, 'b'); + } + while (stack_a->length != length) + { + if (stack_a->dernier->number > stack_b->premier->number \ + && stack_a->dernier->number < stack_a->premier->number) + reverse_rotate(stack_a, 'a'); + push(stack_a, stack_b, 'a'); + } +} + +void turc(t_Liste *stack_a, t_Liste *stack_b) +{ + long length; + + length = stack_a->length; + push_two_number(stack_a, stack_b); + anti_25_ligne(stack_a, stack_b, length); +}