diff options
| author | Charles <sircharlesaze@gmail.com> | 2019-07-21 15:26:32 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2019-07-21 15:26:32 +0200 |
| commit | 23ad79e8b41c25bb4992d103d29a17612a52e351 (patch) | |
| tree | 9de3cde07cc38e59f08885171e9f99eeab8ab71b /rush02/ex00/convert.c | |
| parent | 8b6e91bdb56bc01a588718472546f2a88e750b48 (diff) | |
| download | piscine-23ad79e8b41c25bb4992d103d29a17612a52e351.tar.gz piscine-23ad79e8b41c25bb4992d103d29a17612a52e351.tar.bz2 piscine-23ad79e8b41c25bb4992d103d29a17612a52e351.zip | |
c10 done, c11 on going, rush02 probably finished, bsq start
Diffstat (limited to 'rush02/ex00/convert.c')
| -rw-r--r-- | rush02/ex00/convert.c | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/rush02/ex00/convert.c b/rush02/ex00/convert.c new file mode 100644 index 0000000..6119f9c --- /dev/null +++ b/rush02/ex00/convert.c @@ -0,0 +1,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); +} |
