-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_nbr.c
121 lines (111 loc) · 3.13 KB
/
ft_nbr.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
109
110
111
112
113
114
115
116
117
118
119
120
121
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_nbr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rledrin <rledrin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/13 10:15:50 by rledrin #+# #+# */
/* Updated: 2019/11/13 10:15:51 by rledrin ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_comptage(unsigned int nb)
{
int i;
i = 0;
if (nb == 0)
return (1);
while (nb > 0)
{
nb /= 10;
i++;
}
return (i);
}
void ft_check_nbr(t_tab *t)
{
int nb;
int nb_len;
unsigned int nbu;
nb_len = 0;
nb = va_arg(t->ap, int);
nbu = 0;
if (nb < 0 && t->type == 'd')
{
t->sign = 1;
if (nb == -2147483648)
{
nbu = 2147483648;
nb_len = ft_comptage(nbu);
ft_print_nbr(t, nbu, nb_len);
return ;
}
nb = -nb;
nb_len = ft_comptage(nb);
ft_print_nbr(t, nb, nb_len);
return ;
}
nb_len = ft_comptage((unsigned int)nb);
ft_print_nbr(t, (unsigned int)nb, nb_len);
}
void ft_print_nbr2(t_tab *t, unsigned int nb, int nblen, int *variable)
{
int check;
check = 1;
if (nb == 0 && t->precision == 0)
{
check = 0;
nblen = 0;
}
if (t->sign == 0 && t->space == 1)
ft_putchar_fd_printf(t, ' ', 1);
*variable = 0;
if (t->min_len > t->precision && t->precision >= nblen)
*variable = t->min_len - (t->precision + t->sign);
else if (t->precision >= t->min_len && t->precision >= nblen)
*variable = 0;
else if (t->min_len > t->precision && nblen > t->precision)
*variable = t->min_len - nblen - t->sign;
if (t->precision > t->min_len && check == 1)
{
ft_printchar(t, '0', (t->precision - nblen));
ft_nbr_fd_u(t, nb, 1);
}
}
void ft_print_nbr(t_tab *t, unsigned int nb, int nb_len)
{
int variable;
int check;
check = 1;
if (nb == 0 && t->precision == 0)
check = 0;
if (nb == 0 && t->precision == 0)
nb_len = 0;
if (t->sign == 1 && t->precision > t->min_len)
ft_putchar_fd_printf(t, '-', 1);
ft_print_nbr2(t, nb, nb_len, &variable);
if (t->precision < 0 && t->zero == 1 && t->minus == 0 && check == 1)
{
t->sign == 1 ? ft_putchar_fd_printf(t, '-', 1) : write(1, "", 0);
ft_printchar(t, '0', (t->min_len - nb_len - t->sign));
ft_nbr_fd_u(t, nb, 1);
}
else if (t->min_len >= t->precision)
{
ft_print_nbr3(t, variable, check, nb_len);
if (check == 1)
ft_nbr_fd_u(t, nb, 1);
if (t->minus == 1)
ft_printchar(t, ' ', (variable));
}
}
void ft_print_nbr3(t_tab *t, int variable, int check, int nb_len)
{
if (t->minus == 0)
ft_printchar(t, ' ', (variable));
if (t->sign == 1)
ft_putchar_fd_printf(t, '-', 1);
if (check == 1)
ft_printchar(t, '0', (t->precision - nb_len));
}