Solution to Ex. 00 to 04.
authorLukáš Jiřiště <ljiriste@student.42prague.com>
Tue, 27 Jun 2023 20:55:07 +0000 (22:55 +0200)
committerLukáš Jiřiště <ljiriste@student.42prague.com>
Tue, 27 Jun 2023 20:55:07 +0000 (22:55 +0200)
Part of solution to Ex. 05.

ex00/ft_foreach.c [new file with mode: 0644]
ex01/ft_map.c [new file with mode: 0644]
ex02/ft_any.c [new file with mode: 0644]
ex03/ft_count_if.c [new file with mode: 0644]
ex04/ft_is_sort.c [new file with mode: 0644]
ex05/main.c [new file with mode: 0644]

diff --git a/ex00/ft_foreach.c b/ex00/ft_foreach.c
new file mode 100644 (file)
index 0000000..8638acc
--- /dev/null
@@ -0,0 +1,11 @@
+//27.06.2023 20:28
+
+void   ft_foreach(int *tab, int length, void(*f)(int))
+{
+       int     i;
+
+       i = 0;
+       while (i <= length)
+               f(tab[i++]);
+       return ;
+}
diff --git a/ex01/ft_map.c b/ex01/ft_map.c
new file mode 100644 (file)
index 0000000..c5655fa
--- /dev/null
@@ -0,0 +1,20 @@
+//27.06.2023 20:30
+
+#include <stdlib.h>
+
+int    *ft_map(int *tab, int length, int(*f)(int))
+{
+       int     i;
+       int     *res;
+
+       res = malloc(length * sizeof(int));
+       if (!res)
+               return (NULL);
+       i = 0;
+       while (i < length)
+       {
+               res[i] = f(tab[i]);
+               ++i;
+       }
+       return (res);
+}
diff --git a/ex02/ft_any.c b/ex02/ft_any.c
new file mode 100644 (file)
index 0000000..4946797
--- /dev/null
@@ -0,0 +1,9 @@
+//27.06.2023 20:39
+
+int    ft_any(char **tab, int(*f)(char*))
+{
+       while (*tab)
+               if(f(*(tab++)))
+                       return (1);
+       return (0);
+}
diff --git a/ex03/ft_count_if.c b/ex03/ft_count_if.c
new file mode 100644 (file)
index 0000000..d4179e2
--- /dev/null
@@ -0,0 +1,14 @@
+//27.06.2023 20:41
+
+int    ft_count_if(char **tab, int length, int(*f)(char*))
+{
+       int     i;
+       int     count;
+
+       count = 0;
+       i = 0;
+       while (i < length)
+               if (f(tab[i++]))
+                       ++count;
+       return (count);
+}
diff --git a/ex04/ft_is_sort.c b/ex04/ft_is_sort.c
new file mode 100644 (file)
index 0000000..c932987
--- /dev/null
@@ -0,0 +1,15 @@
+//27.06.2023 20:48
+
+int    ft_is_sort(int *tab, int length, int(*f)(int, int))
+{
+       int     i;
+
+       i = 0;
+       while (i < length - 1)
+       {
+               if (f(tab[i], tab[i + 1]) > 0)
+                       return (0);
+               ++i;
+       }
+       return (1);
+}
diff --git a/ex05/main.c b/ex05/main.c
new file mode 100644 (file)
index 0000000..40a2763
--- /dev/null
@@ -0,0 +1,95 @@
+//27.06.2023 20:53
+
+#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 ;
+}
+
+enum operator_id
+{
+       Invalid,
+       Add,
+       Sub,
+       Mult,
+       Div,
+       Mod,
+};
+
+enum operator_id       get_op_id(char *str)
+{
+       if (str[2] != '\0')
+               return (Invalid);
+       if (*str == '+')
+               return (Add);
+       if (*str == '-')
+               return (Sub);
+       if (*str == '*')
+               return (Mult);
+       if (*str == '/')
+               return (Div);
+       if (*str == '%')
+               return (Mod);
+       return (Invalid);
+}
+
+void   fill_f_arr(int (f_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[Div] = &ft_div;
+       fun_arr[Mod] = &ft_mod;
+       return ;
+}
+
+int    main(int argc, char **argv)
+{
+       int     val1;
+       int     val2;
+       int     op_id;
+       int     (f_arr[6])(int, int);
+
+       if (argc != 4)
+               return (0);
+       fill_f_arr(&f_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)
+               print_error("Stop : division by zero");
+       else if (val2 == 0 && op_id == Mod)
+               print_error("Stop : modulo by zero");
+       else if (op_id != Invalid)
+               f_arr[op_id](val1, val2);
+       write(1, "\n", 1);
+       return (0);
+}