-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_putnbr_fd.c
90 lines (82 loc) · 1.78 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
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yhadari <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/22 19:42:51 by yhadari #+# #+# */
/* Updated: 2021/06/12 17:18:01 by yhadari ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
static void checknum(int n, int *fd)
{
if (n == -2147483648)
{
write(*fd, "-2147483648", 11);
return ;
}
if (n == 0)
{
write(*fd, "0", 1);
return ;
}
if (n == 2147483647)
{
write(*fd, "2147483647", 10);
return ;
}
}
static int lenum(int n)
{
int i;
i = 0;
while (n > 0)
{
n = n / 10;
i++;
}
return (i);
}
static void revnum(int *n, int *ncopy, int *i)
{
if (*n > 0)
*ncopy = *n;
*i = lenum(*n);
if (*n < 0)
{
*ncopy = - *n;
*i = lenum(- *n);
}
}
void ft_putchar_fd(char c, int fd)
{
write(fd, &c, 1);
}
void ft_putnbr_fd(int n, int fd)
{
int i;
int j;
int ncopy;
j = 9;
if (n == 0 || n == -2147483648 || n == 2147483647)
{
checknum(n, &fd);
return ;
}
revnum(&n, &ncopy, &i);
if (n < 0)
{
write(fd, "-", 1);
n = -n;
}
while (i-- > 0)
{
while (n > j)
n = n / 10;
j = (j * 10) + 9;
ft_putchar_fd((n % 10) + 48, fd);
n = ncopy;
}
}