From 79f8ba0b777f3361002ed2ae0c6c6f8f353ca731 Mon Sep 17 00:00:00 2001 From: Charles Date: Sat, 6 Jul 2019 12:48:23 +0200 Subject: c04 first tests (not clean) --- c04/ex02/ft_putnbr.c | 7 ++++++- c04/ex03/ft_atoi.c | 16 +++++++++++++--- c04/ex04/ft_putnbr_base.c | 47 ++++++++++++++++++++++++++--------------------- c04/ex05/ft_atoi_base.c | 29 +++++++++++++++++++---------- c04/main.c | 40 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 104 insertions(+), 35 deletions(-) (limited to 'c04') diff --git a/c04/ex02/ft_putnbr.c b/c04/ex02/ft_putnbr.c index 8a13dc5..4ba6a0e 100644 --- a/c04/ex02/ft_putnbr.c +++ b/c04/ex02/ft_putnbr.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/06 07:24:04 by cacharle #+# #+# */ -/* Updated: 2019/07/06 08:34:48 by cacharle ### ########.fr */ +/* Updated: 2019/07/06 08:47:01 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,6 +23,11 @@ void ft_putnbr(int nb) int rev_digits[100]; unsigned int nbu; + if (nb == 0) + { + ft_putchar('0'); + return; + } if (nb < 0) { ft_putchar('-'); diff --git a/c04/ex03/ft_atoi.c b/c04/ex03/ft_atoi.c index 0d4247b..10f6b07 100644 --- a/c04/ex03/ft_atoi.c +++ b/c04/ex03/ft_atoi.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/06 07:31:38 by cacharle #+# #+# */ -/* Updated: 2019/07/06 08:36:47 by cacharle ### ########.fr */ +/* Updated: 2019/07/06 10:23:20 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,21 +28,31 @@ 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; + /*printf("> %s\n", str);*/ while (*str == '-' || *str == '+') { if (*str == '-') is_negative = !is_negative; str++; } + /*printf("> %s\n", str);*/ + nb = 0; i = 0; while (str[i] >= '0' && str[i] <= '9') - { - nb += pow10(i) * (str[i] - '0'); i++; + j = 0; + while (str[j] >= '0' && str[j] <= '9') + { + /*printf("%d i, %d nb\n", i, nb);*/ + i--; + nb += pow10(i) * (str[j] - '0'); + j++; } if (is_negative) nb = -nb; diff --git a/c04/ex04/ft_putnbr_base.c b/c04/ex04/ft_putnbr_base.c index 6827475..764c42b 100644 --- a/c04/ex04/ft_putnbr_base.c +++ b/c04/ex04/ft_putnbr_base.c @@ -6,28 +6,33 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/06 07:47:44 by cacharle #+# #+# */ -/* Updated: 2019/07/06 08:37:59 by cacharle ### ########.fr */ +/* Updated: 2019/07/06 11:12:25 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ +#include + int check_base(char *base) { int i; + int j; - if (!*base) - return (0); - while (*base) + i = 0; + while (base[i]) { - if (*base == '-' || *base == '+') + if (base[i] == '-' || base[i] == '+') return (0); - i = 0; - while (base[i]) + j = 0; + while (base[j]) { - if (base[i] == *base) + if (j != i && base[j] == base[i]) return (0); - i++; + j++; } + i++; } + if (i < 2) + return (0); return (1); } @@ -36,32 +41,32 @@ int my_strlen(char *str) int counter; counter = 0; - while (*str) - { + while (str[counter]) counter++; - str++; - } return (counter); } void ft_putnbr_base(int nbr, char *base) { - int radix; - int i; - int nb; - int rev_digits[1024]; + int radix; + int i; + int nb; + char rev_digits[1024]; if (!check_base(base)) return; radix = my_strlen(base); - nb = 0; i = 0; - while (nb > 0) + /*printf("\n%d base %d (%s)\n", nbr, radix, base);*/ + while (nbr > 0) { - rev_digits[i] = base[nb % radix]; - nb /= radix; + /*printf("%d %d %c\n", nbr, nbr % radix, base[nbr % radix]);*/ + rev_digits[i] = base[nbr % radix]; + nbr /= radix; i++; } + /*for (int k = 0; k < 10; k++)*/ + /*printf("%d ", rev_digits[k]);*/ while (i > 0) { i--; diff --git a/c04/ex05/ft_atoi_base.c b/c04/ex05/ft_atoi_base.c index b595a5c..5de5f81 100644 --- a/c04/ex05/ft_atoi_base.c +++ b/c04/ex05/ft_atoi_base.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/06 08:07:17 by cacharle #+# #+# */ -/* Updated: 2019/07/06 08:30:16 by cacharle ### ########.fr */ +/* Updated: 2019/07/06 12:47:25 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,6 +23,16 @@ int my_pow(int base, int exponent) return (accumulator); } +int position_in_base(char digit, char *base) +{ + int i; + + i = 0; + while (base[i] != digit) + i++; + return (i); +} + int ft_atoi_base(char *str, char *base) { int radix; @@ -30,19 +40,18 @@ int ft_atoi_base(char *str, char *base) int j; int nb; + nb = 0; radix = 0; - while (base[radix]) - radix++; + while (base[++radix]); i = 0; - while (str[i]) - i++; + while (str[++i]); + printf("%s base %d (%s)\n", str, radix, base); j = 0; - while (i > 0) + while (--i >= 0) { - nb += my_pow(radix, j); - nb %= radix; - i--; + printf("%c: %d * (%d^%d) = %d\n", str[i], position_in_base(str[i], base), radix, i, my_pow(radix, i) * position_in_base(str[j], base)); + nb += my_pow(radix, i) * position_in_base(str[j], base); j++; } - return nb; + return (nb); } diff --git a/c04/main.c b/c04/main.c index a165b3c..0e035a9 100644 --- a/c04/main.c +++ b/c04/main.c @@ -1,5 +1,6 @@ #include #include +#include #include "ex00/ft_strlen.c" #include "ex01/ft_putstr.c" #include "ex02/ft_putnbr.c" @@ -16,5 +17,44 @@ int main() ft_putstr(s); printf("\n"); + ft_putnbr(0); printf("\n"); + ft_putnbr(42); printf("\n"); + ft_putnbr(-42); printf("\n"); + ft_putnbr(INT_MAX); printf("\n"); + ft_putnbr(INT_MIN); printf("\n"); + char *s_happypath = "42"; + char *s_happypathn = "-42"; + char *s_int0 = "0"; + char *s_intmax = "2147483647"; + char *s_intmin = "-2147483648"; + char *spaces = " \t\n\v\f\r 43"; + char *neg = " -+--++36"; + char *pos = "++--+++--4"; + char *garbage_tail = "76iqu21#!@"; + char *all = "\n\t \v++++---12341234#3%^@"; + printf("%d\n", ft_atoi(s_happypath)); + printf("%d\n", ft_atoi(s_happypathn)); + printf("%d\n", ft_atoi(s_int0)); + printf("%d\n", ft_atoi(s_intmax)); + printf("%d\n", ft_atoi(s_intmin)); + printf("%d\n", ft_atoi(spaces)); + printf("%d\n", ft_atoi(neg)); + printf("%d\n", ft_atoi(pos)); + printf("%d\n", ft_atoi(garbage_tail)); + printf("%d\n", ft_atoi(all)); + + printf("----------------------\n"); + // doit gerer les nombres negatifs ?? + ft_putnbr_base(42, "0123456789"); printf("\n"); + ft_putnbr_base(42, "01"); printf("\n"); + ft_putnbr_base(42, "0123456789abcdef"); printf("\n"); + ft_putnbr_base(42, "01234567"); printf("\n"); + ft_putnbr_base(INT_MAX - 5, "01"); printf("\n"); + ft_putnbr_base(INT_MAX, ""); + ft_putnbr_base(INT_MAX, "a"); + + printf("----------------------\n"); + printf("%d\n", ft_atoi_base("111000", "01")); + printf("%d\n", ft_atoi_base("ff", "0123456789abcdef")); } -- cgit