diff options
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); } |
