From a29ece55777e8e98a048444746fb3df8bcc6988c Mon Sep 17 00:00:00 2001 From: lclerel- lclerel- Date: Fri, 6 Mar 2026 10:56:11 +0100 Subject: [PATCH] Exos --- ex0/ft_putnbr.c | 42 ++++++++++++++++++++++++++ ex1/ft_print_comb.c | 52 ++++++++++++++++++++++++++++++++ ex2/ft_print_comb2.c | 50 ++++++++++++++++++++++++++++++ ex3/ft_print_combn.c | 72 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 216 insertions(+) create mode 100644 ex0/ft_putnbr.c create mode 100644 ex1/ft_print_comb.c create mode 100644 ex2/ft_print_comb2.c create mode 100644 ex3/ft_print_combn.c diff --git a/ex0/ft_putnbr.c b/ex0/ft_putnbr.c new file mode 100644 index 0000000..f886dbd --- /dev/null +++ b/ex0/ft_putnbr.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lclerel- +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/03/05 13:17:20 by lclerel- #+# #+# */ +/* Updated: 2026/03/06 10:25:12 by lclerel- ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void ft_putnbr(int nb) +{ + char value; + + if (nb == -2147483648) + { + write(1, "-2147483648", 11); + return ; + } + if (nb < 0) + { + write(1, "-", 1); + nb = -nb; + } + if (nb > 9) + { + ft_putnbr(nb / 10); + } + value = nb % 10 + '0'; + write(1, &value, 1); +} + +/*int main(void) +{ + ft_putnbr(42); + ft_putnbr(-2147483647); + return (0); +}*/ diff --git a/ex1/ft_print_comb.c b/ex1/ft_print_comb.c new file mode 100644 index 0000000..05aeeae --- /dev/null +++ b/ex1/ft_print_comb.c @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_print_comb.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lclerel- +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/03/05 09:00:24 by lclerel- #+# #+# */ +/* Updated: 2026/03/06 10:26:18 by lclerel- ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void print(char a, char b, char c) +{ + write(1, &a, 1); + write(1, &b, 1); + write(1, &c, 1); + if (!(a == '7' && b == '8' && c == '9')) + write(1, ", ", 2); +} + +void ft_print_comb(void) +{ + char a; + char b; + char c; + + a = '0'; + while (a <= '7') + { + b = a + 1; + while (b <= '8') + { + c = b + 1; + while (c <= '9') + { + print(a, b, c); + c++; + } + b++; + } + a++; + } +} + +int main(void) +{ + ft_print_comb(); + return (0); +} diff --git a/ex2/ft_print_comb2.c b/ex2/ft_print_comb2.c new file mode 100644 index 0000000..87a310f --- /dev/null +++ b/ex2/ft_print_comb2.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_print_comb2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lclerel- +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/03/05 13:45:48 by lclerel- #+# #+# */ +/* Updated: 2026/03/05 19:09:34 by lclerel- ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void print(char a, char space, char b) +{ + write(1, &"0123456789"[a / 10], 1); + write(1, &"0123456789"[a % 10], 1); + write(1, &space, 1); + write(1, &"0123456789"[b / 10], 1); + write(1, &"0123456789"[b % 10], 1); + if (!(a == 98 && b == 99)) + write(1, ", ", 2); +} + +void ft_print_comb2(void) +{ + char space; + char a; + char b; + + space = ' '; + a = 0; + while (a <= 98) + { + b = a + 1; + while (b <= 99) + { + print(a, space, b); + b++; + } + a++; + } +} + +/*int main(void) +{ + ft_print_comb2(); + return (0); +}*/ diff --git a/ex3/ft_print_combn.c b/ex3/ft_print_combn.c new file mode 100644 index 0000000..3c629fe --- /dev/null +++ b/ex3/ft_print_combn.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_print_combn.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lclerel- +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/03/05 19:08:08 by lclerel- #+# #+# */ +/* Updated: 2026/03/05 19:08:53 by lclerel- ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void ft_put_tab(int *tab, int n) +{ + int i; + char c; + + i = 0; + while (i < n) + { + c = tab[i] + '0'; + write(1, &c, 1); + i++; + } + if (tab[0] != (10 - n)) + { + write(1, ", ", 2); + } +} + +void ft_recursive(int *tab, int n, int pos, int start) +{ + int i; + + if (pos == n) + { + ft_put_tab(tab, n); + return ; + } + i = start; + while (i <= 9) + { + tab[pos] = i; + ft_recursive(tab, n, pos + 1, i + 1); + i++; + } +} + +void ft_print_combn(int n) +{ + int tab[10]; + + if (n > 0 && n < 10) + { + ft_recursive(tab, n, 0, 0); + } +} + +/*int main(void) +{ + write(1, "Test n = 2 :\n", 13); + ft_print_combn(2); + write(1, "\n\n", 2); + write(1, "Test n = 3 :\n", 13); + ft_print_combn(3); + write(1, "\n\n", 2); + write(1, "Test n = 9 :\n", 13); + ft_print_combn(9); + write(1, "\n\n", 2); +}*/