-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_putnbr_base.c
62 lines (57 loc) · 1.69 KB
/
ft_putnbr_base.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: knzeng-e <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/03/24 04:41:27 by knzeng-e #+# #+# */
/* Updated: 2016/03/26 11:29:56 by knzeng-e ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_check_base(char *base)
{
int i;
int current;
if (ft_strlen(base) <= 1)
return (0);
i = 0;
while (base[i] != '\0')
{
if (base[i] == '-' || base[i] == '+')
return (0);
current = i + 1;
while ((base[current] != base[i]) && (base[current] != '\0'))
current++;
if (base[current] == base[i])
return (0);
i++;
}
return (1);
}
void ft_putnbr_base(int nbr, char *base)
{
unsigned int n;
if (ft_check_base(base))
{
if (nbr < 0)
{
ft_putchar('-');
nbr *= -1;
}
n = (unsigned int)nbr;
if (n == 2147483648)
ft_putstr("2147483648");
else
{
if (n >= (unsigned int)ft_strlen(base))
{
ft_putnbr_base(n / (unsigned int)ft_strlen(base), base);
ft_putnbr_base(n % (unsigned int)ft_strlen(base), base);
}
else
ft_putchar(base[n]);
}
}
}