diff options
| -rw-r--r-- | c04/ex05/ft_atoi_base.c | 3 | ||||
| -rw-r--r-- | c05/ex00/ft_iterative_factorial.c | 3 | ||||
| -rw-r--r-- | c05/ex03/ft_recursive_power.c | 11 | ||||
| -rw-r--r-- | c05/ex05/ft_sqrt.c | 30 | ||||
| -rw-r--r-- | c05/ex06/ft_is_prime.c | 16 | ||||
| -rw-r--r-- | c05/ex07/ft_find_next_prime.c | 16 | ||||
| -rw-r--r-- | c05/main.c | 24 | ||||
| -rwxr-xr-x | c06/bonjour | bin | 8432 -> 0 bytes | |||
| -rw-r--r-- | c07/ex00/ft_strdup.c | 4 | ||||
| -rw-r--r-- | c07/ex01/ft_range.c | 8 | ||||
| -rw-r--r-- | c07/ex02/ft_ultimate_range.c | 4 | ||||
| -rw-r--r-- | c07/ex03/ft_strjoin.c | 13 | ||||
| -rw-r--r-- | c07/ex04/ft_convert_base.c | 65 | ||||
| -rw-r--r-- | c07/ex04/ft_convert_base2.c | 0 | ||||
| -rw-r--r-- | c07/ex05/ft_split.c | 49 | ||||
| -rw-r--r-- | c07/main.c | 17 | ||||
| -rw-r--r-- | c08/ex04/ft_stock_str.h | 23 | ||||
| -rw-r--r-- | c08/ex04/ft_strs_to_tab.c | 29 |
18 files changed, 257 insertions, 58 deletions
diff --git a/c04/ex05/ft_atoi_base.c b/c04/ex05/ft_atoi_base.c index 9a89fc1..c8a3c2e 100644 --- a/c04/ex05/ft_atoi_base.c +++ b/c04/ex05/ft_atoi_base.c @@ -6,7 +6,7 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/06 08:07:17 by cacharle #+# #+# */ -/* Updated: 2019/07/07 13:26:28 by cacharle ### ########.fr */ +/* Updated: 2019/07/08 16:35:32 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -59,6 +59,7 @@ int position_in_base(char digit, char *base) return (i); } +// CHECK THE STR NOT JUST THE BASE (you idiot) int ft_atoi_base(char *str, char *base) { int radix; diff --git a/c05/ex00/ft_iterative_factorial.c b/c05/ex00/ft_iterative_factorial.c index 3773b9d..c8f4a35 100644 --- a/c05/ex00/ft_iterative_factorial.c +++ b/c05/ex00/ft_iterative_factorial.c @@ -6,14 +6,13 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/06 16:48:21 by cacharle #+# #+# */ -/* Updated: 2019/07/08 12:06:59 by cacharle ### ########.fr */ +/* Updated: 2019/07/08 17:22:07 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ int ft_iterative_factorial(int nb) { int acc; - int i; if (nb < 0) return (0); diff --git a/c05/ex03/ft_recursive_power.c b/c05/ex03/ft_recursive_power.c index 8339768..7c710d6 100644 --- a/c05/ex03/ft_recursive_power.c +++ b/c05/ex03/ft_recursive_power.c @@ -6,17 +6,16 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/06 17:39:43 by cacharle #+# #+# */ -/* Updated: 2019/07/08 12:07:33 by cacharle ### ########.fr */ +/* Updated: 2019/07/08 13:44:24 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ int ft_recursive_power(int nb, int power) { - if (power < 0) - return (0); if (power == 0) return (1); - if (power == 1) - return (nb); - return (nb * ft_recursive_power(nb, power - 1)); + if (power > 0) + return (nb * ft_recursive_power(nb, power - 1)); + else + return (ft_recursive_power(nb, power + 1) / nb); } diff --git a/c05/ex05/ft_sqrt.c b/c05/ex05/ft_sqrt.c index a72af2d..aef9630 100644 --- a/c05/ex05/ft_sqrt.c +++ b/c05/ex05/ft_sqrt.c @@ -6,20 +6,28 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/06 20:06:48 by cacharle #+# #+# */ -/* Updated: 2019/07/08 12:08:36 by cacharle ### ########.fr */ +/* Updated: 2019/07/09 06:38:18 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -int ft_sqrt(int nb) +int bin_search_sqrt(long unsigned int nb, unsigned int low, unsigned int up) { - int i; + long unsigned int mid; + + mid = low + ((up - low) / 2); + if (mid * mid == nb) + return (mid); + if (low > up) + return (0); + if (mid * mid < nb) + return (bin_search_sqrt(nb, mid + 1, up)); + else + return (bin_search_sqrt(nb, low, mid - 1)); +} - i = 0; - while (i <= nb) - { - if (i * i == nb) - return (i); - i++; - } - return (0); +int ft_sqrt(int nb) +{ + if (nb < 0) + return (0); + return (bin_search_sqrt(nb, 0, nb)); } diff --git a/c05/ex06/ft_is_prime.c b/c05/ex06/ft_is_prime.c index dc06fa5..73ca59b 100644 --- a/c05/ex06/ft_is_prime.c +++ b/c05/ex06/ft_is_prime.c @@ -6,13 +6,14 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/06 20:17:59 by cacharle #+# #+# */ -/* Updated: 2019/07/08 12:08:46 by cacharle ### ########.fr */ +/* Updated: 2019/07/09 10:17:58 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ int ft_is_prime(int nb) { - int i; + long unsigned int i; + long unsigned int nbu; if (nb <= 1) return (0); @@ -20,12 +21,15 @@ int ft_is_prime(int nb) return (1); if (nb % 2 == 0 || nb % 3 == 0) return (0); - i = 5; - while (i * i <= nb) + i = 1; + nbu = nb; + while (i * i <= nbu) { - if (nb % i == 0 || nb % (i + 2) == 0) + if (nbu % (i * 6 - 1) == 0) return (0); - i += 6; + if (nbu % (i * 6 + 1) == 0) + return (0); + i += 1; } return (1); } diff --git a/c05/ex07/ft_find_next_prime.c b/c05/ex07/ft_find_next_prime.c index e17ca94..80227da 100644 --- a/c05/ex07/ft_find_next_prime.c +++ b/c05/ex07/ft_find_next_prime.c @@ -6,13 +6,14 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/07 07:50:11 by cacharle #+# #+# */ -/* Updated: 2019/07/08 12:09:16 by cacharle ### ########.fr */ +/* Updated: 2019/07/09 10:20:25 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ int is_prime(int nb) { - int i; + long unsigned int i; + long unsigned int nbu; if (nb <= 1) return (0); @@ -20,12 +21,15 @@ int is_prime(int nb) return (1); if (nb % 2 == 0 || nb % 3 == 0) return (0); - i = 5; - while (i * i <= nb) + i = 1; + nbu = nb; + while (i * i <= nbu) { - if (nb % i == 0 || nb % (i + 2) == 0) + if (nbu % (i * 6 - 1) == 0) return (0); - i += 6; + if (nbu % (i * 6 + 1) == 0) + return (0); + i += 1; } return (1); } @@ -32,12 +32,15 @@ int main() printf("%d^%d = %d\n", 2, 2, ft_iterative_power(2, 4)); printf("%d^%d = %d\n", 3, 3, ft_iterative_power(3, 3)); printf("%d^%d = %d\n", 4, 5, ft_iterative_power(4, 5)); + printf("%d^%d = %d\n", -4, 5, ft_iterative_power(-4, 5)); printf("---------------------\n"); + printf("%d^%d = %d\n", 1, -9, ft_recursive_power(1, -9)); printf("%d^%d = %d\n", 2, 0, ft_recursive_power(2, 0)); printf("%d^%d = %d\n", 2, 2, ft_recursive_power(2, 4)); printf("%d^%d = %d\n", 3, 3, ft_recursive_power(3, 3)); printf("%d^%d = %d\n", 4, 5, ft_recursive_power(4, 5)); + printf("%d^%d = %d\n", -4, 5, ft_iterative_power(-4, 5)); printf("---------------------\n"); printf("F%d = %d\n", -1, ft_fibonacci(-1)); @@ -45,6 +48,8 @@ int main() printf("F%d = %d\n", 1, ft_fibonacci(1)); printf("F%d = %d\n", 2, ft_fibonacci(2)); printf("F%d = %d\n", 3, ft_fibonacci(3)); + printf("F%d = %d\n", 4, ft_fibonacci(4)); + printf("F%d = %d\n", 5, ft_fibonacci(5)); printf("F%d = %d\n", 8, ft_fibonacci(8)); printf("F%d = %d\n", 30, ft_fibonacci(30)); /*printf("F%d = %d\n", 41, ft_fibonacci(41));*/ @@ -53,8 +58,16 @@ int main() printf("sqrt(%d) = %d\n", -36, ft_sqrt(-36)); printf("sqrt(%d) = %d\n", 0, ft_sqrt(0)); printf("sqrt(%d) = %d\n", 4, ft_sqrt(4)); - printf("sqrt(%d) = %d\n", 9, ft_sqrt(4)); + printf("sqrt(%d) = %d\n", 7, ft_sqrt(7)); + printf("sqrt(%d) = %d\n", 9, ft_sqrt(9)); printf("sqrt(%d) = %d\n", 678, ft_sqrt(678 * 678)); + printf("sqrt(%d) = %d\n", 5555 * 5555, ft_sqrt(5555 * 5555)); + printf("sqrt(%d) = %d\n", 10000 * 10000, ft_sqrt(10000 * 10000)); + /*for (int i = 0; i < INT_MAX; i++)*/ + /*if (ft_sqrt(i))*/ + /*printf("sqrt(%d) = %d\n", i, ft_sqrt(i));*/ + + printf("---------------------\n"); printf("prime(%d) = %d\n", 3, ft_is_prime(3)); @@ -63,14 +76,17 @@ int main() printf("prime(%d) = %d\n", 21, ft_is_prime(21)); printf("prime(%d) = %d\n", 36, ft_is_prime(36)); printf("prime(%d) = %d\n", 2147483617, ft_is_prime(2147483617)); - /*printf("prime(%d) = %d\n", 2147483629, ft_is_prime(2147483629));*/ - /*for (int i = INT_MAX; i > INT_MAX - 100; i--)*/ + printf("prime(%d) = %d\n", 2147483629, ft_is_prime(2147483629)); + printf("prime(%d) = %d\n", 2147483647, ft_is_prime(2147483647)); + printf("prime(%d) = %d\n", 899, ft_is_prime(899)); + printf("prime(%d) = %d\n", 289, ft_is_prime(289)); + /*for (int i = INT_MAX; i > INT_MAX - 1000; i--)*/ /*printf("%d is %d\n", i, ft_is_prime(i));*/ printf("---------------------\n"); printf("nextp(%d) = %d\n", 21, ft_find_next_prime(21)); printf("nextp(%d) = %d\n", 23, ft_find_next_prime(23)); printf("nextp(%d) = %d\n", 2147483600, ft_find_next_prime(2147483600)); - /*for (int i = INT_MAX; i > INT_MAX - 100; i--)*/ + /*for (int i = INT_MAX; i > INT_MAX - 1000000; i--)*/ /*printf("%d is %d\n", i, ft_find_next_prime(i));*/ } diff --git a/c06/bonjour b/c06/bonjour Binary files differdeleted file mode 100755 index 2b50ec2..0000000 --- a/c06/bonjour +++ /dev/null diff --git a/c07/ex00/ft_strdup.c b/c07/ex00/ft_strdup.c index c63bd27..e53a3af 100644 --- a/c07/ex00/ft_strdup.c +++ b/c07/ex00/ft_strdup.c @@ -6,7 +6,7 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/07 15:39:50 by cacharle #+# #+# */ -/* Updated: 2019/07/08 07:49:52 by cacharle ### ########.fr */ +/* Updated: 2019/07/09 07:36:10 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,7 +28,7 @@ char *ft_strdup(char *src) int i; char *dup_ptr; - dup_ptr = malloc(sizeof(char) * ft_strlen(src)); + dup_ptr = (char*)malloc(sizeof(char) * ft_strlen(src)); if (dup_ptr == NULL) { errno = ENOMEM; diff --git a/c07/ex01/ft_range.c b/c07/ex01/ft_range.c index be171f3..669f663 100644 --- a/c07/ex01/ft_range.c +++ b/c07/ex01/ft_range.c @@ -6,18 +6,20 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/07 16:03:47 by cacharle #+# #+# */ -/* Updated: 2019/07/08 08:03:28 by cacharle ### ########.fr */ +/* Updated: 2019/07/09 07:46:19 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -int *ft_range(int min, int max) +#include <stdlib.h> + +int *ft_range(int min, int max) { int i; int *range; if (min >= max) return (NULL); - range = malloc(sizeof(int) * (max - min)); + range = (int*)malloc(sizeof(int) * (max - min)); i = 0; while (i < max - min) { diff --git a/c07/ex02/ft_ultimate_range.c b/c07/ex02/ft_ultimate_range.c index 141889c..d254ed1 100644 --- a/c07/ex02/ft_ultimate_range.c +++ b/c07/ex02/ft_ultimate_range.c @@ -6,13 +6,13 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/07 16:09:06 by cacharle #+# #+# */ -/* Updated: 2019/07/08 08:38:29 by cacharle ### ########.fr */ +/* Updated: 2019/07/09 07:46:19 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include <stdlib.h> -int ft_ultimate_range(int **range, int min, int max) +int ft_ultimate_range(int **range, int min, int max) { int i; diff --git a/c07/ex03/ft_strjoin.c b/c07/ex03/ft_strjoin.c index 1264000..ea97bb6 100644 --- a/c07/ex03/ft_strjoin.c +++ b/c07/ex03/ft_strjoin.c @@ -6,7 +6,7 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/07 16:14:25 by cacharle #+# #+# */ -/* Updated: 2019/07/08 11:08:00 by cacharle ### ########.fr */ +/* Updated: 2019/07/09 09:08:52 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ int my_strlen(char *str) int i; i = 0; - while(str[i]) + while (str[i]) i++; return (i); } @@ -31,12 +31,7 @@ int cummulative_strlen(int size, char **strs) i = 0; while (size > 0) { - i = 0; - while (strs[size - 1][i]) - { - i++; - len++; - } + len += my_strlen(strs[size - 1]); size--; } return (len); @@ -51,7 +46,7 @@ char *ft_strjoin(int size, char **strs, char *sep) char *join; join = (char*)malloc(sizeof(char) * (cummulative_strlen(size, strs) - + my_strlen(sep) * (size - 1))); + + my_strlen(sep) * (size - 1) + 1)); j = 0; l = 0; while (l < size) diff --git a/c07/ex04/ft_convert_base.c b/c07/ex04/ft_convert_base.c new file mode 100644 index 0000000..f50289a --- /dev/null +++ b/c07/ex04/ft_convert_base.c @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_convert_base.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/08 16:16:53 by cacharle #+# #+# */ +/* Updated: 2019/07/09 09:05:47 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int check_base(char *base) +{ + int i; + int j; + + i = 0; + while (base[i]) + { + if (base[i] == '-' || base[i] == '+' || base[i] == ' ' + || base[i] == '\t' || base[i] == '\n' || base[i] == '\v' + || base[i] == '\f' || base[i] == '\r') + return (0); + j = 0; + while (base[j]) + { + if (j != i && base[j] == base[i]) + return (0); + j++; + } + i++; + } + if (i < 2) + return (0); + return (1); +} + +int ft_pow(int base, int exponent) +{ + int accumulator; + + accumulator = 1; + while (exponent-- > 0) + accumulator *= base; + return (accumulator); +} + +int ft_atoi_base(char *nbr, char *base) +{ + +} + +char *ft_convert_base(char *nbr, char *base_from, char *base_to) +{ + int converted_nb; + char *converted_to + + if (!check_base(base_from) || !check_base(base_to)) + return (NULL); + converted_nb = ft_atoi_base(nbr, base_from); + printf("%d\n", convert_nb); + /*converted_to = ft_itoa_base(converted_nb, base_to);*/ + /*return (converted_to);*/ +} diff --git a/c07/ex04/ft_convert_base2.c b/c07/ex04/ft_convert_base2.c new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c07/ex04/ft_convert_base2.c diff --git a/c07/ex05/ft_split.c b/c07/ex05/ft_split.c new file mode 100644 index 0000000..da2d7fc --- /dev/null +++ b/c07/ex05/ft_split.c @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/08 15:58:03 by cacharle #+# #+# */ +/* Updated: 2019/07/09 09:40:49 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <stdlib.h> + +int count_segment(char *str, char *charset) +{ + int counter; + int i; + + counter = 0; + while (*str) + { + i = 0; + while (charset[i]) + if (str++ == charset[i++]) + counter++; + str++; + } + return (counter); +} + +int strlen_until_sep(char *str, char *charset) +{ + +} + +char **ft_split(char *str, char *charset) +{ + char **strs; + + strs = (char**)malloc(sizeof(char*) * count_segment(str, charset)); + printf("%d\n", count_segment(str, charset)); + /*while (*str)*/ + /*{*/ + + /*str++;*/ + /*}*/ + return (strs); +} @@ -6,7 +6,7 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/08 08:19:02 by cacharle #+# #+# */ -/* Updated: 2019/07/08 11:00:55 by cacharle ### ########.fr */ +/* Updated: 2019/07/09 09:42:11 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ #include "ex02/ft_ultimate_range.c" #include "ex03/ft_strjoin.c" /*#include "ex04/ft_strdup.c"*/ -/*#include "ex05/ft_strdup.c"*/ +#include "ex05/ft_split.c" int main() { @@ -48,14 +48,21 @@ int main() printf("\n "); printf("---------------\n"); - char *strs[] = {"bonjour", "je", "suis", "charle"}; + char *strs[] = {"bon", "je", "suis", "charles"}; char *sep = ", "; int size = 4; char *join; - /*printf("%d\n", cummulative_strlen(size, strs));*/ join = ft_strjoin(size, strs, sep); - printf("%s\n", join); + printf("[%d] %s\n", my_strlen(join), join); + /*for (int i = 0; i < my_strlen(join) + 1; i++)*/ + /*printf("%d ", join[i]);*/ free(join); + printf("\n---------------\n"); + char *str = "bon,je,suis,charles"; + char *charset = ","; + char **sstrs = ft_split(str, charset); + free(sstrs); + return 0; } diff --git a/c08/ex04/ft_stock_str.h b/c08/ex04/ft_stock_str.h new file mode 100644 index 0000000..ccadd2f --- /dev/null +++ b/c08/ex04/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/08 16:56:02 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/ex04/ft_strs_to_tab.c b/c08/ex04/ft_strs_to_tab.c index bc4c42f..67a0304 100644 --- a/c08/ex04/ft_strs_to_tab.c +++ b/c08/ex04/ft_strs_to_tab.c @@ -6,8 +6,35 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/07 17:15:32 by cacharle #+# #+# */ -/* Updated: 2019/07/07 17:16:52 by cacharle ### ########.fr */ +/* Updated: 2019/07/08 17:10:21 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ +#include <stdlib.h> +#include "ft_stock_str.h" +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 + + strs_stocks = malloc(sizeof(t_stock_str) * ac); + while (ac-- > 0) + { + 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; + } + return (strs_stocks); +} |
