-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_putnbr_base.c
executable file
·74 lines (68 loc) · 1.92 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
63
64
65
66
67
68
69
70
71
72
73
74
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akharrou <akharrou@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/21 19:24:05 by akharrou #+# #+# */
/* Updated: 2019/03/04 13:17:20 by akharrou ### ########.fr */
/* */
/* ************************************************************************** */
#include "../Includes/ctype_42.h"
#include "../Includes/stdio_42.h"
#include "../Includes/string_42.h"
static int ft_validate_base_system(char *base_str)
{
short i;
short j;
short count;
if (!base_str || !base_str[0] || !base_str[1])
return (0);
i = -1;
while (base_str[++i])
{
count = 0;
if (!ISPRINT(base_str[i]))
return (0);
if (base_str[i] == '+' || base_str[i] == '-')
return (0);
j = -1;
while (base_str[++j])
if (base_str[i] == base_str[j])
count++;
if (count > 1)
return (0);
}
return (1);
}
static void ft_putnbr_base_util(long nbr, char *base)
{
int quotient;
long col;
short i;
short base_sys;
if (!ft_validate_base_system(base))
return ;
i = 0;
col = ft_strlen(base);
base_sys = col;
if (nbr < 0)
ft_putchar('-');
if (nbr < 0)
nbr = -nbr;
while (col <= nbr)
col *= base_sys;
while (col > 1)
{
col /= base_sys;
quotient = (nbr / col);
ft_putchar(base[quotient]);
nbr -= (quotient * col);
}
}
void ft_putnbr_base(int nbr, char *base)
{
ft_putnbr_base_util((long)nbr, base);
return ;
}