public static int atoi(String str)
{
int sign = 0;
int result=0;
if ((str.toCharArray()[0]-'0') > 9 && str.toCharArray()[0] != '-')
{
return 0;
}
if ( str.toCharArray()[0] == '-')
{
sign = 1;
}
for (int i = sign; i < str.toCharArray().length; i++)
{
if ((str.toCharArray()[i]-'0') > 9)
break;
result = (int) (result + (str.toCharArray()[i]-'0')*Math.pow(10, str.toCharArray().length-1-i));
}
int a = result;
if (a < 0)
return Integer.MAX_VALUE;
if (sign == 1) result = result * -1;
return result;
}
atoi函数实现代码
最新推荐文章于 2021-07-15 21:29:37 发布