From 23ad79e8b41c25bb4992d103d29a17612a52e351 Mon Sep 17 00:00:00 2001 From: Charles Date: Sun, 21 Jul 2019 15:26:32 +0200 Subject: c10 done, c11 on going, rush02 probably finished, bsq start --- rush02/ex00/convert.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 rush02/ex00/convert.c (limited to 'rush02/ex00/convert.c') 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 +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/20 11:14:59 by yiacono #+# #+# */ +/* Updated: 2019/07/21 15:03:02 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "include.h" +#include + +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); +} -- cgit