blob: 6119f9cb0ef0c51e389df47392a86ed56a688fce (
plain)
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
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* convert.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yiacono <yiacono@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/07/20 11:14:59 by yiacono #+# #+# */
/* Updated: 2019/07/21 15:03:02 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "include.h"
#include <unistd.h>
static int check_arg(t_max_nbr nb, t_dict dict)
{
if (nb < 0)
return (-1);
if (nb == 0)
{
while (dict->key > 0)
dict++;
ft_putstr(dict->value);
return (0);
}
if ((nb / dict->key) >= 1000)
return (-1);
return (0);
}
static void effective_convert(t_max_nbr nb, t_dict dict)
{
t_max_nbr div_result;
while (nb > 0)
{
div_result = nb / dict->key;
if (div_result)
{
if (div_result == 1 && dict->key >= 100)
{
effective_convert(div_result, dict);
ft_putstr(" ");
}
else if (div_result > 1)
{
effective_convert(div_result, dict);
ft_putstr(" ");
}
ft_putstr(dict->value);
nb = nb % dict->key;
if (nb)
ft_putstr(" ");
}
dict++;
}
}
int convert(t_max_nbr nb, t_dict dict)
{
if (check_arg(nb, dict) < 0)
return (-1);
effective_convert(nb, dict);
ft_putstr("\n");
return (0);
}
|