From e8a89c456ecf792e8f1ea77bb622db7b26fdfb43 Mon Sep 17 00:00:00 2001 From: EniumRaphael Date: Wed, 29 May 2024 15:38:19 +0200 Subject: [PATCH 1/2] Repushing the correct version --- math/Makefile | 16 +++---- math/main.c | 37 ++++++++++++---- math/sources/ft_split.c | 93 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+), 16 deletions(-) create mode 100644 math/sources/ft_split.c diff --git a/math/Makefile b/math/Makefile index f79bae6e..6d593eef 100644 --- a/math/Makefile +++ b/math/Makefile @@ -6,7 +6,7 @@ # By: rparodi +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/12 11:05:05 by rparodi #+# #+# # -# Updated: 2024/05/29 14:20:08 by rparodi ### ########.fr # +# Updated: 2024/05/29 15:37:40 by rparodi ### ########.fr # # # # **************************************************************************** # @@ -14,7 +14,7 @@ # Variables # Name -NAME = libmmath.a +NAME = test LIBDIRNAME = libft SRCDIRNAME = sources @@ -26,15 +26,17 @@ RM = rm -rf CFLAGS = -Werror -Wextra -Wall -Wno-unused-command-line-argument -g3 -MMD # Sources -LIB = ./libft/ft_split.c +LIB = -SRC = ./sources/operation/ft_pow.c \ +SRC = ./main.c \ + ./sources/operation/ft_pow.c \ ./sources/operation/ft_add.c \ ./sources/utils/ft_init_numbers.c \ ./sources/utils/ft_nblen.c \ ./sources/comparison/ft_is_less.c \ ./sources/comparison/ft_equal.c \ - ./sources/comparison/ft_is_greater.c + ./sources/comparison/ft_is_greater.c \ + ./sources/ft_split.c # Objects OBJDIRNAME = ./objects @@ -83,7 +85,7 @@ $(NAME): $(OBJ) $(LIB_OBJ) @mkdir -p $(OBJDIRNAME)/$(LIBDIRNAME) @mkdir -p $(OBJDIRNAME)/$(SRCDIRNAME) @printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n' - @ar rc $(NAME) $(OBJ) $(OBJBonus) 1>/dev/null + @ar rc $(NAME) $(OBJ) $(OBJBonus) @ranlib $(NAME) # Creating the objects $(OBJDIRNAME)/%.o: %.c @@ -125,5 +127,5 @@ footer: # Phony .PHONY: all bonus clean fclean re -# -DCMAKE_EXPORT_COMPILE_COMMANDS=ON +-DCMAKE_EXPORT_COMPILE_COMMANDS=ON -include ${OBJ:.o=.d} diff --git a/math/main.c b/math/main.c index a66d5602..62045190 100644 --- a/math/main.c +++ b/math/main.c @@ -1,17 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/27 14:41:26 by rparodi #+# #+# */ +/* Updated: 2024/05/28 15:48:40 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include #include "./includes/ft_math.h" -int main(int argc, char *argv[]) +int main(int argc, char *argv[]) { - t_number nb; + t_number nb; - if (argc == 2) + if (argc != 2) { - ft_init_numbers(argv[1], &nb); - printf("Number: %s\n", nb.number); - printf("Integer: %s\n", nb.int_part); - printf("Float: %s\n", nb.float_part); - printf("Integer size: %zu\n", nb.int_size); - printf("Float size: %zu\n", nb.float_size); + printf("Usage: %s \n", argv[0]); + return 1; + } + else if (argc == 2) + { + if (ft_init_numbers(argv[1], &nb) == ERROR) + { + printf("Error\n"); + return 1; + } + else + printf("Number = %s \n(int: %s, %zu) (float: %s, %zu)\n", nb.number, nb.int_part, nb.int_size, nb.float_part, nb.float_size); } return (0); } diff --git a/math/sources/ft_split.c b/math/sources/ft_split.c new file mode 100644 index 00000000..cfeb6b3b --- /dev/null +++ b/math/sources/ft_split.c @@ -0,0 +1,93 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 13:56:02 by rparodi #+# #+# */ +/* Updated: 2024/05/28 15:39:50 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/ft_math.h" + +static int count_words(const char *str, char sep) +{ + int i; + int count; + + i = 0; + count = 0; + while (str[i]) + { + while (str[i] == sep && str[i]) + i++; + if (str[i] != sep && str[i]) + { + while (str[i] != sep && str[i]) + i++; + count++; + } + } + return (count); +} + +static char *ft_strndup(const char *s, int j) +{ + int i; + char *str; + + i = 0; + str = (char *)malloc((j + 1)); + if (!str) + return (NULL); + while (s[i] && i < j) + { + str[i] = s[i]; + i++; + } + str[i] = '\0'; + return (str); +} + +static char **ext_w(char **words_array, const char *str, char sep, int size) +{ + int i; + int j; + + i = 0; + j = 0; + while (j < size) + { + while (str[i] == sep && str[i]) + i++; + str = str + i; + i = 0; + while (str[i] != sep && str[i]) + i++; + words_array[j++] = ft_strndup(str, i); + str = str + i; + i = 0; + } + words_array[j] = 0; + return (words_array); +} + +char **ft_split(char const *s, char c) +{ + int size; + char **words_array; + + size = count_words(s, c); + words_array = (char **)malloc(sizeof(char *) * (size + 1)); + if (!words_array) + return (NULL); + if (size == 0) + { + words_array[0] = NULL; + return (words_array); + } + words_array = ext_w(words_array, s, c, size); + return (words_array); +} From 1960bbc47a4d9823908aee3b07c08e5395f8106b Mon Sep 17 00:00:00 2001 From: EniumRaphael Date: Wed, 29 May 2024 16:24:37 +0200 Subject: [PATCH 2/2] Adding the negative number --- math/includes/ft_math.h | 5 +++-- math/main.c | 9 +++++---- math/sources/utils/ft_init_numbers.c | 9 +++++---- math/sources/utils/ft_nblen.c | 10 ++++++++-- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/math/includes/ft_math.h b/math/includes/ft_math.h index 133a6737..1d5a64aa 100644 --- a/math/includes/ft_math.h +++ b/math/includes/ft_math.h @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/04 12:16:08 by rparodi #+# #+# */ -/* Updated: 2024/05/28 15:43:40 by rparodi ### ########.fr */ +/* Updated: 2024/05/29 16:22:10 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,6 +20,7 @@ typedef struct s_number { + t_i8 sign; t_str number; t_str int_part; t_str float_part; @@ -27,7 +28,7 @@ typedef struct s_number t_usize float_size; } t_number; -t_error ft_nblen(const t_str str, t_usize *interger, t_usize *floating); +t_error ft_nblen(const t_str str, t_usize *int_len, t_usize *float_len, t_u8 *sign); t_error ft_init_numbers(t_str str, t_number *nb); char **ft_split(char const *s, char c); diff --git a/math/main.c b/math/main.c index 62045190..736faada 100644 --- a/math/main.c +++ b/math/main.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/27 14:41:26 by rparodi #+# #+# */ -/* Updated: 2024/05/28 15:48:40 by rparodi ### ########.fr */ +/* Updated: 2024/05/29 16:16:28 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,8 +15,9 @@ int main(int argc, char *argv[]) { - t_number nb; + t_number *nb; + nb = (t_number *)malloc(sizeof(t_number)); if (argc != 2) { printf("Usage: %s \n", argv[0]); @@ -24,13 +25,13 @@ int main(int argc, char *argv[]) } else if (argc == 2) { - if (ft_init_numbers(argv[1], &nb) == ERROR) + if (ft_init_numbers(argv[1], nb) == ERROR) { printf("Error\n"); return 1; } else - printf("Number = %s \n(int: %s, %zu) (float: %s, %zu)\n", nb.number, nb.int_part, nb.int_size, nb.float_part, nb.float_size); + printf("Number = %s \n(int: %s, %zu) (float: %s, %zu)\n", nb->number, nb->int_part, nb->int_size, nb->float_part, nb->float_size); } return (0); } diff --git a/math/sources/utils/ft_init_numbers.c b/math/sources/utils/ft_init_numbers.c index 1ce7a650..0882271e 100644 --- a/math/sources/utils/ft_init_numbers.c +++ b/math/sources/utils/ft_init_numbers.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/28 13:41:03 by rparodi #+# #+# */ -/* Updated: 2024/05/28 15:44:24 by rparodi ### ########.fr */ +/* Updated: 2024/05/29 16:23:35 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,22 +16,23 @@ t_error ft_init_numbers(t_str str, t_number *nb) { t_usize int_size; t_usize float_size; + t_u8 sign; t_str *tmp; int_size = 0; float_size = 0; + sign = 1; if (nb == NULL) return (ERROR); - if (ft_nblen(str, &int_size, &float_size) == ERROR) + if (ft_nblen(str, &int_size, &float_size, &sign) == ERROR) return (ERROR); else { - nb = (t_number *)malloc(sizeof(t_number)); nb->number = str; tmp = ft_split(str, '.'); + nb->sign = sign; nb->int_part = tmp[0]; nb->float_part = tmp[1]; - // free_strs(tmp); nb->int_size = int_size; nb->float_size = float_size; } diff --git a/math/sources/utils/ft_nblen.c b/math/sources/utils/ft_nblen.c index 47cf5968..cff453ed 100644 --- a/math/sources/utils/ft_nblen.c +++ b/math/sources/utils/ft_nblen.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/27 12:30:54 by rparodi #+# #+# */ -/* Updated: 2024/05/28 13:34:20 by rparodi ### ########.fr */ +/* Updated: 2024/05/29 16:20:52 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,15 +21,21 @@ t_error ft_counter(const t_str str, t_usize *i) return (NO_ERROR); } -t_error ft_nblen(const t_str str, t_usize *int_len, t_usize *float_len) +t_error ft_nblen(const t_str str, t_usize *int_len, t_usize *float_len, t_u8 *sign) { t_usize i; (*int_len) = 0; (*float_len) = 0; + (*sign) = 1; i = 0; if (str == NULL) return (ERROR); + if (str[i] == '-') + { + (*sign) = -1; + i++; + } while (str[i] != '.' && str[i] != '\0') ft_counter(str, &i); (*int_len) = i;