Finalized Ex. 05.
authorLukáš Jiřiště <ljiriste@student.42prague.com>
Wed, 28 Jun 2023 04:57:25 +0000 (06:57 +0200)
committerLukáš Jiřiště <ljiriste@student.42prague.com>
Wed, 28 Jun 2023 04:57:25 +0000 (06:57 +0200)
ex05/headers/operators.h [new file with mode: 0644]
ex05/headers/utils.h [new file with mode: 0644]
ex05/sources/.main.c.swp [new file with mode: 0644]
ex05/sources/.utils.c.swp [new file with mode: 0644]
ex05/sources/main.c [moved from ex05/main.c with 61% similarity]
ex05/sources/operators.c [new file with mode: 0644]
ex05/sources/utils.c [new file with mode: 0644]

diff --git a/ex05/headers/operators.h b/ex05/headers/operators.h
new file mode 100644 (file)
index 0000000..a8691e0
--- /dev/null
@@ -0,0 +1,12 @@
+//28.06.2023 06:21
+
+#ifndef OPERATORS_H
+# define OPERATORS_H
+
+int    ft_add(int n1, int n2);
+int    ft_sub(int n1, int n2);
+int    ft_mul(int n1, int n2);
+int    ft_div(int n1, int n2);
+int    ft_mod(int n1, int n2);
+
+#endif
diff --git a/ex05/headers/utils.h b/ex05/headers/utils.h
new file mode 100644 (file)
index 0000000..2804084
--- /dev/null
@@ -0,0 +1,10 @@
+//28.06.2023 06:47
+
+#ifndef UTILS_H
+# define UTILS_H
+
+void   print_error(char *str);
+void   print_nbr(int nbr);
+int            ft_atoi(char *str);
+
+#endif
diff --git a/ex05/sources/.main.c.swp b/ex05/sources/.main.c.swp
new file mode 100644 (file)
index 0000000..9de2123
Binary files /dev/null and b/ex05/sources/.main.c.swp differ
diff --git a/ex05/sources/.utils.c.swp b/ex05/sources/.utils.c.swp
new file mode 100644 (file)
index 0000000..6f13ea4
Binary files /dev/null and b/ex05/sources/.utils.c.swp differ
similarity index 61%
rename from ex05/main.c
rename to ex05/sources/main.c
index 40a2763d6a7c2d9e1a2db07693950221811776ff..a90e8001e8870c7ed83d30d05ed181d973b03d54 100644 (file)
@@ -2,55 +2,28 @@
 
 #include <unistd.h>
 #include "operators.h"
-
-void   print_error(char *str)
-{
-       while (*str)
-               write(2, str++, 1);
-       return ;
-}
-
-void   print_nbr(int nbr)
-{
-       char    digit;
-
-       if (nbr < 0)
-       {
-               write(1, "-", 1);
-               if (nbr <= -10)
-                       print_nbr(-(nbr / 10));
-               digit = '0' - nbr % 10;
-       }
-       else
-       {
-               if (nbr >= 10)
-                       print_nbr(nbr / 10);
-               digit = '0' + nbr % 10;
-       }
-       write(1, &digit, 1);
-       return ;
-}
+#include "utils.h"
 
 enum operator_id
 {
        Invalid,
        Add,
        Sub,
-       Mult,
+       Mul,
        Div,
        Mod,
 };
 
 enum operator_id       get_op_id(char *str)
 {
-       if (str[2] != '\0')
+       if (str[1] != '\0')
                return (Invalid);
        if (*str == '+')
                return (Add);
        if (*str == '-')
                return (Sub);
        if (*str == '*')
-               return (Mult);
+               return (Mul);
        if (*str == '/')
                return (Div);
        if (*str == '%')
@@ -58,12 +31,12 @@ enum operator_id    get_op_id(char *str)
        return (Invalid);
 }
 
-void   fill_f_arr(int (f_arr[6])(int, int))
+void   fill_f_arr(int (*fun_arr[6])(int, int))
 {
        fun_arr[Invalid] = NULL;
        fun_arr[Add] = &ft_add;
        fun_arr[Sub] = &ft_sub;
-       fun_arr[Mul] = &ft_mult;
+       fun_arr[Mul] = &ft_mul;
        fun_arr[Div] = &ft_div;
        fun_arr[Mod] = &ft_mod;
        return ;
@@ -74,14 +47,12 @@ int main(int argc, char **argv)
        int     val1;
        int     val2;
        int     op_id;
-       int     (f_arr[6])(int, int);
+       int     (*fun_arr[6])(int, int);
 
        if (argc != 4)
                return (0);
-       fill_f_arr(&f_arr);
+       fill_f_arr(fun_arr);
        op_id = get_op_id(argv[2]);
-       if (op_id == Invalid)
-               write(1, "0", 2);
        val1 = ft_atoi(argv[1]);
        val2 = ft_atoi(argv[3]);
        if (val2 == 0 && op_id == Div)
@@ -89,7 +60,9 @@ int   main(int argc, char **argv)
        else if (val2 == 0 && op_id == Mod)
                print_error("Stop : modulo by zero");
        else if (op_id != Invalid)
-               f_arr[op_id](val1, val2);
+               print_nbr(fun_arr[op_id](val1, val2));
+       else
+               print_nbr(0);
        write(1, "\n", 1);
        return (0);
 }
diff --git a/ex05/sources/operators.c b/ex05/sources/operators.c
new file mode 100644 (file)
index 0000000..4acc323
--- /dev/null
@@ -0,0 +1,28 @@
+//28.06.2023 6:23
+
+#include "operators.h"
+
+int    ft_add(int n1, int n2)
+{
+       return (n1 + n2);
+}
+
+int ft_sub(int n1, int n2)
+{
+       return (n1 - n2);
+}
+
+int ft_mul(int n1, int n2)
+{
+       return (n1 * n2);
+}
+
+int ft_div(int n1, int n2)
+{
+       return (n1 / n2);
+}
+
+int    ft_mod(int n1, int n2)
+{
+       return (n1 % n2);
+}
diff --git a/ex05/sources/utils.c b/ex05/sources/utils.c
new file mode 100644 (file)
index 0000000..d47bbab
--- /dev/null
@@ -0,0 +1,63 @@
+//28.06.2023 6:37
+
+#include <unistd.h>
+#include "utils.h"
+
+void   print_error(char *str)
+{
+       while (*str)
+               write(2, str++, 1);
+       return ;
+}
+
+void   print_nbr(int nbr)
+{
+       char    digit;
+
+       if (nbr < 0)
+       {
+               write(1, "-", 1);
+               if (nbr <= -10)
+                       print_nbr(-(nbr / 10));
+               digit = '0' - nbr % 10;
+       }
+       else
+       {
+               if (nbr >= 10)
+                       print_nbr(nbr / 10);
+               digit = '0' + nbr % 10;
+       }
+       write(1, &digit, 1);
+       return ;
+}
+
+int    is_space(char c)
+{
+       return (c == ' ' || ('\t' <= c && c <= '\r'));
+}
+
+int is_digit(char c)
+{
+       return ('0' <= c && c <= '9');
+}
+
+int    ft_atoi(char *str)
+{
+       int     res;
+       int     sign;
+
+       res = 0;
+       sign = 1;
+       while (is_space(*str))
+               ++str;
+       while (*str == '+' || *str == '-')
+               if (*str++ == '-')
+                       sign *= -1;
+       while (is_digit(*str))
+       {
+               res *= 10;
+               res += *str - '0';
+               ++str;
+       }
+       return (sign * res);
+}