-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathint_print.c
122 lines (112 loc) · 2.85 KB
/
int_print.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
122
#include "ft_printf.h"
void ft_putnbr(t_flag *ans, long long int n, int mode, int *return_num)
{
char num;
if (ans->acc == 0 && ans->field > 0 && n == 0)
*return_num += write(1, " ", 1);
else if (ans->acc == 0 && n == 0)
return ;
else
{
if (n < 0)
{
if (mode == 1)
*return_num += write(1, "-", 1);
n *= -1;
}
if (n >= 10)
ft_putnbr(ans, n / 10, mode, return_num);
num = n % 10 + '0';
*return_num += write(1, &num, 1);
}
}
void ft_write_field(t_flag *ans, int mode, int flag, int *return_num)
{
int i;
i = 0;
if ((mode == 0 || mode == 1)
&& (ans->field > ans->acc && ans->acc > ans->putlen))
{
if (flag == 0)
ans->field -= ans->acc;
if (flag == 1)
ans->field -= (ans->acc + 1);
}
else if (mode == 0 && ans->acc > ans->field)
ans->field = 0;
else if (mode == 0 && ans->field > ans->putlen)
ans->field -= ans->putlen;
else if (mode == 1 && (ans->field > ans->putlen && ans->acc < ans->field))
ans->field -= ans->putlen;
else if (mode == 2 && ans->flag[1]
&& (ans->field > ans->acc && ans->acc > ans->putlen))
ans->field -= (ans->acc + ans->putlen);
else if (mode == 2 && ans->field > ans->putlen)
ans->field -= ans->putlen;
else
ans->field = -1;
while (i++ < ans->field)
*return_num += write(1, " ", 1);
}
void ft_write_zero(t_flag *ans, int mode, int flag, int *return_num)
{
int i;
i = 0;
if (flag == 1)
*return_num += write(1, "-", 1);
if ((mode == 0 || mode == 1 ) && ans->acc > ans->putlen)
{
if (flag == 0)
ans->acc -= ans->putlen;
else if (flag == 1)
ans->acc -= (ans->putlen - 1);
while (i++ < ans->acc)
*return_num += write(1, "0", 1);
if (mode == 0)
ans->putlen += ans->acc;
}
else if ((mode == 0 && ans->flag[0] && !ans->flag[1])
|| (mode == 1 && ans->field > ans->acc && !ans->flag[1]))
{
ans->field -= ans->putlen;
while (i++ < ans->field)
*return_num += write(1, "0", 1);
}
}
void ft_write_zero_acc(t_flag *ans, int *return_num)
{
int i;
i = 0;
ans->acc -= ans->putlen;
if (!ans->flag[1])
ans->field -= ans->acc;
while (i++ < ans->acc)
*return_num += write(1, "0", 1);
}
void int_print(va_list *ap, t_flag *ans, int *return_num)
{
long long num;
int flag;
flag = 0;
num = va_arg(*ap, int);
ans->putlen = int_count(num);
if (num < 0 && ((ans->flag[0] && ans->field > ans->putlen) \
|| (ans->acc > ans->putlen)))
flag = 1;
if (!ans->flag[1] && !ans->flag[0])
ft_write_field(ans, 0, flag, return_num);
if (!ans->flag[1] && ans->flag[0] && ans->acc != -1)
ft_write_field(ans, 1, flag, return_num);
if (flag == 1)
ft_write_zero(ans, 0, flag, return_num);
else if (ans->flag[0])
ft_write_zero(ans, 1, flag, return_num);
else if (ans->acc > ans->putlen)
ft_write_zero_acc(ans, return_num);
if (flag == 1)
ft_putnbr(ans, num, 0, return_num);
else
ft_putnbr(ans, num, 1, return_num);
if (ans->flag[1])
ft_write_field(ans, 2, flag, return_num);
}