init
This commit is contained in:
81
C/c04/ex04/ft_putnbr_base.c
Normal file
81
C/c04/ex04/ft_putnbr_base.c
Normal file
@ -0,0 +1,81 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putnbr_base.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/07/19 13:52:59 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/07/19 19:02:38 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_put(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_put('-');
|
||||
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_put(base[nbr]);
|
||||
}
|
||||
/*
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
ft_putnbr_base(128, "01");
|
||||
}
|
||||
*/
|
Reference in New Issue
Block a user