-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnbr_utils.c
108 lines (97 loc) · 2.28 KB
/
nbr_utils.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* nbr_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ybarbot <ybarbot@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 21:26:56 by yvann #+# #+# */
/* Updated: 2023/11/20 16:31:04 by ybarbot ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_check_character_occurence(char *base)
{
int j;
int i;
char ascii_table[127];
j = 0;
while (j != 127)
{
ascii_table[j] = 0;
i = 0;
while (base[i])
{
if (j == base[i])
ascii_table[j]++;
i++;
}
j++;
}
j = 0;
while (ascii_table[j])
{
if (ascii_table[j] > 1)
return (0);
j++;
}
return (1);
}
int ft_check_parameters(char *base)
{
int i;
if (base[0] == '\0' || base[1] == '\0' )
return (0);
i = 0;
while (base[i])
{
if (base[i] == '+' || base[i] == '-')
return (0);
i++;
}
return (1);
}
static int ft_unsigned_nbr(unsigned int nbr, char *base)
{
unsigned int i;
unsigned int digit;
unsigned int counter;
i = 0;
counter = 0;
while (base[i])
i++;
digit = nbr % i;
nbr = nbr / i;
if (nbr)
counter += ft_unsigned_nbr(nbr, base);
write(1, &base[digit], 1);
counter++;
return (counter);
}
int ft_putlnbr_base(int nbr, char *base)
{
unsigned int p;
int counter;
counter = 0;
if (ft_check_parameters(base) && ft_check_character_occurence(base))
{
if (nbr < 0)
{
write (1, "-", 1);
counter++;
p = -nbr;
}
else
p = nbr;
counter += ft_unsigned_nbr(p, base);
}
return (counter);
}
unsigned int ft_putlnbr_ubase(unsigned int nbr, char *base)
{
unsigned int counter;
counter = 0;
if (ft_check_parameters(base) && ft_check_character_occurence(base))
counter += ft_unsigned_nbr(nbr, base);
return (counter);
}