aboutsummaryrefslogtreecommitdiff
path: root/c04/ex05/ft_atoi_base.c
blob: 9a89fc11284a5d3d9f68089c6b28d8d2bd676454 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_atoi_base.c                                     :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   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       */
/*                                                                            */
/* ************************************************************************** */

int	check_the_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	my_pow(int base, int exponent)
{
	int	accumulator;

	accumulator = 1;
	while (exponent > 0)
	{
		accumulator *= base;
		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;
	int	i;
	int	j;
	int	nb;

	if (!check_the_base(base))
		return (0);
	nb = 0;
	radix = 0;
	while (base[radix])
		radix++;
	i = 0;
	while (str[i])
		i++;
	j = 0;
	while (--i >= 0)
	{
		nb += my_pow(radix, i) * position_in_base(str[j], base);
		j++;
	}
	return (nb);
}