42_C_PISCINE/C/c04v2/ex04/ft_putnbr_base.c
Camille Chauvet 29ed24d567 init
2023-05-17 16:45:25 +00:00

79 lines
1.7 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/19 13:52:59 by cchauvet #+# #+# */
/* Updated: 2022/08/03 17:47:16 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
int ft_baser(char *str)
{
int i;
int j;
int count;
i = -1;
while (str[++i] != 0)
{
count = 0;
j = 0;
while (str[j] != 0)
{
if (str[i] == str[j])
count++;
j++;
}
if (count > 1)
return (0);
if (str[i] == '+')
return (0);
if (str[i] == '-')
return (0);
}
if (i == 1)
return (0);
return (i);
}
void ft_putchar(char c)
{
write(1, &c, 1);
}
void ft_putnbr_base(int nbr, char *base)
{
int base_size;
base_size = ft_baser(base);
if (base_size == 0)
return ;
if (nbr == -2147483648)
{
ft_putnbr_base(-2, base);
ft_putnbr_base(147483648, base);
}
else if (nbr < 0)
{
ft_putchar('-');
ft_putnbr_base(-nbr, base);
}
else if (nbr > base_size - 1)
{
ft_putnbr_base(nbr / base_size, base);
ft_putnbr_base(nbr % base_size, base);
}
else
ft_putchar(base[nbr]);
}
int main ()
{
ft_putnbr_base(15, "01");
}