atoi函数实现代码

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;
		
	}

猜你喜欢

转载自blog.csdn.net/liuhao2415/article/details/106359166