diff --git a/sources/operation/ft_classic_op.c b/sources/operation/ft_classic_op.c new file mode 100644 index 00000000..e12ade5b --- /dev/null +++ b/sources/operation/ft_classic_op.c @@ -0,0 +1,62 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_classic_op.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/30 15:33:53 by rparodi #+# #+# */ +/* Updated: 2024/05/30 16:07:58 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "operation.h" + +t_error ft_add(t_i32 first, t_i32 second, t_i64 *result) +{ + (*result) = first + second; + if ((*result) - first != second) + return (ERROR); + else + return (NO_ERROR); +} + +t_error ft_subtract(t_i32 first, t_i32 second, t_i64 *result) +{ + (*result) = first - second; + if ((*result) + first != second) + return (ERROR); + else + return (NO_ERROR); +} + +t_error ft_multiply(t_i32 first, t_i32 second, t_i64 *result) +{ + if (first == 0 || second == 0) + return (0); + (*result) = first * second; + if ((*result) / first != second) + return (ERROR); + else + return (NO_ERROR); +} + +t_error ft_divide(t_i32 first, t_i32 second, t_i64 *result) +{ + if (second == 0) + return (ERROR); + (*result) = first / second; + if ((*result) * first != second) + return (ERROR); + else + return (NO_ERROR); +} + +t_error ft_modulo(t_i32 first, t_i32 second, t_i64 *result) +{ + if (second == 0) { + return (ERROR); + } + *result = first % second; + return (NO_ERROR); +} diff --git a/sources/operation/ft_hard_op.c b/sources/operation/ft_hard_op.c new file mode 100644 index 00000000..f757a8e0 --- /dev/null +++ b/sources/operation/ft_hard_op.c @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_hard_op.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/30 15:51:55 by rparodi #+# #+# */ +/* Updated: 2024/05/30 16:04:33 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "operation.h" +t_error ft_power(t_i32 base, t_i32 exponent, t_i64 *result) +{ + t_i32 i; + t_i64 tmp; + + i = 0; + tmp = base; + if (exponent < 0) + return (ERROR); + while (i < exponent) + { + (*result) *= base; + if ((*result) / base != tmp) + return (ERROR); + tmp = (*result); + i++; + } + return (NO_ERROR); +} + +t_error ft_decrimented(t_i64 *value) +{ + t_i64 tmp; + + tmp = *value; + (*value)--; + if ((*value) + 1 != tmp) + return (ERROR); + return (NO_ERROR); +} + +t_error ft_incrimented(t_i64 *value) +{ + t_i64 tmp; + + tmp = *value; + (*value)++; + if ((*value) - 1 != tmp) + return (ERROR); + return (NO_ERROR); +} diff --git a/sources/operation/operation.h b/sources/operation/operation.h new file mode 100644 index 00000000..7ead4484 --- /dev/null +++ b/sources/operation/operation.h @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* operation.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/30 16:03:29 by rparodi #+# #+# */ +/* Updated: 2024/05/30 16:05:30 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef OPERATION_H +# define OPERATION_H + +# include "minishell.h" +# include "me/types.h" + +t_error ft_add(t_i32 first, t_i32 second, t_i64 *result); +t_error ft_subtract(t_i32 first, t_i32 second, t_i64 *result); +t_error ft_multiply(t_i32 first, t_i32 second, t_i64 *result); +t_error ft_divide(t_i32 first, t_i32 second, t_i64 *result); +t_error ft_modulo(t_i32 first, t_i32 second, t_i64 *result); +t_error ft_power(t_i32 base, t_i32 exponent, t_i64 *result); +t_error ft_incrimented(t_i64 *value); +t_error ft_decrimented(t_i64 *value); + +#endif +