diff options
| author | Charles <sircharlesaze@gmail.com> | 2019-07-17 19:30:52 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2019-07-17 19:30:52 +0200 |
| commit | 880102ae9358db130ef67cc9a7177a1e1de76875 (patch) | |
| tree | b5929722cf123e7c1701f4bc164a905f79510bf5 /c11/ex05 | |
| parent | dcf89e9366538c70fc08ceaabd5b770f5d5b890d (diff) | |
| download | piscine-880102ae9358db130ef67cc9a7177a1e1de76875.tar.gz piscine-880102ae9358db130ef67cc9a7177a1e1de76875.tar.bz2 piscine-880102ae9358db130ef67cc9a7177a1e1de76875.zip | |
c10 tail stuff, c11 start, c12/c13 files
Diffstat (limited to 'c11/ex05')
| -rw-r--r-- | c11/ex05/Makefile | 37 | ||||
| -rwxr-xr-x | c11/ex05/do-op | bin | 0 -> 8956 bytes | |||
| -rw-r--r-- | c11/ex05/helper.c | 76 | ||||
| -rw-r--r-- | c11/ex05/include.h | 42 | ||||
| -rw-r--r-- | c11/ex05/main.c | 48 | ||||
| -rw-r--r-- | c11/ex05/operators.c | 38 | ||||
| -rw-r--r-- | c11/ex05/parse.c | 39 |
7 files changed, 280 insertions, 0 deletions
diff --git a/c11/ex05/Makefile b/c11/ex05/Makefile new file mode 100644 index 0000000..0aca71b --- /dev/null +++ b/c11/ex05/Makefile @@ -0,0 +1,37 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2019/07/17 07:55:23 by cacharle #+# #+# # +# Updated: 2019/07/17 08:15:25 by cacharle ### ########.fr # +# # +# **************************************************************************** # + +OUT = do-op +CC = gcc +FLAGS = -Wall -Wextra #-Werror +SRC = main.c operators.c parse.c helper.c +OBJ = $(SRC:.c=.o) + +.PHONY: all +all : $(OUT) + +$(OUT): $(OBJ) + $(CC) $(FLAGS) -o $@ $^ + +%.o: %.c include.h + $(CC) $(FLAGS) -c $< -o $@ + +.PHONY: clean +clean: + rm -f $(OBJ) + +.PHONY: fclean +fclean: clean + rm -f $(OUT) + +.PHONY: re +re: fclean all diff --git a/c11/ex05/do-op b/c11/ex05/do-op Binary files differnew file mode 100755 index 0000000..90f13df --- /dev/null +++ b/c11/ex05/do-op diff --git a/c11/ex05/helper.c b/c11/ex05/helper.c new file mode 100644 index 0000000..8f370de --- /dev/null +++ b/c11/ex05/helper.c @@ -0,0 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* helper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/17 07:53:07 by cacharle #+# #+# */ +/* Updated: 2019/07/17 07:53:59 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <unistd.h> +#include "include.h" + +void ft_putchar(char c) +{ + write(1, &c, 1); +} + +void ft_putnbr(int nb) +{ + unsigned int p_nb; + + p_nb = nb; + if (nb < 0) + { + write(1, "-", 1); + p_nb = -nb; + } + if (p_nb > 9) + ft_putnbr(p_nb / 10); + ft_putchar(p_nb % 10 + '0'); +} + +int pow10(int exponent) +{ + int accumulator; + + accumulator = 1; + while (exponent > 0) + { + accumulator *= 10; + exponent--; + } + return (accumulator); +} + +int ft_atoi(char *str) +{ + int is_negative; + int nb; + int i; + int j; + + while (*str == ' ' || *str == '\t' || *str == '\n' + || *str == '\v' || *str == '\f' || *str == '\r') + str++; + is_negative = 0; + while (*str == '-' || *str == '+') + { + if (*str == '-') + is_negative = !is_negative; + str++; + } + nb = 0; + i = 0; + while (str[i] >= '0' && str[i] <= '9') + i++; + j = 0; + while (str[j] >= '0' && str[j] <= '9') + nb += pow10(--i) * (str[j++] - '0'); + if (is_negative) + nb = -nb; + return (nb); +} diff --git a/c11/ex05/include.h b/c11/ex05/include.h new file mode 100644 index 0000000..49381a3 --- /dev/null +++ b/c11/ex05/include.h @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* include.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/17 07:38:10 by cacharle #+# #+# */ +/* Updated: 2019/07/17 08:15:39 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef INCLUDE_H +# define INCLUDE_H + +/* +** operators.c +*/ + +int add(int x, int y); +int subtract(int x, int y); +int multiply(int x, int y); +int divide(int x, int y); +int modulo(int x, int y); + +/* +** parse.c +*/ + +int parse(int argc, char **argv); +//int make_operation(int x, int y, int (*operator)(int, int)) + +/* +** helper.c +*/ + +void ft_putchar(char c); +void ft_putnbr(int nb); +int pow10(int exponent); +int ft_atoi(char *str); + +#endif diff --git a/c11/ex05/main.c b/c11/ex05/main.c new file mode 100644 index 0000000..e234053 --- /dev/null +++ b/c11/ex05/main.c @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/17 07:29:52 by cacharle #+# #+# */ +/* Updated: 2019/07/17 16:37:31 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <unistd.h> +#include "include.h" + +int main(int argc, char **argv) +{ + int x; + int y; + int operator_index; + int (*operators[5])(int, int); + + operators[0] = &add; + operators[1] = &subtract; + operators[2] = &multiply; + operators[3] = ÷ + operators[4] = &modulo; + operator_index = parse(argc, argv); + if (operator_index == -1) + return (0); + if (operator_index == -2) + ft_putchar('0'); + else + { + x = ft_atoi(argv[1]); + y = ft_atoi(argv[3]); + if (operator_index == 3 && y == 0) + { + + } + if (operator_index == 3 && y == 0) + { + } + ft_putnbr((*operators[operator_index])(x, y)); + } + ft_putchar('\n'); + return (0); +} diff --git a/c11/ex05/operators.c b/c11/ex05/operators.c new file mode 100644 index 0000000..c138be3 --- /dev/null +++ b/c11/ex05/operators.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* operators.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/17 07:38:59 by cacharle #+# #+# */ +/* Updated: 2019/07/17 08:03:34 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int add(int x, int y) +{ + return (x + y); +} + +int subtract(int x, int y) +{ + + return (x - y); +} + +int multiply(int x, int y) +{ + + return (x * y); +} + +int divide(int x, int y) +{ + return (x / y); +} + +int modulo(int x, int y) +{ + return (x % y); +} diff --git a/c11/ex05/parse.c b/c11/ex05/parse.c new file mode 100644 index 0000000..5b1c531 --- /dev/null +++ b/c11/ex05/parse.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/17 08:05:59 by cacharle #+# #+# */ +/* Updated: 2019/07/17 08:35:50 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int parse(int argc, char **argv) +{ + int i; + + if (argc != 4) + return (-1); + if (argv[2][0] == '+') + i = 0; + else if (argv[2][0] == '-') + i = 1; + else if (argv[2][0] == '*') + i = 2; + else if (argv[2][0] == '/') + i = 3; + else if (argv[2][0] == '%') + i = 4; + else + i = -2; + return (i); +} + +/*int make_operation(int x, int y, int (*operator)(int, int))*/ +/*{*/ + + /*retu*/ + +/*}*/ |
