aboutsummaryrefslogtreecommitdiff
path: root/rush02/ex00/convert.c
diff options
context:
space:
mode:
Diffstat (limited to 'rush02/ex00/convert.c')
-rw-r--r--rush02/ex00/convert.c67
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);
+}