简易小程序(将字符型数据转换为浮点型数据)
#include<stdio.h>
#include<ctype.h>
#define SIZE 1000
double my_atof(char const *str)
{
float ret = 0;
float temp = 0;
int sign = 0;
while(isspace(*str))
str++;
if(*str == '-')
{
sign = -1;
str++;
}
else
{
sign = 1;
}
while(isdigit(*str))
{
ret = ret * 10 +(*str-'0');
str++;
}
while(*str != '\0')
{
NULL;
str++;
}
while(isdigit(*(--str)))
{
temp = temp / 10 +(*str-'0');
}
temp = temp / 10;
return sign * (ret + temp);
}
int main()
{
float number;
char *string = "35.00001";
number = my_atof(string);
printf("%f\n",number);
return 0;
}