diff options
Diffstat (limited to 'c11')
| -rw-r--r-- | c11/ex00/ft_foreach.c | 20 | ||||
| -rw-r--r-- | c11/ex01/ft_map.c | 29 | ||||
| -rw-r--r-- | c11/ex02/ft_any.c | 19 | ||||
| -rw-r--r-- | c11/ex03/ft_count_if.c | 22 | ||||
| -rw-r--r-- | c11/ex04/ft_is_sort.c | 26 | ||||
| -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 | ||||
| -rw-r--r-- | c11/ex06/ft_sort_string_tab.c | 56 | ||||
| -rw-r--r-- | c11/ex07/ft_advanced_sort_string_tab.c | 46 | ||||
| -rw-r--r-- | c11/main.c | 83 |
15 files changed, 581 insertions, 0 deletions
diff --git a/c11/ex00/ft_foreach.c b/c11/ex00/ft_foreach.c new file mode 100644 index 0000000..a2b0d0a --- /dev/null +++ b/c11/ex00/ft_foreach.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_foreach.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/16 21:12:43 by cacharle #+# #+# */ +/* Updated: 2019/07/16 21:16:21 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +void ft_foreach(int *tab, int length, void(*f)(int)) +{ + int i; + + i = 0; + while (i < length) + (*f)(tab[i++]); +} diff --git a/c11/ex01/ft_map.c b/c11/ex01/ft_map.c new file mode 100644 index 0000000..c221f2b --- /dev/null +++ b/c11/ex01/ft_map.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_map.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/16 21:17:12 by cacharle #+# #+# */ +/* Updated: 2019/07/16 21:21:31 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <stdlib.h> + +int *ft_map(int *tab, int length, int(*f)(int)) +{ + int i; + int *mapped; + + if ((mapped = (int*)malloc(sizeof(int) * length)) == NULL) + return (NULL); + i = 0; + while (i < length) + { + mapped[i] = (*f)(tab[i]); + i++; + } + return (mapped); +} diff --git a/c11/ex02/ft_any.c b/c11/ex02/ft_any.c new file mode 100644 index 0000000..100fd12 --- /dev/null +++ b/c11/ex02/ft_any.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_any.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/16 21:23:26 by cacharle #+# #+# */ +/* Updated: 2019/07/16 21:32:21 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_any(char **tab, int(*f)(char*)) +{ + while (*tab != NULL) + if ((*f)(*tab++)) + return (1); + return (0); +} diff --git a/c11/ex03/ft_count_if.c b/c11/ex03/ft_count_if.c new file mode 100644 index 0000000..1b1c00a --- /dev/null +++ b/c11/ex03/ft_count_if.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_count_if.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/16 21:35:34 by cacharle #+# #+# */ +/* Updated: 2019/07/16 21:36:42 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_count_if(char **tab, int length, int(*f)(char*)) +{ + int counter; + + counter = 0; + while (length-- > 0) + if ((*f)(*tab++)) + counter++; + return (counter); +} diff --git a/c11/ex04/ft_is_sort.c b/c11/ex04/ft_is_sort.c new file mode 100644 index 0000000..7afed9c --- /dev/null +++ b/c11/ex04/ft_is_sort.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_is_sort.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/16 21:39:43 by cacharle #+# #+# */ +/* Updated: 2019/07/17 16:34:13 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_is_sort(int *tab, int length, int(*f)(int, int)) +{ + + int i; + + i = 0; + while (i < length - 1) + { + if ((*f)(tab[i], tab[i + 1]) > 0) + return (0); + i++; + } + return (1); +} 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*/ + +/*}*/ diff --git a/c11/ex06/ft_sort_string_tab.c b/c11/ex06/ft_sort_string_tab.c new file mode 100644 index 0000000..25ac46e --- /dev/null +++ b/c11/ex06/ft_sort_string_tab.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sort_string_tab.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/16 21:48:20 by cacharle #+# #+# */ +/* Updated: 2019/07/17 16:43:27 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_strcmp(char *s1, char *s2) +{ + while (*s1 == *s2 && *s1 && *s2) + { + s1++; + s2++; + } + return (*s1 - *s2); +} + +int is_sorted(char **argv) +{ + int i; + + i = 1; + while (argv[i + 1] != NULL) + { + if (ft_strcmp(argv[i], argv[i + 1]) > 0) + return (0); + i++; + } + return (1); +} + +void ft_sort_string_tab(char **tab) +{ + int i; + char *tmp; + + while (!is_sorted(argv)) + { + i = 1; + while (tab[i + 1] != NULL) + { + if (ft_strcmp(tab[i], tab[i + 1]) > 0) + { + tmp = tab[i]; + tab[i] = tab[i + 1]; + tab[i + 1] = tmp; + } + i++; + } + } +} diff --git a/c11/ex07/ft_advanced_sort_string_tab.c b/c11/ex07/ft_advanced_sort_string_tab.c new file mode 100644 index 0000000..f640664 --- /dev/null +++ b/c11/ex07/ft_advanced_sort_string_tab.c @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_advanced_sort_string_tab.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/16 21:51:06 by cacharle #+# #+# */ +/* Updated: 2019/07/16 21:54:24 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int is_sorted(char **tab, int(*cmp)(char *, char *)) +{ + int i; + + i = 1; + while (tab[i] != NULL) + { + if ((*cmp)(tab[i], tab[i +1]) > 0) + return (0); + i++; + } + return (1); +} + +void ft_advanced_sort_string_tab(char **tab, int(*cmp)(char *, char *)) +{ + int i; + char *tmp; + + while (!is_sorted(tab)) + { + i = 1; + while (tab[i] != NULL) + { + if ((*cmp)(tab[i], tab[i +1]) > 0) + { + tmp = argv[i]; + argv[i] = argv[i + 1]; + argv[i + 1] = tmp; + } + i++; + } + } +} diff --git a/c11/main.c b/c11/main.c new file mode 100644 index 0000000..0765d8e --- /dev/null +++ b/c11/main.c @@ -0,0 +1,83 @@ +#include <stdio.h> +#include <limits.h> +#include <string.h> +#include "ex00/ft_foreach.c" +#include "ex01/ft_map.c" +#include "ex02/ft_any.c" +#include "ex03/ft_count_if.c" +#include "ex04/ft_is_sort.c" +/*#include "ex05/ft_foreach.c"*/ +/*#include "ex06/ft_foreach.c"*/ +/*#include "ex07/ft_foreach.c"*/ + +void f_fe(int x); +int f_ma(int x); +int f_len(char *x); +int f_cou(char *x); +int f_sor(int x, int y); + +int main() +{ + int tab[] = {1, 2, 3, 45, 67, 12, 89}; + ft_foreach(tab, 7, &f_fe); + + printf("\n------------------------\n"); + int *mapped = ft_map(tab, 7, &f_ma); + for (int i = 0; i < 7; i++) + printf("%d ", mapped[i]); + free(mapped); + + printf("\n------------------------\n"); + char **ev = malloc(sizeof(char) * 5); + ev[0] = malloc(sizeof(char) * 32); + ev[1] = malloc(sizeof(char) * 32); + ev[2] = malloc(sizeof(char) * 32); + strcpy(ev[0], "bonjour"); + strcpy(ev[1], "j"); + strcpy(ev[2], "charles"); + ev[3] = NULL; + printf("any %d", ft_any(ev, &f_len)); + + printf("\n------------------------\n"); + printf("count if %d", ft_count_if(ev, 3, &f_cou)); + + printf("\n------------------------\n"); + int sorted[10] = {1, 2, 3, 4, 5, 5, 6}; + printf("sorted %d", ft_is_sort(sorted, 6, &f_sor)); + + printf("\n------------------------\n"); + +} + +void f_fe(int x) +{ + printf("%d ", x + 2); +} + +int f_ma(int x) +{ + return (x * 2); +} + +int f_len(char *x) +{ + int counter = 0; + while (x[counter]) + counter++; + return counter < 2; +} + +int f_cou(char *x) +{ + int counter = 0; + while (x[counter]) + counter++; + return counter > 2; +} + +int f_sor(int x, int y) +{ + if (x == y) + return (0); + return x < y ? -1 : 1; +} |
