int myatoi(const char *nptr) // 把字符串转换成长整型数
{
int result = 0;
const char *p = nptr++;//保存初始地址,并自增一位
while (*nptr!='\0')
{
if (*nptr>='0'&&*nptr<='9')//第二位开始如果是字符0-9
{
result *= 10;
result+=*nptr - 48;//转换成相应的int整数
nptr++;//继续自增
}
else
{
break;//碰到非字符0-9退出循环
}
}
//根据首地址值输出对应的结果
if (*p == '+'||*p=='0')
{
return result;
}
else if (*p == '-')
{
return -result;
}
else
{
return 0;
}
}//end aoti函数
C++ atoi函数源码自我实现
猜你喜欢
转载自blog.csdn.net/haku_yyf/article/details/79604630
今日推荐
周排行