71 lines
1.7 KiB
C
71 lines
1.7 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* ft_putnbr_base.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: pbonilla <marvin@42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2020/09/16 14:58:50 by pbonilla #+# #+# */
|
|
/* Updated: 2021/02/28 02:52:20 by pbonilla ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "libft.h"
|
|
|
|
void write_base(long nbr, char *base)
|
|
{
|
|
long tmp;
|
|
int len_base;
|
|
|
|
tmp = nbr;
|
|
len_base = ft_strlen(base);
|
|
if (tmp < 0)
|
|
{
|
|
ft_putchar('-');
|
|
tmp *= -1;
|
|
}
|
|
if (tmp > len_base - 1)
|
|
{
|
|
write_base(tmp / len_base, base);
|
|
write_base(tmp % len_base, base);
|
|
}
|
|
else
|
|
ft_putchar(tmp + base[0]);
|
|
}
|
|
|
|
int check_base(char *base, int len_base)
|
|
{
|
|
int i;
|
|
int j;
|
|
|
|
i = 0;
|
|
if (len_base > 1)
|
|
{
|
|
while (base[i])
|
|
{
|
|
j = i;
|
|
while (base[j])
|
|
{
|
|
if ((base[i] == base[j] && i != j) ||
|
|
base[j] == '+' || base[j] == '-')
|
|
return (0);
|
|
j++;
|
|
}
|
|
i++;
|
|
}
|
|
return (1);
|
|
}
|
|
return (0);
|
|
}
|
|
|
|
void ft_putnbr_base(int nbr, char *base)
|
|
{
|
|
long tmp;
|
|
int len_base;
|
|
|
|
tmp = nbr;
|
|
len_base = ft_strlen(base);
|
|
if (check_base(base, len_base))
|
|
write_base(tmp, base);
|
|
}
|