diff options
| author | Charles <sircharlesaze@gmail.com> | 2019-07-06 12:48:23 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2019-07-06 12:48:23 +0200 |
| commit | 79f8ba0b777f3361002ed2ae0c6c6f8f353ca731 (patch) | |
| tree | 34d7248fd9de69886f93349d1ebce2da8b892839 /c04/ex05/ft_atoi_base.c | |
| parent | d732047d6681b1f64f7907d1c0413abdaab76050 (diff) | |
| download | piscine-79f8ba0b777f3361002ed2ae0c6c6f8f353ca731.tar.gz piscine-79f8ba0b777f3361002ed2ae0c6c6f8f353ca731.tar.bz2 piscine-79f8ba0b777f3361002ed2ae0c6c6f8f353ca731.zip | |
c04 first tests (not clean)
Diffstat (limited to 'c04/ex05/ft_atoi_base.c')
| -rw-r--r-- | c04/ex05/ft_atoi_base.c | 29 |
1 files changed, 19 insertions, 10 deletions
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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); } |
