From c454868026d75c999f63363c2bae31e9c6d498ce Mon Sep 17 00:00:00 2001 From: =?utf8?q?Luk=C3=A1=C5=A1=20Ji=C5=99i=C5=A1t=C4=9B?= Date: Tue, 27 Jun 2023 22:55:07 +0200 Subject: [PATCH] Solution to Ex. 00 to 04. Part of solution to Ex. 05. --- ex00/ft_foreach.c | 11 ++++++ ex01/ft_map.c | 20 ++++++++++ ex02/ft_any.c | 9 +++++ ex03/ft_count_if.c | 14 +++++++ ex04/ft_is_sort.c | 15 ++++++++ ex05/main.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 164 insertions(+) create mode 100644 ex00/ft_foreach.c create mode 100644 ex01/ft_map.c create mode 100644 ex02/ft_any.c create mode 100644 ex03/ft_count_if.c create mode 100644 ex04/ft_is_sort.c create mode 100644 ex05/main.c diff --git a/ex00/ft_foreach.c b/ex00/ft_foreach.c new file mode 100644 index 0000000..8638acc --- /dev/null +++ b/ex00/ft_foreach.c @@ -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 index 0000000..c5655fa --- /dev/null +++ b/ex01/ft_map.c @@ -0,0 +1,20 @@ +//27.06.2023 20:30 + +#include + +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 index 0000000..4946797 --- /dev/null +++ b/ex02/ft_any.c @@ -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 index 0000000..d4179e2 --- /dev/null +++ b/ex03/ft_count_if.c @@ -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 index 0000000..c932987 --- /dev/null +++ b/ex04/ft_is_sort.c @@ -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 index 0000000..40a2763 --- /dev/null +++ b/ex05/main.c @@ -0,0 +1,95 @@ +//27.06.2023 20:53 + +#include +#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); +} -- 2.30.2