首页 > 其他 > 详细

atoi

时间:2015-03-16 16:23:46      阅读:202      评论:0      收藏:0      [点我收藏+]
	public int atoi(String str) {
		int f = 1;
		int result = 0;
		boolean start = false;
		for (int i = 0; i < str.length(); ++i) {
			if (start) {
				if (str.charAt(i) < '0' || str.charAt(i) > '9') {
					break;
				} else {
					if (f == 1 && result >= Integer.MAX_VALUE / 10) {
						if (result > Integer.MAX_VALUE / 10
								|| (str.charAt(i) - '0') > Integer.MAX_VALUE % 10) {
							return Integer.MAX_VALUE;
						}
					} else if (f == -1 && f * result <= Integer.MIN_VALUE / 10) {
						if (f * result < Integer.MIN_VALUE / 10
								|| (str.charAt(i) - '0') > (0 - (Integer.MIN_VALUE % 10))) {
							return Integer.MIN_VALUE;
						}
					}
					result = result * 10 + (str.charAt(i) - '0');
				}
			} else {
				if (str.charAt(i) == ' ') {
					continue;
				} else if (str.charAt(i) == '-') {
					f = -1;
				} else if (str.charAt(i) == '+') {
					f = 1;
				} else if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {
					result = str.charAt(i) - '0';
				} else {
				    break;
				}
				start=true;
			}
		}
		return f * result;

	}
}


atoi

原文:http://blog.csdn.net/youdianmengxiangba/article/details/44307417

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!