数字字符串转为相应的浮点数

#include 
#include
double atof(char s[])
{
    double sum,power;
    int i,sign;
    for(i=0;isspace(s[i]);i++);
    sign=(s[i]=='-')?1:-1;
    if(s[i]=='+'||s[i]=='-')i++;
    for(sum=0;isdigit(s[i]);i++)
    sum=sum*10+s[i]-'0';
    if(s[i]=='.')i++;
    for(power=1.0;isdigit(s[i]);i++);
    {
    sum=sum*10+s[i]-'0';
    power=power*10.0;
    }
    return sign*sum/power;
}
int main()
{
    char s[20];
    char c;
    int i=0;
    double m;
    gets(s);
    m=atof(s);
    printf("%lf",m);
 } 

你可能感兴趣的:(c)