add ft_atof
This commit is contained in:
42
srcs/ft_atof.c
Normal file
42
srcs/ft_atof.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <stdio.h>
|
||||
#include "libft.h"
|
||||
|
||||
double ft_atof(const char *str)
|
||||
{
|
||||
double nbr;
|
||||
int i;
|
||||
int negatif;
|
||||
double fraction;
|
||||
|
||||
i = 0;
|
||||
negatif = 1;
|
||||
nbr = 0.0;
|
||||
fraction = 1.0;
|
||||
|
||||
// Skip leading whitespace
|
||||
while ((str[i] == ' ') || (str[i] > 8 && str[i] < 14))
|
||||
i++;
|
||||
|
||||
// Handle optional sign
|
||||
if (str[i] == '-')
|
||||
negatif = -1;
|
||||
if (str[i] == '+' || str[i] == '-')
|
||||
i++;
|
||||
|
||||
// Parse integer part
|
||||
while (str[i] >= '0' && str[i] <= '9')
|
||||
nbr = nbr * 10.0 + (str[i++] - '0');
|
||||
|
||||
// Parse fractional part if '.' is present
|
||||
if (str[i] == '.')
|
||||
{
|
||||
i++;
|
||||
while (str[i] >= '0' && str[i] <= '9')
|
||||
{
|
||||
fraction /= 10.0;
|
||||
nbr += (str[i++] - '0') * fraction;
|
||||
}
|
||||
}
|
||||
|
||||
return (nbr * negatif);
|
||||
}
|
||||
@@ -12,22 +12,29 @@
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
int ft_atoi(const char *str)
|
||||
int ft_atoi(const char *str)
|
||||
{
|
||||
long nbr;
|
||||
int i;
|
||||
int negatif;
|
||||
long nbr;
|
||||
int i;
|
||||
int negatif;
|
||||
|
||||
i = 0;
|
||||
negatif = 1;
|
||||
nbr = 0;
|
||||
|
||||
// Skip leading whitespace
|
||||
while ((str[i] == ' ') || (str[i] > 8 && str[i] < 14))
|
||||
i++;
|
||||
|
||||
// Handle optional sign
|
||||
if (str[i] == '-')
|
||||
negatif = -1;
|
||||
if (str[i] == '+' || str[i] == '-')
|
||||
i++;
|
||||
|
||||
// parse integer
|
||||
while (str[i] >= '0' && str[i] <= '9')
|
||||
nbr = nbr * 10 + (str[i++] - '0');
|
||||
|
||||
return (nbr * negatif);
|
||||
}
|
||||
|
||||
@@ -1,22 +1,29 @@
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
long ft_atol(const char *str)
|
||||
long ft_atol(const char *str)
|
||||
{
|
||||
long long nbr;
|
||||
int i;
|
||||
int negatif;
|
||||
long long nbr;
|
||||
int i;
|
||||
int negatif;
|
||||
|
||||
i = 0;
|
||||
negatif = 1;
|
||||
nbr = 0;
|
||||
|
||||
// Skip leading whitespace
|
||||
while ((str[i] == ' ') || (str[i] > 8 && str[i] < 14))
|
||||
i++;
|
||||
|
||||
// Handle optional sign
|
||||
if (str[i] == '-')
|
||||
negatif = -1;
|
||||
if (str[i] == '+' || str[i] == '-')
|
||||
i++;
|
||||
|
||||
// parse integer
|
||||
while (str[i] >= '0' && str[i] <= '9')
|
||||
nbr = nbr * 10 + (str[i++] - '0');
|
||||
|
||||
return (nbr * negatif);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user