Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # **************************************************************************** #
- # LE - / #
- # / #
- # Makefile .:: .:/ . .:: #
- # +:+:+ +: +: +:+:+ #
- # By: humoyne <marvin@le-101.fr> +:+ +: +: +:+ #
- # #+# #+ #+ #+# #
- # Created: 2018/07/16 13:15:14 by humoyne #+# ## ## #+# #
- # Updated: 2018/07/16 14:29:36 by humoyne ### #+. /#+ ###.fr #
- # / #
- # / #
- # **************************************************************************** #
- NAME = libft.a
- PATH_SRC = ./srcs/
- HEADER = ./includes/
- FLAG = -Wall -Wextra -Werror
- OPTION = -c -I $(HEADER)
- SRC = $(PATH_SRC)ft_putchar.c $(PATH_SRC)ft_putstr.c $(PATH_SRC)ft_strcmp.c \
- $(PATH_SRC)ft_strlen.c $(PATH_SRC)ft_swap.c
- OBJ = ft_putchar.o ft_putstr.o ft_strcmp.o ft_strlen.o ft_swap.o
- all: $(NAME)
- ar rc $(NAME) $(OBJ)
- $(NAME):
- gcc $(FLAG) $(OPTION) $(SRC)
- clean:
- /bin/rm -f $(OBJ)
- fclean: clean
- /bin/rm -f $(NAME)
- re: fclean all
- /bin/rm -f $(NAME) $(OBJ)
- /* ************************************************************************** */
- /* LE - / */
- /* / */
- /* ft_foreach.c .:: .:/ . .:: */
- /* +:+:+ +: +: +:+:+ */
- /* By: humoyne <marvin@le-101.fr> +:+ +: +: +:+ */
- /* #+# #+ #+ #+# */
- /* Created: 2018/07/16 14:33:28 by humoyne #+# ## ## #+# */
- /* Updated: 2018/07/16 15:07:32 by humoyne ### #+. /#+ ###.fr */
- /* / */
- /* / */
- /* ************************************************************************** */
- void ft_foreach(int *tab, int length, void (*f)(int))
- {
- int i;
- i = 0;
- while (i < length)
- {
- f(tab[i]);
- i++;
- }
- }
- /* ************************************************************************** */
- /* LE - / */
- /* / */
- /* ft_map.c .:: .:/ . .:: */
- /* +:+:+ +: +: +:+:+ */
- /* By: humoyne <marvin@le-101.fr> +:+ +: +: +:+ */
- /* #+# #+ #+ #+# */
- /* Created: 2018/07/16 15:12:37 by humoyne #+# ## ## #+# */
- /* Updated: 2018/07/16 15:26:24 by humoyne ### #+. /#+ ###.fr */
- /* / */
- /* / */
- /* ************************************************************************** */
- #include <stdlib.h>
- int *ft_map(int *tab, int length, int (*f)(int))
- {
- int i;
- int *s;
- i = 0;
- if ((s = (int*)malloc(sizeof(int) * length)) == NULL)
- return (NULL);
- while (i < length)
- {
- s[i] = f(tab[i]);
- i++;
- }
- return (s);
- }
- /* ************************************************************************** */
- /* LE - / */
- /* / */
- /* ft_any.c .:: .:/ . .:: */
- /* +:+:+ +: +: +:+:+ */
- /* By: humoyne <marvin@le-101.fr> +:+ +: +: +:+ */
- /* #+# #+ #+ #+# */
- /* Created: 2018/07/16 15:28:13 by humoyne #+# ## ## #+# */
- /* Updated: 2018/07/16 16:03:09 by humoyne ### #+. /#+ ###.fr */
- /* / */
- /* / */
- /* ************************************************************************** */
- int ft_any(char **tab, int (*f)(char*))
- {
- int i;
- i = 0;
- while (tab[i] != '\0')
- {
- if (f(tab[i]) == 1)
- return (1);
- i++;
- }
- return (0);
- }
- /* ************************************************************************** */
- /* LE - / */
- /* / */
- /* ft_count_if.c .:: .:/ . .:: */
- /* +:+:+ +: +: +:+:+ */
- /* By: humoyne <marvin@le-101.fr> +:+ +: +: +:+ */
- /* #+# #+ #+ #+# */
- /* Created: 2018/07/16 16:06:33 by humoyne #+# ## ## #+# */
- /* Updated: 2018/07/16 16:28:03 by humoyne ### #+. /#+ ###.fr */
- /* / */
- /* / */
- /* ************************************************************************** */
- int ft_count_if(char **tab, int (*f)(char*))
- {
- int i;
- int c;
- i = 0;
- c = 0;
- while (tab[i] != '\0')
- {
- if (f(tab[i]))
- c++;
- i++;
- }
- return (c);
- }
- /* ************************************************************************** */
- /* LE - / */
- /* / */
- /* ft_is_sort.c .:: .:/ . .:: */
- /* +:+:+ +: +: +:+:+ */
- /* By: humoyne <marvin@le-101.fr> +:+ +: +: +:+ */
- /* #+# #+ #+ #+# */
- /* Created: 2018/07/16 16:32:22 by humoyne #+# ## ## #+# */
- /* Updated: 2018/07/16 16:48:12 by humoyne ### #+. /#+ ###.fr */
- /* / */
- /* / */
- /* ************************************************************************** */
- int ft_is_sort(int *tab, int length, int (*f)(int, int))
- {
- int i;
- int res;
- i = 0;
- res = 0;
- while (i < length - 1 && res <= 0)
- {
- res = f(tab[i], tab[i + 1]);
- i++;
- }
- res = (res <= 0) ? 1 : 0;
- return (res);
- }
- # **************************************************************************** #
- # LE - / #
- # / #
- # Makefile .:: .:/ . .:: #
- # +:+:+ +: +: +:+:+ #
- # By: humoyne <marvin@le-101.fr> +:+ +: +: +:+ #
- # #+# #+ #+ #+# #
- # Created: 2018/07/16 17:28:25 by humoyne #+# ## ## #+# #
- # Updated: 2018/07/17 15:52:33 by humoyne ### #+. /#+ ###.fr #
- # / #
- # / #
- # **************************************************************************** #
- NAME = do-op
- PATH_SRC = ./
- HEADER = ./
- FLAG = -Wall -Wextra -Werror
- OPTION = -o $(NAME)
- SRC = $(PATH_SRC)op.c $(PATH_SRC)utility.c $(PATH_SRC)main.c
- all: $(NAME)
- $(NAME):
- gcc $(FLAG) $(OPTION) $(SRC)
- clean:
- /bin/rm -f $(NAME)
- fclean: clean
- /bin/rm -f $(NAME)
- re: fclean all
- /bin/rm -f $(NAME) $(OBJ)
- /* ************************************************************************** */
- /* LE - / */
- /* / */
- /* header.h .:: .:/ . .:: */
- /* +:+:+ +: +: +:+:+ */
- /* By: humoyne <marvin@le-101.fr> +:+ +: +: +:+ */
- /* #+# #+ #+ #+# */
- /* Created: 2018/07/17 07:36:37 by humoyne #+# ## ## #+# */
- /* Updated: 2018/07/17 08:38:06 by humoyne ### #+. /#+ ###.fr */
- /* / */
- /* / */
- /* ************************************************************************** */
- #ifndef HEADER_H
- # define HEADER_H
- # include <unistd.h>
- void ft_putchar(char c);
- void ft_putstr(char *str);
- void ft_putnbr(int nb);
- int ft_atoi_da_shit(char *str);
- int doop(char op, int nb1, int nb2);
- int add(int a, int b);
- int div(int a, int b);
- int sub(int a, int b);
- int multy(int a, int b);
- int mod(int a, int b);
- int doop(char op, int nb1, int nb2);
- int is_numeric_char(char c);
- #endif
- /* ************************************************************************** */
- /* LE - / */
- /* / */
- /* main.c .:: .:/ . .:: */
- /* +:+:+ +: +: +:+:+ */
- /* By: humoyne <marvin@le-101.fr> +:+ +: +: +:+ */
- /* #+# #+ #+ #+# */
- /* Created: 2018/07/16 17:41:07 by humoyne #+# ## ## #+# */
- /* Updated: 2018/07/17 12:07:10 by humoyne ### #+. /#+ ###.fr */
- /* / */
- /* / */
- /* ************************************************************************** */
- #include "header.h"
- int main(int argc, char **argv)
- {
- int nb[2];
- int result;
- if (argc == 4)
- {
- if (!argv[1] || !*argv[1] || !argv[2] || !*argv[2] || !argv[3] ||
- !*argv[3])
- {
- ft_putchar('\n');
- return (0);
- }
- nb[0] = ft_atoi_da_shit(argv[1]);
- nb[1] = ft_atoi_da_shit(argv[3]);
- if ((argv[2][0] == '/') && !(nb[1]))
- ft_putstr("Stop : division by zero\n");
- else if ((argv[2][0] == '%') && !(nb[1]))
- ft_putstr("Stop : modulo by zero\n");
- else
- {
- result = doop(argv[2][0], nb[0], nb[1]);
- ft_putnbr(result);
- ft_putchar('\n');
- }
- }
- return (0);
- }
- /* ************************************************************************** */
- /* LE - / */
- /* / */
- /* op.c .:: .:/ . .:: */
- /* +:+:+ +: +: +:+:+ */
- /* By: humoyne <marvin@le-101.fr> +:+ +: +: +:+ */
- /* #+# #+ #+ #+# */
- /* Created: 2018/07/16 17:12:43 by humoyne #+# ## ## #+# */
- /* Updated: 2018/07/17 08:54:50 by humoyne ### #+. /#+ ###.fr */
- /* / */
- /* / */
- /* ************************************************************************** */
- #include "header.h"
- int add(int a, int b)
- {
- return (a + b);
- }
- int div(int a, int b)
- {
- return (a / b);
- }
- int sub(int a, int b)
- {
- return (a - b);
- }
- int multy(int a, int b)
- {
- return (a * b);
- }
- int mod(int a, int b)
- {
- return (a % b);
- }
- /* ************************************************************************** */
- /* LE - / */
- /* / */
- /* utility.c .:: .:/ . .:: */
- /* +:+:+ +: +: +:+:+ */
- /* By: humoyne <marvin@le-101.fr> +:+ +: +: +:+ */
- /* #+# #+ #+ #+# */
- /* Created: 2018/07/16 17:25:48 by humoyne #+# ## ## #+# */
- /* Updated: 2018/07/17 15:53:28 by humoyne ### #+. /#+ ###.fr */
- /* / */
- /* / */
- /* ************************************************************************** */
- #include "header.h"
- void ft_putchar(char c);
- int is_numeric_char(char c)
- {
- if (c >= '0' && c <= '9')
- return (1);
- else if (c == '-' || c == '+')
- return (1);
- else
- return (0);
- }
- void ft_putstr(char *str)
- {
- int i;
- i = 0;
- while (str[i])
- {
- ft_putchar(str[i]);
- i++;
- }
- }
- void ft_putnbr(int nb)
- {
- if (nb == -2147483648)
- {
- ft_putstr("-2147483648");
- return ;
- }
- if (nb < 0)
- {
- ft_putchar('-');
- nb = nb * -1;
- }
- if (nb >= 10)
- {
- ft_putnbr(nb / 10);
- }
- ft_putchar((nb % 10) + '0');
- }
- int ft_atoi_da_shit(char *str)
- {
- int i;
- int negative;
- int result;
- i = 0;
- result = 0;
- negative = 0;
- while ((str[i] != '\0') && (is_numeric_char(str[i])))
- {
- result *= 10;
- if (str[i] == '-' && !(negative))
- negative = 1;
- if (str[i] >= '0' && str[i] <= '9')
- result += str[i] - '0';
- i++;
- }
- if (negative)
- return (result * -1);
- return (result);
- }
- int doop(char op, int nb1, int nb2)
- {
- int result;
- int i;
- int (*opfunc[5])(int, int);
- char operator[5];
- opfunc[0] = &add;
- operator[0] = '+';
- opfunc[1] = ⊂
- operator[1] = '-';
- opfunc[2] = ÷
- operator[2] = '/';
- opfunc[3] = &multy;
- operator[3] = '*';
- opfunc[4] = &mod;
- operator[4] = '%';
- result = 0;
- i = 0;
- while (i < 5)
- {
- if (operator[i] == op)
- result = opfunc[i](nb1, nb2);
- i++;
- }
- return (result);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement