Adding the negative number

This commit is contained in:
EniumRaphael 2024-05-29 16:24:37 +02:00
parent e8a89c456e
commit 1960bbc47a
4 changed files with 21 additions and 12 deletions

View file

@ -6,7 +6,7 @@
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */ /* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/04 12:16:08 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 typedef struct s_number
{ {
t_i8 sign;
t_str number; t_str number;
t_str int_part; t_str int_part;
t_str float_part; t_str float_part;
@ -27,7 +28,7 @@ typedef struct s_number
t_usize float_size; t_usize float_size;
} t_number; } 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); t_error ft_init_numbers(t_str str, t_number *nb);
char **ft_split(char const *s, char c); char **ft_split(char const *s, char c);

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/27 14:41:26 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[]) int main(int argc, char *argv[])
{ {
t_number nb; t_number *nb;
nb = (t_number *)malloc(sizeof(t_number));
if (argc != 2) if (argc != 2)
{ {
printf("Usage: %s <string>\n", argv[0]); printf("Usage: %s <string>\n", argv[0]);
@ -24,13 +25,13 @@ int main(int argc, char *argv[])
} }
else if (argc == 2) else if (argc == 2)
{ {
if (ft_init_numbers(argv[1], &nb) == ERROR) if (ft_init_numbers(argv[1], nb) == ERROR)
{ {
printf("Error\n"); printf("Error\n");
return 1; return 1;
} }
else 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); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/28 13:41:03 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 int_size;
t_usize float_size; t_usize float_size;
t_u8 sign;
t_str *tmp; t_str *tmp;
int_size = 0; int_size = 0;
float_size = 0; float_size = 0;
sign = 1;
if (nb == NULL) if (nb == NULL)
return (ERROR); return (ERROR);
if (ft_nblen(str, &int_size, &float_size) == ERROR) if (ft_nblen(str, &int_size, &float_size, &sign) == ERROR)
return (ERROR); return (ERROR);
else else
{ {
nb = (t_number *)malloc(sizeof(t_number));
nb->number = str; nb->number = str;
tmp = ft_split(str, '.'); tmp = ft_split(str, '.');
nb->sign = sign;
nb->int_part = tmp[0]; nb->int_part = tmp[0];
nb->float_part = tmp[1]; nb->float_part = tmp[1];
// free_strs(tmp);
nb->int_size = int_size; nb->int_size = int_size;
nb->float_size = float_size; nb->float_size = float_size;
} }

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/27 12:30:54 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); 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; t_usize i;
(*int_len) = 0; (*int_len) = 0;
(*float_len) = 0; (*float_len) = 0;
(*sign) = 1;
i = 0; i = 0;
if (str == NULL) if (str == NULL)
return (ERROR); return (ERROR);
if (str[i] == '-')
{
(*sign) = -1;
i++;
}
while (str[i] != '.' && str[i] != '\0') while (str[i] != '.' && str[i] != '\0')
ft_counter(str, &i); ft_counter(str, &i);
(*int_len) = i; (*int_len) = i;