--- /dev/null
+//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
--- /dev/null
+//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
#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 == '%')
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 ;
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)
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);
}
--- /dev/null
+//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);
+}
--- /dev/null
+//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);
+}