/* Send the end pointer in case we need to bail out early */
if (endptr)
*endptr = (char *)nptr;
/* Bail out if the value is NULL */
if (!nptr)
return 0.0;
/* Skip white space to find the start of the number */
current = nptr;
while (*current != '\0' && isspace (*current & 0xFF))
++current;
start = current;
/* Verify that the number has a valid form */
if (*current == '-' || *current == '+')
++current;
if (*current >= '0' && *current <= '9')
{
while (*current >= '0' && *current <= '9')
++current;
}
else if (*current != '.')
{
return 0.0;
}
if (*current == '.')
++current;
while (*current >= '0' && *current <= '9')
++current;
if (*current == 'e' || *current == 'E')
{
++current;
if (*current == '-' || *current == '+')
++current;
if (*current < '0' || *current > '9')
return 0.0;
while (*current >= '0' && *current <= '9')
++current;
}
/* Set the end pointer appropriately */
if (endptr)
*endptr = (char *)current;
/* Use the C# library to convert the value into a double */
value = DoubleParse
(MarshalPtrToStringAnsi ((long)start, (int)(current - start)));
if (report_errors && DoubleIsInfinity (value))
errno = ERANGE;
return value;
}
#include
#include
double atof(char s[]);
int main()
{
char input_str[] = "-123.32e+6"; /*the input string to be converted*/
double s = atof(input_str);
printf("the converted value is: %f" , s);
}
double atof (char s[])
{
double val , val1 = 0 , power ;
int i ,j , sign , sign1;
for (i = 0 ; isspace(s[i]) ; i++) /* skip white spaces*/
;
sign = (s[i] == '-') ? -1 : 1; /*check for the sign of the number*/
if (s[i] == '+' || s[i] == '-') /* skip the sign*/
i++;
for (val = 0.0; isdigit(s[i]) ; i++)
val = 10.0 * val + (s[i] - '0');
if (s[i] == '.') /*skip the dot*/
i++;
for (power = 1.0; isdigit(s[i]) ; i++){
val = 10.0 * val + (s[i] - '0');
power *= 10.0;
}
val = val / power;
if (s[i] == 'e' || s[i] == 'E') /*skip the character*/
i++;
sign1 = (s[i] == '-') ? -1 : 1; /*check for the sign of the exponent*/
printf("%d\n",sign1);
if (s[i] == '+' || s[i] == '-') /*skip the sign*/
i++;
for (power = 1.0 ; isdigit(s[i]) ; i++){
val1 = 10.0 * val1 + (s[i] - '0');
}
for (j = 0; j < val1 ; j++)
{
power *= 10;
}
printf("%f\n" , power);
if(sign1 == -1)
{
return sign * val / power;
}
else
return sign * val * power;
}
atof from pnetc libc code
static double
strtod_internal (const char *nptr, char **endptr, int report_errors)
{
const char *current;
const char *start;
double value;
/* Send the end pointer in case we need to bail out early */
if (endptr)
*endptr = (char *)nptr;
/* Bail out if the value is NULL */
if (!nptr)
return 0.0;
/* Skip white space to find the start of the number */
current = nptr;
while (*current != '\0' && isspace (*current & 0xFF))
++current;
start = current;
/* Verify that the number has a valid form */
if (*current == '-' || *current == '+')
++current;
if (*current >= '0' && *current <= '9')
{
while (*current >= '0' && *current <= '9')
++current;
}
else if (*current != '.')
{
return 0.0;
}
if (*current == '.')
++current;
while (*current >= '0' && *current <= '9')
++current;
if (*current == 'e' || *current == 'E')
{
++current;
if (*current == '-' || *current == '+')
++current;
if (*current < '0' || *current > '9')
return 0.0;
while (*current >= '0' && *current <= '9')
++current;
}
/* Set the end pointer appropriately */
if (endptr)
*endptr = (char *)current;
/* Use the C# library to convert the value into a double */
value = DoubleParse
(MarshalPtrToStringAnsi ((long)start, (int)(current - start)));
if (report_errors && DoubleIsInfinity (value))
errno = ERANGE;
return value;
}
double
atof (const char *nptr)
{
return strtod_internal (nptr, (char **)0, 0);
}
simpler without e and all
float atof(char *s)
{
float num=0;
int radix=1;
bool dec=1;
bool neg=1;
if(*s == '-')
{
*s++;
neg=-1;
}
while(*s)
{
if(isdigit(*s))
{
num = 10.0 * num + (*s - '0');
if(!dec)
radix*=10;
}
else
dec=0;
*s++;
}
return (num/radix) * neg;
}
Implement the atof function
#include
#include
double atof(char s[]);
int main()
{
char input_str[] = "-123.32e+6"; /*the input string to be converted*/
double s = atof(input_str);
printf("the converted value is: %f" , s);
}
double atof (char s[])
{
double val , val1 = 0 , power ;
int i ,j , sign , sign1;
for (i = 0 ; isspace(s[i]) ; i++) /* skip white spaces*/
;
sign = (s[i] == '-') ? -1 : 1; /*check for the sign of the number*/
if (s[i] == '+' || s[i] == '-') /* skip the sign*/
i++;
for (val = 0.0; isdigit(s[i]) ; i++)
val = 10.0 * val + (s[i] - '0');
if (s[i] == '.') /*skip the dot*/
i++;
for (power = 1.0; isdigit(s[i]) ; i++){
val = 10.0 * val + (s[i] - '0');
power *= 10.0;
}
val = val / power;
if (s[i] == 'e' || s[i] == 'E') /*skip the character*/
i++;
sign1 = (s[i] == '-') ? -1 : 1; /*check for the sign of the exponent*/
printf("%d\n",sign1);
if (s[i] == '+' || s[i] == '-') /*skip the sign*/
i++;
for (power = 1.0 ; isdigit(s[i]) ; i++){
val1 = 10.0 * val1 + (s[i] - '0');
}
for (j = 0; j < val1 ; j++)
{
power *= 10;
}
printf("%f\n" , power);
if(sign1 == -1)
{
return sign * val / power;
}
else
return sign * val * power;
}