From: Lukáš Jiřiště Date: Wed, 28 Jun 2023 04:57:25 +0000 (+0200) Subject: Finalized Ex. 05. X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=fd528b40f3ee669e92a5d284b0f200829d7f125a;p=42%2FC_11.git Finalized Ex. 05. --- diff --git a/ex05/headers/operators.h b/ex05/headers/operators.h new file mode 100644 index 0000000..a8691e0 --- /dev/null +++ b/ex05/headers/operators.h @@ -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 index 0000000..2804084 --- /dev/null +++ b/ex05/headers/utils.h @@ -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 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 index 0000000..6f13ea4 Binary files /dev/null and b/ex05/sources/.utils.c.swp differ diff --git a/ex05/main.c b/ex05/sources/main.c similarity index 61% rename from ex05/main.c rename to ex05/sources/main.c index 40a2763..a90e800 100644 --- a/ex05/main.c +++ b/ex05/sources/main.c @@ -2,55 +2,28 @@ #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 ; -} +#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 index 0000000..4acc323 --- /dev/null +++ b/ex05/sources/operators.c @@ -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 index 0000000..d47bbab --- /dev/null +++ b/ex05/sources/utils.c @@ -0,0 +1,63 @@ +//28.06.2023 6:37 + +#include +#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); +}