atof函数的实现

来源:互联网 发布:雄3导弹知乎 编辑:程序博客网 时间:2024/05/18 03:12
/*************************************************************************
> File Name: atof.c
> Author: zhanglp
> Mail: ql.zhanglp@qq.com
> Created Time: 2014年09月28日 星期日 13时59分55秒

 ************************************************************************/

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

double myAtof (const char *nptr)
{
    double  res_i = 0; // integer
    double  res_d = 1; // decimal
    const char    *p = nptr;

    while ( isdigit (*p) ) {

        res_i = res_i * 10 + (*p++ - '0');
    }


    if ( '.' == *p++ ) {

        while ( isdigit (*p) ) {
            
            res_i += (res_d /= 10) * (*p++ - '0');
        }
    }

    return res_i;
}


int main (void)
{
    const char  *str = "143.43254354353425345432";


    printf ("myAtof: %f\n", myAtof (str));
    printf ("atof  : %f\n", atof (str));

    return 0;

}

74.125.205.147

0 0