-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_atoi_base.c
92 lines (83 loc) · 2.21 KB
/
ft_atoi_base.c
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
86
87
88
89
90
91
92
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kprytkov <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/05/04 18:03:58 by kprytkov #+# #+# */
/* Updated: 2018/05/04 18:03:59 by kprytkov ### ########.fr */
/* */
/* ************************************************************************** */
#include "./includes/fdf.h"
static int i_will_power(int nb, int power)
{
if (power == 0)
return (1);
else if (power < 0)
return (0);
return (nb * i_will_power(nb, power - 1));
}
static int is_correct_input(char c)
{
if (c >= '0' && c <= '9')
return (1);
else if (c >= 'a' && c <= 'z')
return (2);
else if (c >= 'A' && c <= 'Z')
return (3);
else
return (0);
}
static int i_will_convert(char c, int base)
{
int result;
int is_correct;
result = 0;
is_correct = 0;
is_correct = is_correct_input(c);
if (is_correct == 1)
result = c - 48;
else if (is_correct == 2)
result = c - 97 + 10;
else if (is_correct == 3)
result = c - 65 + 10;
else
result = -1;
if (result < base && result != -1)
return (result);
else
return (-1);
}
static int length_number(char *str)
{
int counter;
counter = 0;
while (str[counter])
{
if (is_correct_input(str[counter]) == 0)
break ;
counter++;
}
return (counter);
}
int ft_atoi_base(char *nb, int base)
{
int result;
int length;
int i;
i = 0;
result = 0;
if (base == 10)
return (ft_atoi(nb));
while ((nb[i] == ' ') || (nb[i] == '\n') || (nb[i] == '\t'))
i++;
length = length_number(nb) - 1;
while (*nb && length >= 0 && i_will_convert(*nb, base) != -1)
{
result += i_will_convert(*nb, base) * i_will_power(base, length);
nb++;
length--;
}
return (result);
}