From 8b6e91bdb56bc01a588718472546f2a88e750b48 Mon Sep 17 00:00:00 2001 From: Charles Date: Fri, 19 Jul 2019 07:04:04 +0200 Subject: c10 done?, c11 moulinetted (ex04, ex06 need correction) --- c11/ex00/ft_foreach.c | 4 +-- c11/ex01/ft_map.c | 4 +-- c11/ex02/ft_any.c | 6 +++-- c11/ex03/ft_count_if.c | 4 +-- c11/ex04/ft_is_sort.c | 24 +++++++++++++++--- c11/ex05/Makefile | 4 +-- c11/ex05/do-op | Bin 8956 -> 0 bytes c11/ex05/helper.c | 6 ++--- c11/ex05/include.h | 6 ++--- c11/ex05/main.c | 11 +++----- c11/ex05/operators.c | 14 +++++----- c11/ex05/parse.c | 26 +++++++++++++------ c11/ex06/ft_sort_string_tab.c | 8 +++--- c11/ex07/ft_advanced_sort_string_tab.c | 28 ++++++++++---------- c11/main.c | 45 ++++++++++++++++++++++++++++++--- 15 files changed, 128 insertions(+), 62 deletions(-) delete mode 100755 c11/ex05/do-op (limited to 'c11') diff --git a/c11/ex00/ft_foreach.c b/c11/ex00/ft_foreach.c index a2b0d0a..4125c3d 100644 --- a/c11/ex00/ft_foreach.c +++ b/c11/ex00/ft_foreach.c @@ -6,11 +6,11 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/16 21:12:43 by cacharle #+# #+# */ -/* Updated: 2019/07/16 21:16:21 by cacharle ### ########.fr */ +/* Updated: 2019/07/18 10:59:42 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -void ft_foreach(int *tab, int length, void(*f)(int)) +void ft_foreach(int *tab, int length, void (*f)(int)) { int i; diff --git a/c11/ex01/ft_map.c b/c11/ex01/ft_map.c index c221f2b..8c973e2 100644 --- a/c11/ex01/ft_map.c +++ b/c11/ex01/ft_map.c @@ -6,13 +6,13 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/16 21:17:12 by cacharle #+# #+# */ -/* Updated: 2019/07/16 21:21:31 by cacharle ### ########.fr */ +/* Updated: 2019/07/18 11:00:42 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include -int *ft_map(int *tab, int length, int(*f)(int)) +int *ft_map(int *tab, int length, int (*f)(int)) { int i; int *mapped; diff --git a/c11/ex02/ft_any.c b/c11/ex02/ft_any.c index 100fd12..ee09b41 100644 --- a/c11/ex02/ft_any.c +++ b/c11/ex02/ft_any.c @@ -6,11 +6,13 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/16 21:23:26 by cacharle #+# #+# */ -/* Updated: 2019/07/16 21:32:21 by cacharle ### ########.fr */ +/* Updated: 2019/07/18 11:03:40 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -int ft_any(char **tab, int(*f)(char*)) +#include + +int ft_any(char **tab, int (*f)(char*)) { while (*tab != NULL) if ((*f)(*tab++)) diff --git a/c11/ex03/ft_count_if.c b/c11/ex03/ft_count_if.c index 1b1c00a..a04fe39 100644 --- a/c11/ex03/ft_count_if.c +++ b/c11/ex03/ft_count_if.c @@ -6,11 +6,11 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/16 21:35:34 by cacharle #+# #+# */ -/* Updated: 2019/07/16 21:36:42 by cacharle ### ########.fr */ +/* Updated: 2019/07/18 11:01:46 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -int ft_count_if(char **tab, int length, int(*f)(char*)) +int ft_count_if(char **tab, int length, int (*f)(char*)) { int counter; diff --git a/c11/ex04/ft_is_sort.c b/c11/ex04/ft_is_sort.c index 7afed9c..84d4fe0 100644 --- a/c11/ex04/ft_is_sort.c +++ b/c11/ex04/ft_is_sort.c @@ -6,13 +6,12 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/16 21:39:43 by cacharle #+# #+# */ -/* Updated: 2019/07/17 16:34:13 by cacharle ### ########.fr */ +/* Updated: 2019/07/18 21:19:29 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -int ft_is_sort(int *tab, int length, int(*f)(int, int)) +static is_sort_asc(int *tab, int length, int (*f)(int, int)) { - int i; i = 0; @@ -24,3 +23,22 @@ int ft_is_sort(int *tab, int length, int(*f)(int, int)) } return (1); } + +static is_sort_dsc(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); +} + +int ft_is_sort(int *tab, int length, int (*f)(int, int)) +{ + return (is_sort_dsc(tab, length, f) || is_sort_asc(tab, length, f)); +} diff --git a/c11/ex05/Makefile b/c11/ex05/Makefile index 0aca71b..ae0b6ad 100644 --- a/c11/ex05/Makefile +++ b/c11/ex05/Makefile @@ -6,13 +6,13 @@ # By: cacharle +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/07/17 07:55:23 by cacharle #+# #+# # -# Updated: 2019/07/17 08:15:25 by cacharle ### ########.fr # +# Updated: 2019/07/18 10:21:51 by cacharle ### ########.fr # # # # **************************************************************************** # OUT = do-op CC = gcc -FLAGS = -Wall -Wextra #-Werror +FLAGS = -Wall -Wextra -Werror SRC = main.c operators.c parse.c helper.c OBJ = $(SRC:.c=.o) diff --git a/c11/ex05/do-op b/c11/ex05/do-op deleted file mode 100755 index 90f13df..0000000 Binary files a/c11/ex05/do-op and /dev/null differ diff --git a/c11/ex05/helper.c b/c11/ex05/helper.c index 8f370de..57075dc 100644 --- a/c11/ex05/helper.c +++ b/c11/ex05/helper.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/17 07:53:07 by cacharle #+# #+# */ -/* Updated: 2019/07/17 07:53:59 by cacharle ### ########.fr */ +/* Updated: 2019/07/18 10:38:57 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,7 +33,7 @@ void ft_putnbr(int nb) ft_putchar(p_nb % 10 + '0'); } -int pow10(int exponent) +int pow10(int exponent) { int accumulator; @@ -46,7 +46,7 @@ int pow10(int exponent) return (accumulator); } -int ft_atoi(char *str) +int ft_atoi(char *str) { int is_negative; int nb; diff --git a/c11/ex05/include.h b/c11/ex05/include.h index 49381a3..bc92263 100644 --- a/c11/ex05/include.h +++ b/c11/ex05/include.h @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/17 07:38:10 by cacharle #+# #+# */ -/* Updated: 2019/07/17 08:15:39 by cacharle ### ########.fr */ +/* Updated: 2019/07/18 10:40:10 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,8 +27,8 @@ 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)) +int parse(int argc, char **argv); +int check_floating_point_error(int operator_index, int y); /* ** helper.c diff --git a/c11/ex05/main.c b/c11/ex05/main.c index e234053..c29b6ef 100644 --- a/c11/ex05/main.c +++ b/c11/ex05/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/17 07:29:52 by cacharle #+# #+# */ -/* Updated: 2019/07/17 16:37:31 by cacharle ### ########.fr */ +/* Updated: 2019/07/18 10:38:57 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,13 +34,8 @@ int main(int argc, char **argv) { x = ft_atoi(argv[1]); y = ft_atoi(argv[3]); - if (operator_index == 3 && y == 0) - { - - } - if (operator_index == 3 && y == 0) - { - } + if (check_floating_point_error(operator_index, y) == -1) + return (0); ft_putnbr((*operators[operator_index])(x, y)); } ft_putchar('\n'); diff --git a/c11/ex05/operators.c b/c11/ex05/operators.c index c138be3..4efba28 100644 --- a/c11/ex05/operators.c +++ b/c11/ex05/operators.c @@ -6,33 +6,31 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/17 07:38:59 by cacharle #+# #+# */ -/* Updated: 2019/07/17 08:03:34 by cacharle ### ########.fr */ +/* Updated: 2019/07/18 10:38:57 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -int add(int x, int y) +int add(int x, int y) { return (x + y); } -int subtract(int x, int y) +int subtract(int x, int y) { - return (x - y); } -int multiply(int x, int y) +int multiply(int x, int y) { - return (x * y); } -int divide(int x, int y) +int divide(int x, int y) { return (x / y); } -int modulo(int x, int y) +int modulo(int x, int y) { return (x % y); } diff --git a/c11/ex05/parse.c b/c11/ex05/parse.c index 5b1c531..1540945 100644 --- a/c11/ex05/parse.c +++ b/c11/ex05/parse.c @@ -6,10 +6,12 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/17 08:05:59 by cacharle #+# #+# */ -/* Updated: 2019/07/17 08:35:50 by cacharle ### ########.fr */ +/* Updated: 2019/07/18 10:40:34 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ +#include + int parse(int argc, char **argv) { int i; @@ -27,13 +29,21 @@ int parse(int argc, char **argv) else if (argv[2][0] == '%') i = 4; else - i = -2; + i = -2; return (i); } -/*int make_operation(int x, int y, int (*operator)(int, int))*/ -/*{*/ - - /*retu*/ - -/*}*/ +int check_floating_point_error(int operator_index, int y) +{ + if (operator_index == 3 && y == 0) + { + write(1, "Stop : division by zero\n", 24); + return (-1); + } + if (operator_index == 4 && y == 0) + { + write(1, "Stop : modulo by zero\n", 22); + return (-1); + } + return (0); +} diff --git a/c11/ex06/ft_sort_string_tab.c b/c11/ex06/ft_sort_string_tab.c index 25ac46e..c10feaa 100644 --- a/c11/ex06/ft_sort_string_tab.c +++ b/c11/ex06/ft_sort_string_tab.c @@ -6,10 +6,12 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/16 21:48:20 by cacharle #+# #+# */ -/* Updated: 2019/07/17 16:43:27 by cacharle ### ########.fr */ +/* Updated: 2019/07/18 11:04:01 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ +#include + int ft_strcmp(char *s1, char *s2) { while (*s1 == *s2 && *s1 && *s2) @@ -34,12 +36,12 @@ int is_sorted(char **argv) return (1); } -void ft_sort_string_tab(char **tab) +void ft_sort_string_tab(char **tab) { int i; char *tmp; - while (!is_sorted(argv)) + while (!is_sorted(tab)) { i = 1; while (tab[i + 1] != NULL) diff --git a/c11/ex07/ft_advanced_sort_string_tab.c b/c11/ex07/ft_advanced_sort_string_tab.c index f640664..efcfb37 100644 --- a/c11/ex07/ft_advanced_sort_string_tab.c +++ b/c11/ex07/ft_advanced_sort_string_tab.c @@ -6,39 +6,41 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/16 21:51:06 by cacharle #+# #+# */ -/* Updated: 2019/07/16 21:54:24 by cacharle ### ########.fr */ +/* Updated: 2019/07/18 11:03:31 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -int is_sorted(char **tab, int(*cmp)(char *, char *)) +#include + +int sorted(char **tab, int (*cmp)(char *, char *)) { int i; - i = 1; - while (tab[i] != NULL) + i = 0; + while (tab[i + 1] != NULL) { - if ((*cmp)(tab[i], tab[i +1]) > 0) + 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 *)) +void ft_advanced_sort_string_tab(char **tab, int (*cmp)(char *, char *)) { int i; char *tmp; - while (!is_sorted(tab)) + while (!sorted(tab, cmp)) { - i = 1; - while (tab[i] != NULL) + i = 0; + while (tab[i + 1] != NULL) { - if ((*cmp)(tab[i], tab[i +1]) > 0) + if ((*cmp)(tab[i], tab[i + 1]) > 0) { - tmp = argv[i]; - argv[i] = argv[i + 1]; - argv[i + 1] = tmp; + tmp = tab[i]; + tab[i] = tab[i + 1]; + tab[i + 1] = tmp; } i++; } diff --git a/c11/main.c b/c11/main.c index 0765d8e..ac7996b 100644 --- a/c11/main.c +++ b/c11/main.c @@ -6,15 +6,15 @@ #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"*/ +#include "ex06/ft_sort_string_tab.c" +#include "ex07/ft_advanced_sort_string_tab.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 f_lensort(char *a, char *b); int main() { @@ -46,7 +46,34 @@ int main() printf("sorted %d", ft_is_sort(sorted, 6, &f_sor)); printf("\n------------------------\n"); + char **a = malloc(sizeof(char*) * 5); + a[0] = malloc(sizeof(char) * 32); + a[1] = malloc(sizeof(char) * 32); + a[2] = malloc(sizeof(char) * 32); + a[3] = malloc(sizeof(char) * 32); + strcpy(a[0], "bonjour"); + strcpy(a[1], "je"); + strcpy(a[2], "suis"); + strcpy(a[3], "charles"); + a[4] = NULL; + ft_sort_string_tab(a); + for (int i = 0; i < 5; i++) + printf("%s\n", a[i]); + printf("\n------------------------\n"); + char **b = malloc(sizeof(char*) * 5); + b[0] = malloc(sizeof(char) * 32); + b[1] = malloc(sizeof(char) * 32); + b[2] = malloc(sizeof(char) * 32); + b[3] = malloc(sizeof(char) * 32); + strcpy(b[0], "bjour"); + strcpy(b[1], "je"); + strcpy(b[2], "suis"); + strcpy(b[3], "carles"); + b[4] = NULL; + ft_advanced_sort_string_tab(b, &f_lensort); + for (int i = 0; i < 5; i++) + printf("%s\n", b[i]); } void f_fe(int x) @@ -81,3 +108,15 @@ int f_sor(int x, int y) return (0); return x < y ? -1 : 1; } + +int f_lensort(char *a, char *b) +{ + int i = 0; + int j = 0; + + while (a[i]) + i++; + while (b[j]) + j++; + return (i - j); +} -- cgit