long atol(char *s)
{
long ret = 0;
int sign = 0;
switch(*s)
{
case '-':
sign = 1;
//do not break here!
case '+':
s++;
break;
}
while(*s>='0' && *s<='9')
{
int n = *s++ - '0';
if(sign)
n = -n;
ret = ret*10 + n;
}
return ret;
}
函数中只使用了负数和零来进行计算,避免溢出。
atol()函数一个可移植的版本
最新推荐文章于 2017-09-07 17:57:03 发布