diff options
| author | Charles <sircharlesaze@gmail.com> | 2019-07-15 08:15:37 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2019-07-15 08:15:37 +0200 |
| commit | 3b9a1d7dcc5683b962f2bf24795e80e1c449cd1f (patch) | |
| tree | 25b02c02f5140dbefbabd7720f292d8be3d5cc51 /c08 | |
| parent | c2bf9fcefbb4453cee271ccd1af9674ad2f3a181 (diff) | |
| download | piscine-3b9a1d7dcc5683b962f2bf24795e80e1c449cd1f.tar.gz piscine-3b9a1d7dcc5683b962f2bf24795e80e1c449cd1f.tar.bz2 piscine-3b9a1d7dcc5683b962f2bf24795e80e1c449cd1f.zip | |
c07 passed, c08 in progress, rush01(+ 6x6 try)
Diffstat (limited to 'c08')
| -rw-r--r-- | c08/boolean_main.c | 34 | ||||
| -rw-r--r-- | c08/ex00/ft.h | 4 | ||||
| -rw-r--r-- | c08/ex01/ft_boolean.h | 29 | ||||
| -rw-r--r-- | c08/ex02/ft_abs.h | 6 | ||||
| -rw-r--r-- | c08/ex03/ft_point.h | 6 | ||||
| -rw-r--r-- | c08/ex04/ft_stock_str.h | 8 | ||||
| -rw-r--r-- | c08/ex04/ft_strs_to_tab.c | 59 | ||||
| -rw-r--r-- | c08/ex05/ft_show_tab.c | 57 | ||||
| -rw-r--r-- | c08/ex05/ft_stock_str.h | 23 | ||||
| -rw-r--r-- | c08/main.c | 51 | ||||
| -rw-r--r-- | c08/point_main.c | 27 |
11 files changed, 259 insertions, 45 deletions
diff --git a/c08/boolean_main.c b/c08/boolean_main.c new file mode 100644 index 0000000..9e8e428 --- /dev/null +++ b/c08/boolean_main.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* boolean_main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/11 14:40:38 by cacharle #+# #+# */ +/* Updated: 2019/07/11 14:40:40 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "./ex01/ft_boolean.h" + +void ft_putstr(char *str) +{ + while (*str) + write(1, str++, 1); +} + +t_bool ft_is_even(int nbr) +{ + return ((EVEN(nbr)) ? TRUE : FALSE); +} + +int main(int argc, char **argv) +{ + (void)argv; + if (ft_is_even(argc - 1) == TRUE) + ft_putstr(EVEN_MSG); + else + ft_putstr(ODD_MSG); + return (SUCCESS); +} diff --git a/c08/ex00/ft.h b/c08/ex00/ft.h index 5dbbb51..9575169 100644 --- a/c08/ex00/ft.h +++ b/c08/ex00/ft.h @@ -6,12 +6,12 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/07 16:33:44 by cacharle #+# #+# */ -/* Updated: 2019/07/07 16:34:58 by cacharle ### ########.fr */ +/* Updated: 2019/07/12 14:50:28 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef FT_H -#define FT_H +# define FT_H void ft_putchar(char c); void ft_swap(int *a, int *b); diff --git a/c08/ex01/ft_boolean.h b/c08/ex01/ft_boolean.h index 43a3c4f..6728409 100644 --- a/c08/ex01/ft_boolean.h +++ b/c08/ex01/ft_boolean.h @@ -6,29 +6,28 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/07 16:35:29 by cacharle #+# #+# */ -/* Updated: 2019/07/07 17:07:47 by cacharle ### ########.fr */ +/* Updated: 2019/07/11 14:37:46 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef FT_BOOLEAN_H -#define FT_BOOLEAN_H +# define FT_BOOLEAN_H -#include <unistd.h> +# include <unistd.h> -#define TRUE 1 -#define FALSE 1 -#define SUCCESS 0 -#define EVEN_MSG "I have an even number of arguments.\n" -#define ODD_MSG "I have an odd number of arguments.\n" -#define EVEN(x) (x % 2 == 0 ? 1 : 0) +# define TRUE 1 +# define FALSE 0 +# define SUCCESS 0 +# define EVEN_MSG "I have an even number of arguments.\n" +# define ODD_MSG "I have an odd number of arguments.\n" +# define EVEN(x) (x % 2 == 0 ? 1 : 0) typedef enum { - FALSE, - TRUE -} t_bool; - -void ft_putstr(char *str); -t_bool ft_is_even(int nbr); + true, + false +} t_bool; +void ft_putstr(char *str); +t_bool ft_is_even(int nbr); #endif diff --git a/c08/ex02/ft_abs.h b/c08/ex02/ft_abs.h index 9a41241..ba01929 100644 --- a/c08/ex02/ft_abs.h +++ b/c08/ex02/ft_abs.h @@ -6,13 +6,13 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/07 17:09:37 by cacharle #+# #+# */ -/* Updated: 2019/07/07 17:11:50 by cacharle ### ########.fr */ +/* Updated: 2019/07/13 08:56:29 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef FT_ABS_H -#define FT_ABS_H +# define FT_ABS_H -#define ABS(x) (x < 0 ? -x : x) +# define ABS(x) ( ( (x) < 0) ? -(x) : (x) ) #endif diff --git a/c08/ex03/ft_point.h b/c08/ex03/ft_point.h index 088b792..5129a4f 100644 --- a/c08/ex03/ft_point.h +++ b/c08/ex03/ft_point.h @@ -6,16 +6,16 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/07 17:12:47 by cacharle #+# #+# */ -/* Updated: 2019/07/07 17:15:20 by cacharle ### ########.fr */ +/* Updated: 2019/07/11 14:13:47 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef FT_POINT_H -#define FT_POINT_H +# define FT_POINT_H typedef struct { int x; int y; -} t_point; +} t_point; #endif diff --git a/c08/ex04/ft_stock_str.h b/c08/ex04/ft_stock_str.h index ccadd2f..2ce09f4 100644 --- a/c08/ex04/ft_stock_str.h +++ b/c08/ex04/ft_stock_str.h @@ -6,18 +6,18 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/08 16:54:59 by cacharle #+# #+# */ -/* Updated: 2019/07/08 16:56:02 by cacharle ### ########.fr */ +/* Updated: 2019/07/11 14:14:13 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef FT_STOCK_STR_H -#define FT_STOCK_STR_H +# define FT_STOCK_STR_H -typedef struct s_stock_str +typedef struct s_stock_str { int size; char *str; char *copy; -} t_stock_str; +} t_stock_str; #endif diff --git a/c08/ex04/ft_strs_to_tab.c b/c08/ex04/ft_strs_to_tab.c index 67a0304..f6fbfaf 100644 --- a/c08/ex04/ft_strs_to_tab.c +++ b/c08/ex04/ft_strs_to_tab.c @@ -6,35 +6,58 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/07 17:15:32 by cacharle #+# #+# */ -/* Updated: 2019/07/08 17:10:21 by cacharle ### ########.fr */ +/* Updated: 2019/07/14 11:12:08 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include <stdlib.h> #include "ft_stock_str.h" -struct s_stock_str *ft_strs_to_tab(int ac, char **av) +int ft_strlen(char *str) +{ + int counter; + + counter = 0; + while (str[counter]) + counter++; + return (counter); +} + +char *ft_strdup(char *src) +{ + int i; + char *dup_ptr; + + dup_ptr = (char*)malloc(sizeof(char) * ft_strlen(src)); + if (dup_ptr == NULL) + return (NULL); + i = 0; + while (src[i]) + { + dup_ptr[i] = src[i]; + i++; + } + dup_ptr[i] = '\0'; + return (dup_ptr); +} + +struct s_stock_str *ft_strs_to_tab(int ac, char **av) { int i; - char *copy; t_stock_str tmp_stock; - t_stock_str *strs_stocks + t_stock_str *strs_stocks; - strs_stocks = malloc(sizeof(t_stock_str) * ac); - while (ac-- > 0) + if ((strs_stocks = malloc(sizeof(t_stock_str) * (ac + 1))) == NULL) + return (NULL); + i = 0; + while (i < ac) { - i = 0 - while (av[ac][i]) - i++; - tmp_stock = malloc(sizeof(t_stock_str)); - tmp_stock->size = i; - tmp_stock->str = av[ac]; - copy = malloc(sizeof(char) * i); - i = -1; - while (av[ac][i++]) - copy[i] = av[ac][i]; - tmp_stock->copy = copy; - strs_stocks[ac] = tmp_stock; + tmp_stock.size = ft_strlen(av[i]); + tmp_stock.str = av[i]; + if ((tmp_stock.copy = ft_strdup(av[i])) == NULL) + return (NULL); + strs_stocks[i++] = tmp_stock; } + strs_stocks[ac].str = 0; return (strs_stocks); } diff --git a/c08/ex05/ft_show_tab.c b/c08/ex05/ft_show_tab.c new file mode 100644 index 0000000..4af1843 --- /dev/null +++ b/c08/ex05/ft_show_tab.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_show_tab.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/11 15:55:41 by cacharle #+# #+# */ +/* Updated: 2019/07/14 11:12:21 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <unistd.h> +#include "ft_stock_str.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'); +} + +void ft_putstr(char *str) +{ + while (*str) + write(1, str++, 1); +} + +void ft_show_tab(struct s_stock_str *par) +{ + int i; + + i = 0; + while (par[i].str != 0) + { + ft_putstr(par[i].str); + write(1, "\n", 1); + ft_putnbr(par[i].size); + write(1, "\n", 1); + ft_putstr(par[i].copy); + write(1, "\n", 1); + i++; + } +} diff --git a/c08/ex05/ft_stock_str.h b/c08/ex05/ft_stock_str.h new file mode 100644 index 0000000..2ce09f4 --- /dev/null +++ b/c08/ex05/ft_stock_str.h @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_stock_str.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/08 16:54:59 by cacharle #+# #+# */ +/* Updated: 2019/07/11 14:14:13 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_STOCK_STR_H +# define FT_STOCK_STR_H + +typedef struct s_stock_str +{ + int size; + char *str; + char *copy; +} t_stock_str; + +#endif diff --git a/c08/main.c b/c08/main.c new file mode 100644 index 0000000..67a40dc --- /dev/null +++ b/c08/main.c @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/12 15:24:19 by cacharle #+# #+# */ +/* Updated: 2019/07/13 08:55:06 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <stdio.h> +#include <string.h> +#include "ex02/ft_abs.h" +#include "ex03/ft_point.h" +#include "ex04/ft_strs_to_tab.c" +#include "ex05/ft_show_tab.c" + +int main() +{ + printf("%d\n", ABS(42)); + printf("%d\n", ABS(-42)); + printf("%d\n", ABS(30 - 42)); + printf("%d\n", ABS(50 * -45)); + printf("%d\n", ABS(-24) - 20); + + printf("--------------------\n"); + char **a = (char**)malloc(sizeof(char*) * 3); + a[0] = (char*)malloc(sizeof(char) * 32); + a[1] = (char*)malloc(sizeof(char) * 32); + a[2] = (char*)malloc(sizeof(char) * 32); + strcpy(a[0], "binjour"); + strcpy(a[1], "je"); + strcpy(a[2], "suis"); + t_stock_str *b = ft_strs_to_tab(3, a); + for (int i = 0; b[i].str != 0; i++) + printf("%s | %s | %d\n", b[i].str, b[i].copy, b[i].size); + + b[0].copy = "bafj"; + ft_show_tab(b); + /*for (int i = 0; b[i].str != 0; i++)*/ + /*free(b[i].copy);*/ + /*free(b);*/ + /*free(a[0]);*/ + /*free(a[1]);*/ + /*free(a[2]);*/ + /*free(a);*/ + + return 0; +} diff --git a/c08/point_main.c b/c08/point_main.c new file mode 100644 index 0000000..ec651f4 --- /dev/null +++ b/c08/point_main.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* point_main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/11 14:50:27 by cacharle #+# #+# */ +/* Updated: 2019/07/11 14:51:48 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ex03/ft_point.h" + +void set_point(t_point *point) +{ + point->x = 42; + point->y = 21; +} + +int main(void) +{ + t_point point; + + set_point(&point); + return (0); +} |
