80 lines
1.8 KiB
C
80 lines
1.8 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* ft_atoi_base.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2022/07/19 14:48:20 by cchauvet #+# #+# */
|
|
/* Updated: 2022/07/19 19:01:16 by cchauvet ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
int ft_baser(char *str)
|
|
{
|
|
int i;
|
|
int j;
|
|
int count;
|
|
|
|
i = -1;
|
|
while (str[++i] != 0)
|
|
{
|
|
count = 0;
|
|
j = 0;
|
|
while (str[j] != 0)
|
|
if (str[i] == str[j++])
|
|
count++;
|
|
if (count > 1 || str[i] == '+' || str[i] == '-')
|
|
return (0);
|
|
}
|
|
if (i == 1)
|
|
return (0);
|
|
return (i);
|
|
}
|
|
|
|
int ft_find(char *str, char to_find)
|
|
{
|
|
int i;
|
|
|
|
i = 0;
|
|
while (str[i] != 0)
|
|
{
|
|
if (str[i] == to_find)
|
|
return (i);
|
|
i++;
|
|
}
|
|
return (-1);
|
|
}
|
|
|
|
int ft_atoi_base(char *str, char *base)
|
|
{
|
|
int base_size;
|
|
int sign;
|
|
int nbr;
|
|
int i;
|
|
|
|
nbr = 0;
|
|
sign = 1;
|
|
base_size = ft_baser(base);
|
|
i = 0;
|
|
if (base == 0)
|
|
return (0);
|
|
while (str[i] == '+' || str[i] == '-' || str[i] == ' ')
|
|
if (str[i++] == '-')
|
|
sign = sign * -1;
|
|
while (ft_find(base, str[i]) != -1)
|
|
{
|
|
nbr = nbr * base_size + ft_find(base, str[i]);
|
|
str++;
|
|
}
|
|
return (nbr * sign);
|
|
}
|
|
/*
|
|
#include <stdio.h>
|
|
|
|
int main()
|
|
{
|
|
printf("%d", ft_atoi_base(" ++----10gdg", "01"));
|
|
}
|
|
*/
|