From fd528b40f3ee669e92a5d284b0f200829d7f125a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Luk=C3=A1=C5=A1=20Ji=C5=99i=C5=A1t=C4=9B?= Date: Wed, 28 Jun 2023 06:57:25 +0200 Subject: [PATCH] Finalized Ex. 05. --- ex05/headers/operators.h | 12 ++++++++ ex05/headers/utils.h | 10 ++++++ ex05/sources/.main.c.swp | Bin 0 -> 12288 bytes ex05/sources/.utils.c.swp | Bin 0 -> 12288 bytes ex05/{ => sources}/main.c | 49 +++++++---------------------- ex05/sources/operators.c | 28 +++++++++++++++++ ex05/sources/utils.c | 63 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 124 insertions(+), 38 deletions(-) create mode 100644 ex05/headers/operators.h create mode 100644 ex05/headers/utils.h create mode 100644 ex05/sources/.main.c.swp create mode 100644 ex05/sources/.utils.c.swp rename ex05/{ => sources}/main.c (61%) create mode 100644 ex05/sources/operators.c create mode 100644 ex05/sources/utils.c 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 0000000000000000000000000000000000000000..9de212342776efde577de89042e0e4b1b45025dc GIT binary patch literal 12288 zcmeI&OKclO0LJlY5wvb7AV3rZt)@mKwsHJQTdJ0nM?*!V1_eT@qSTm;y-v1qcBA!f zng?kSaB2w@Due_Ti1I#wDg-K^Ai)JFh{p+mK)f$VMdi(*7ygsjiJfef1E->~^jq6A zv-9{o>&VLXrgpS<$c>4N;BkQvyQZEfoQSuJHCu!j7|mO%c(F%^WqTdQm|0q+;a=5Y zvdt_Hc+OC=({=h>V{kONF4O56eZHC8n(MU6dCOL>(&?l*l3JfEJHDGY%gI5*vJ?45 z2GG?y0i8f_0=`#l-ndev%;~*w*H+I9q4s>HHZbT9ZT!G869Lw<6av}c2_xKK<;A6anH}L|V$00m|d$12B z4B!sz#7$^J25GFpYBXR4{#ho(-}nhX;xl}Tw=sp6@FFImA36b@fKK3yNnk>RT*dT! z*Ork~bh8NUaV^h`q~kKYYa*a7pe&%s3MdOG(#_^a9}rSNktJa=G0$XZN}@-w|f1Y}S<)eLH8kZjUpRvkKYBuuW}ohI?AFY9iF6Wovy+db*}E8QEug zx#`z4r)8!qi&m+WE2@wpm9VCRt=q?HQjC;W39XCr`+?9ykytg{4cTaflDOocPFA(k z<5XU&InTXJZx-W1HBomltMjDlIxh45hAU$+wIYU?`}8@pIuK!ISz;rr=aZ=;I&c8to_P-orJy?wh{#js-)LNx)Z#hfOV zx+O)b+l+B%|H!GG!h4lYr!q;_KUvrs!=ev4#X3zoZ}MXas`>t4{Jxvm8P zENF3cU5lF8tJyNyo^)32jiy~G8jcIo_6NBHOqV_Ca^Z#McHYdV%qc1m7Q?FJBE-r# zGJB59kR!9;SQ#lE&?k58T6VtV7fc!UJ*!ks^oMJTs-xEzU+dde*()UaxuHLq%v_U5 RZAfHNnHHHzZCc+V{sp531cU$p literal 0 HcmV?d00001 diff --git a/ex05/sources/.utils.c.swp b/ex05/sources/.utils.c.swp new file mode 100644 index 0000000000000000000000000000000000000000..6f13ea4cdc04b9e0223a25d15119811320179f0d GIT binary patch literal 12288 zcmeI&O=uit9LMpe-o~bBeF1O!WRqrgv)P^9jZLG?Rz*=mjpkxeiCs6d(`4v&cbS=O zO;h#IDtZtEL8+Xa3Lg z{GLgnWM*=^Cnoh+wxIZIQR<7+$18UYPpF%BDz$Ik^-c47pGm(KO_mRN)pa^PZ3;T~ zc;UXN-f*UZ`fO0%KkwXHm~(>pURgcAPGnUl7J*22}lBxfFvLZNCJ|8Bp?a=2NNh)lsdOjsn2_u zJb(XR{Qv(?o0a+zmvI5-@fP01F&x1>4&ngzU>XnMLEMQVQn(3!_A2!?zQSc(!g-v) z%XkTg&_E6Q@d$Qf5CiB(FM9C9CZ(?83NGR!T)=6Z!b!Y=7jYC%U>C+QfIi%ae^~b~ z_!(cq#*SA2_aa1kHl1H6xS@HSq>D>#ZHXrPYAF^vh_ zk5Ozx0)5zwUi`uS{EqMO9X`b;ID-#y0`KBA9K&;X79j$Zum=xd7sjw1TQPv^umyi# ztJGh(iYxdGmv9beaT4$0bsWbFcpgnen1OsG0ZBj-kOU+FNk9@X2|T5&z>AteP47_F z!N9yzy0R%+rEQfa>9!(D31sa+mY%U>=+D;lkPf^s#UscCE1eFbfMcG_P^XJ}h*O=> zcFtaG94hL3j*|tR**U-J>7*Z)D*mh=B~9m4ikq!g;iAsu&29c(9bZeQb+M@Jj7^{R zy>Wfhn6~xd!>!>ueLAyQ8Ypa(U9U4xJPh+;5HsJRW@5_Xwd;5Sw3`)8J9PW)u z<*4o_-MMlQkCj^fE0<)Idc3GzJvhh@j_liPf{E|domIv+yWMxX@xpYxhNL|mt*kR0 z*r~-z7L{7dSfvtk=3j!yOXi35V4Ui(&M$6Ge9kJ(Jkz>16X>n2y|AoC!1_uwB~7zK zv$-iw-Zbnqn?~vj%JQnA*V^mlAV~8CO|m zsa8Co$@4(nuPmD~Q#ZqRMQUc>N}@2Vd9KpwcI{;kPO7_<7X #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); +} -- 2.30.2