-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_putnbr_fd.c
59 lines (54 loc) · 1.49 KB
/
ft_putnbr_fd.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: amacarul <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/14 18:47:13 by amacarul #+# #+# */
/* Updated: 2024/09/21 18:14:07 by amacarul ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static void is_positive(int n, int fd)
{
int n_temp;
int digit;
int divisor;
n_temp = n;
divisor = 1;
while (n_temp > 9)
{
n_temp /= 10;
divisor *= 10;
}
while (divisor > 0)
{
digit = (n / divisor) % 10;
ft_putchar_fd(digit + '0', fd);
divisor /= 10;
}
}
void is_negative(int n, int fd)
{
if (n == -2147483648)
ft_putstr_fd("-2147483648", fd);
else if (n < 0)
{
ft_putchar_fd('-', fd);
n = -n;
is_positive(n, fd);
}
}
void ft_putnbr_fd(int n, int fd)
{
if (n < 0)
is_negative(n, fd);
if (n == 0)
{
ft_putchar_fd('0', fd);
return ;
}
if (n > 0)
is_positive(n, fd);
}