atol的实现代码

来源:互联网 发布:淘宝网上怎么买处方药 编辑:程序博客网 时间:2024/05/22 02:07
long __cdecl atol(
        const char *nptr
        )
{
        int c;              /* current char */
        long total;        /* current total */
        int sign;          /* if '-', then negative, otherwise positive */

        /* skip whitespace */
        while ( isspace((int)(unsigned char)*nptr) )
            ++nptr;

        c = (int)(unsigned char)*nptr++;        sign = c;          /* save sign indication */
        if (c == '-' || c == '+')
            c = (int)(unsigned char)*nptr++;    /* skip sign */

        total = 0;

        while (isdigit(c)) {
            total = 10 * total + (c - '0');    /* accumulate digit */
            c = (int)(unsigned char)*nptr++;    /* get next char */
        }

        if (sign == '-')
            return -total;
        else
            return total;  /* return result, negated if necessary */
}
原创粉丝点击