eg:"12.34"返回12.34,“123.34”返回123.34,函数原型:double my_atof(char *str)
#include <stdio.h>
#include <ctype.h>
double my_atof(char *str)
{
int flag = 1;
int count = 1;
double num = 0;
double value = 0;
while(isspace(*str))
{
*str++;
}
if(*str == '-')
{
flag = -1;
*str++;
}
if(*str == '+')
{
*str++;
}
while(*str != '\0'&&*str != '.')
{
value = value * 10 + (*str - '0');
*str++;
}
*str++;
while(*str != '\0')
{
count *= 10;
num = num * 10 + (*str - '0');
*str++;
}
num = num/count;
return flag * (value + num);
}
int main(void)
{
char *str1 = "12.34";
char *str2 = "-123.34";
printf("%f\n",my_atof(str1));
printf("%f\n",my_atof(str2));
return 0;
}