跟小D每日学口语

atoi 的内部实现过程

//NULL, "", " ", "0", "1", "12", "123", "-123", "+123", " 12", "12 23", "12ab", "ab12", "222222222222", ''65535", "65536", "-65536", "4294967296"(232),"4294967295"(232 - 1),       "-231 - 1" ,  "231 - 1"

char * nptr = "123";

int c; /* current char */

int total; /* current total */

int sign; /* if ''-'', then negative, otherwise positive */

/* skip whitespace */

while ( isspace((int)*nptr) )

nptr++;

c = (int)*nptr++;

sign = c; /* save sign indication */

if (c == '-' || c == '+')

c = (int)*nptr++; /* skip sign */

total = 0;

while (isdigit(c)) {

total = 10 * total + (c - '0'); /* accumulate digit */

//total = 10 * total + (int)c; /* accumulate digit */

c = (int)*nptr++; /* get next char */

}

if (sign == '-')

return -total;

else

return total; /* return result, negated if necessary */

posted @ 2009-09-09 16:59  简简单单幸福  阅读(201)  评论(0编辑  收藏  举报