-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloat_adjust.c
118 lines (108 loc) · 2.5 KB
/
float_adjust.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* float_adjust.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jjacobso <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/27 16:15:11 by jjacobso #+# #+# */
/* Updated: 2019/01/27 16:29:17 by jjacobso ### ########.fr */
/* */
/* ************************************************************************** */
#include "printf.h"
void cut(char *str, int dot)
{
char *s;
s = str + ft_strlen(str) - 1;
while (*s == '0')
*s-- = '\0';
if (*s == '.' && !dot)
*s-- = '\0';
}
void pound(char *s, int prec)
{
int dot;
int i;
i = 0;
dot = 0;
while (s[i])
if (s[i] == '.')
{
s++;
if (i)
dot = 1;
}
else if (s[i] == '0')
{
if (dot || i)
i++;
else
s++;
}
else
i++;
while (i < prec)
s[i++] = '0';
s[i] = '\0';
}
void super_hard_adjust(char **s, t_spec *spec, int prec,
int mod)
{
int i;
int del;
char *res;
i = 0;
del = 0;
res = *s;
while (sign_num(res) > prec)
{
while (res[i] == '0' || res[i] == '.')
i++;
del = res[((i) ? (ft_min(prec, sign_num(res)) + i)
: (ft_max(prec, sign_num(res))))];
res[((i) ? (ft_min(prec, sign_num(res)) + i)
: (ft_max(prec, sign_num(res))))] = '\0';
if (del >= '5')
fadjust(&res, mod);
cut(res, check_flag(spec->flags, HASH));
}
*s = res;
}
int hard_adjust(char **str, char *s, int mod)
{
s--;
while (s >= *str && *s >= '9')
*s-- = '0';
if (s < *str && mod)
{
if (!(s = ft_strdup("1"))
|| !(*str = ft_strmerge(2, &s, str)))
exit(0);
}
else if (s < *str)
*(s + 1) = '1';
else
(*s)++;
return (1);
}
int fadjust(char **str, int mod)
{
char *s;
s = *str + ft_strlen(*str);
while (--s >= *str)
if (*s == '.' && s[-1] == '9')
return (hard_adjust(str, s, mod));
else if (*s == '9' && s == *str)
{
*s = '0';
return (hard_adjust(str, s, mod));
}
else if (*s >= '9')
*s = '0';
else if (*s != '.')
{
(*s)++;
return (0);
}
return (0);
}