ajout des fonctions atol free_tab et isnumber
This commit is contained in:
13
Makefile
13
Makefile
@@ -14,6 +14,14 @@ DEPS = $(_DEP:%.h=$(IDIR)/%.h)
|
|||||||
CFLAGS = -I$(IDIR)
|
CFLAGS = -I$(IDIR)
|
||||||
CFLAGS += -Wall -Wextra -Werror -g3
|
CFLAGS += -Wall -Wextra -Werror -g3
|
||||||
|
|
||||||
|
# ft_putstr
|
||||||
|
# ft_atoi
|
||||||
|
# ft_putchar
|
||||||
|
# ft_putnbrendl
|
||||||
|
# ft_putnbrendl_fd
|
||||||
|
# ft_putchar_fd
|
||||||
|
# ft_putnbr_fd
|
||||||
|
|
||||||
SRCS = ft_memset.c \
|
SRCS = ft_memset.c \
|
||||||
ft_bzero.c \
|
ft_bzero.c \
|
||||||
ft_memcpy.c \
|
ft_memcpy.c \
|
||||||
@@ -25,6 +33,7 @@ SRCS = ft_memset.c \
|
|||||||
ft_isalpha.c \
|
ft_isalpha.c \
|
||||||
ft_isdigit.c \
|
ft_isdigit.c \
|
||||||
ft_isalnum.c \
|
ft_isalnum.c \
|
||||||
|
ft_isnumber.c \
|
||||||
ft_isascii.c \
|
ft_isascii.c \
|
||||||
ft_isprint.c \
|
ft_isprint.c \
|
||||||
ft_toupper.c \
|
ft_toupper.c \
|
||||||
@@ -37,6 +46,7 @@ SRCS = ft_memset.c \
|
|||||||
ft_strlcat.c \
|
ft_strlcat.c \
|
||||||
ft_strnstr.c \
|
ft_strnstr.c \
|
||||||
ft_atoi.c \
|
ft_atoi.c \
|
||||||
|
ft_atol.c \
|
||||||
ft_calloc.c \
|
ft_calloc.c \
|
||||||
ft_strdup.c \
|
ft_strdup.c \
|
||||||
\
|
\
|
||||||
@@ -107,7 +117,8 @@ SRCS = ft_memset.c \
|
|||||||
ft_greater.c \
|
ft_greater.c \
|
||||||
ft_smaller.c \
|
ft_smaller.c \
|
||||||
ft_sign.c \
|
ft_sign.c \
|
||||||
ft_sqrt.c
|
ft_sqrt.c \
|
||||||
|
ft_free_tab.c
|
||||||
|
|
||||||
|
|
||||||
ODIR = ./builds
|
ODIR = ./builds
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ int ft_memcmp(const void *s1, const void *s2, size_t n);
|
|||||||
size_t ft_strlen(const char *str);
|
size_t ft_strlen(const char *str);
|
||||||
int ft_isalpha(int c);
|
int ft_isalpha(int c);
|
||||||
int ft_isdigit(int c);
|
int ft_isdigit(int c);
|
||||||
|
int ft_isnumber(char *nb);
|
||||||
int ft_isalnum(int c);
|
int ft_isalnum(int c);
|
||||||
int ft_isascii(int c);
|
int ft_isascii(int c);
|
||||||
int ft_isprint(int c);
|
int ft_isprint(int c);
|
||||||
@@ -40,6 +41,7 @@ size_t ft_strlcpy(char *dst, const char *src, size_t size);
|
|||||||
size_t ft_strlcat(char *dst, const char *src, size_t size);
|
size_t ft_strlcat(char *dst, const char *src, size_t size);
|
||||||
char *ft_strnstr(const char *b, const char *l, size_t s);
|
char *ft_strnstr(const char *b, const char *l, size_t s);
|
||||||
int ft_atoi(const char *str);
|
int ft_atoi(const char *str);
|
||||||
|
long ft_atol(const char *str);
|
||||||
void *ft_calloc(size_t count, size_t size);
|
void *ft_calloc(size_t count, size_t size);
|
||||||
char *ft_strdup(const char *s1);
|
char *ft_strdup(const char *s1);
|
||||||
|
|
||||||
@@ -109,5 +111,6 @@ int ft_greater(int a, int b);
|
|||||||
int ft_smaller(int a, int b);
|
int ft_smaller(int a, int b);
|
||||||
int ft_sign(int i);
|
int ft_sign(int i);
|
||||||
int ft_sqrt(int i);
|
int ft_sqrt(int i);
|
||||||
|
void ft_free_tab(char **tab);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -10,6 +10,12 @@
|
|||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
/*
|
||||||
|
** i don't understand the purpose of this function...
|
||||||
|
** it takes a 2D array, and for each array it checks
|
||||||
|
** if a function given in parameters is true or false
|
||||||
|
*/
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
int ft_any(char **tab, int (*f)(char*))
|
int ft_any(char **tab, int (*f)(char*))
|
||||||
|
|||||||
@@ -14,26 +14,20 @@
|
|||||||
|
|
||||||
int ft_atoi(const char *str)
|
int ft_atoi(const char *str)
|
||||||
{
|
{
|
||||||
long long nbr;
|
long nbr;
|
||||||
int i;
|
int i;
|
||||||
int n;
|
int negatif;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
n = 1;
|
negatif = 1;
|
||||||
nbr = 0;
|
nbr = 0;
|
||||||
while ((str[i] == ' ') || (str[i] > 8 && str[i] < 14))
|
while ((str[i] == ' ') || (str[i] > 8 && str[i] < 14))
|
||||||
i++;
|
i++;
|
||||||
if (str[i] == '-')
|
if (str[i] == '-')
|
||||||
n = -1;
|
negatif = -1;
|
||||||
if (str[i] == '+' || str[i] == '-')
|
if (str[i] == '+' || str[i] == '-')
|
||||||
i++;
|
i++;
|
||||||
while (str[i] >= '0' && str[i] <= '9')
|
while (str[i] >= '0' && str[i] <= '9')
|
||||||
{
|
|
||||||
if ((nbr >= 922337203685477580
|
|
||||||
&& ((str[i] > 8 && n < 0) || (str[i] > 7 && n > 0))))
|
|
||||||
return ((n > 0) ? -1 : 0);
|
|
||||||
else
|
|
||||||
nbr = nbr * 10 + (str[i++] - '0');
|
nbr = nbr * 10 + (str[i++] - '0');
|
||||||
}
|
return (nbr * negatif);
|
||||||
return (nbr * n);
|
|
||||||
}
|
}
|
||||||
|
|||||||
22
srcs/ft_atol.c
Normal file
22
srcs/ft_atol.c
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
long ft_atol(const char *str)
|
||||||
|
{
|
||||||
|
long long nbr;
|
||||||
|
int i;
|
||||||
|
int negatif;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
negatif = 1;
|
||||||
|
nbr = 0;
|
||||||
|
while ((str[i] == ' ') || (str[i] > 8 && str[i] < 14))
|
||||||
|
i++;
|
||||||
|
if (str[i] == '-')
|
||||||
|
negatif = -1;
|
||||||
|
if (str[i] == '+' || str[i] == '-')
|
||||||
|
i++;
|
||||||
|
while (str[i] >= '0' && str[i] <= '9')
|
||||||
|
nbr = nbr * 10 + (str[i++] - '0');
|
||||||
|
return (nbr * negatif);
|
||||||
|
}
|
||||||
15
srcs/ft_free_tab.c
Normal file
15
srcs/ft_free_tab.c
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
void ft_free_tab(char **tab)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (tab[i] != NULL)
|
||||||
|
{
|
||||||
|
free(tab[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
free(tab);
|
||||||
|
}
|
||||||
19
srcs/ft_isnumber.c
Normal file
19
srcs/ft_isnumber.c
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
int ft_isnumber(char *nb)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
if (nb[i] == '+' || nb[i] == '-')
|
||||||
|
i++;
|
||||||
|
while (nb[i] != '\0')
|
||||||
|
{
|
||||||
|
if (ft_isdigit(nb[i]) == 0)
|
||||||
|
return (0);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,7 +1,11 @@
|
|||||||
#ifndef LIBFTEST_H
|
#ifndef LIBFTEST_H
|
||||||
# define LIBFTEST_H
|
# define LIBFTEST_H
|
||||||
# include "libft.h"
|
# include "libft.h"
|
||||||
# include <stdio.h>
|
# include <unistd.h> // for write
|
||||||
|
# include <stdio.h> // for printf
|
||||||
|
# include <stdlib.h> // for malloc
|
||||||
|
# include <string.h> // for strdup and strlen and strcmp
|
||||||
|
# include <ctype.h> // for characters like isalpha or isdigit etc
|
||||||
|
|
||||||
# define RED write(1, "\033[91m", 5);
|
# define RED write(1, "\033[91m", 5);
|
||||||
# define GREEN write(1, "\033[92m", 5);
|
# define GREEN write(1, "\033[92m", 5);
|
||||||
|
|||||||
BIN
testing/libftest
BIN
testing/libftest
Binary file not shown.
@@ -1,6 +1,6 @@
|
|||||||
#include "libftest.h"
|
#include "libftest.h"
|
||||||
|
|
||||||
void compare(int i, int ans)
|
void compare_abs(int i, int ans)
|
||||||
{
|
{
|
||||||
if (ft_abs(i) != ans)
|
if (ft_abs(i) != ans)
|
||||||
{
|
{
|
||||||
@@ -15,10 +15,9 @@ void compare(int i, int ans)
|
|||||||
|
|
||||||
void test_abs(void)
|
void test_abs(void)
|
||||||
{
|
{
|
||||||
compare(-1, 1);
|
compare_abs(-1, 1);
|
||||||
compare(0, 0);
|
compare_abs(0, 0);
|
||||||
compare(-0, 0);
|
compare_abs(-0, 0);
|
||||||
compare(-1000, 1000);
|
compare_abs(-1000, 1000);
|
||||||
compare(-2147483648, 2147483648);
|
compare_abs(-2147483647, 2147483647);
|
||||||
compare(2147483647, 2147483647);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,131 @@
|
|||||||
#include "libftest.h"
|
#include "libftest.h"
|
||||||
|
|
||||||
//int test_any(char **tab, int (*f)(char*))
|
char **create_tab(void)
|
||||||
|
{
|
||||||
|
char **tab;
|
||||||
|
|
||||||
|
tab = (char **)malloc(sizeof(char *) * (6 + 1));
|
||||||
|
tab[0] = strdup("premiere ligne de test");
|
||||||
|
tab[1] = strdup("deuxieme ligne de test");
|
||||||
|
tab[2] = strdup("troisieme ligne pour le fun");
|
||||||
|
tab[3] = strdup("quatrieme linge avec une erreur");
|
||||||
|
tab[4] = strdup("cinquieme peche a la ligne");
|
||||||
|
tab[5] = strdup("sixieme et derniere ligne");
|
||||||
|
return (tab);
|
||||||
|
}
|
||||||
|
|
||||||
|
void free_tab(char **tab)
|
||||||
|
{
|
||||||
|
free(tab[0]);
|
||||||
|
free(tab[1]);
|
||||||
|
free(tab[2]);
|
||||||
|
free(tab[3]);
|
||||||
|
free(tab[4]);
|
||||||
|
free(tab[5]);
|
||||||
|
free(tab);
|
||||||
|
}
|
||||||
|
|
||||||
|
int contain_a(char *s)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = -1;
|
||||||
|
while (s[++i])
|
||||||
|
if (s[i] == 'a')
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int contain_y(char *s)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = -1;
|
||||||
|
while (s[++i])
|
||||||
|
if (s[i] == 'y')
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int contain_z(char *s)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = -1;
|
||||||
|
while (s[++i])
|
||||||
|
if (s[i] == 'z')
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int contain_x(char *s)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = -1;
|
||||||
|
while (s[++i])
|
||||||
|
if (s[i] == 'x')
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int contain_u(char *s)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = -1;
|
||||||
|
while (s[++i])
|
||||||
|
if (s[i] == 'u')
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int contain_1(char *s)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = -1;
|
||||||
|
while (s[++i])
|
||||||
|
if (s[i] == '1')
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void compare_any(char **tab, int (*f)(char *), char *s, int solution)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = ft_any(tab, f);
|
||||||
|
if (i != solution)
|
||||||
|
{
|
||||||
|
write(1, "\033[91m", 5);
|
||||||
|
printf("error: ");
|
||||||
|
write(1, "\033[0m", 4);
|
||||||
|
printf("ft_any(tab, %s) returned", s);
|
||||||
|
printf(" %i ", i);
|
||||||
|
printf("in tab :\n");
|
||||||
|
printf(" %s:\n", tab[0]);
|
||||||
|
printf(" %s:\n", tab[1]);
|
||||||
|
printf(" %s:\n", tab[2]);
|
||||||
|
printf(" %s:\n", tab[3]);
|
||||||
|
printf(" %s:\n", tab[4]);
|
||||||
|
printf(" %s:\n", tab[5]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void test_any(void)
|
void test_any(void)
|
||||||
{
|
{
|
||||||
|
char **tab;
|
||||||
|
|
||||||
|
tab = create_tab();
|
||||||
|
|
||||||
|
compare_any(tab, contain_a, "contain_a", 1);
|
||||||
|
compare_any(tab, contain_y, "contain_y", 0);
|
||||||
|
compare_any(tab, contain_z, "contain_z", 0);
|
||||||
|
compare_any(tab, contain_x, "contain_x", 1);
|
||||||
|
compare_any(tab, contain_u, "contain_u", 1);
|
||||||
|
compare_any(tab, contain_1, "contain_1", 0);
|
||||||
|
|
||||||
|
free_tab(tab);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user