字符串转换成整数

来源:互联网 发布:ios的一个编程游戏 编辑:程序博客网 时间:2024/04/29 20:19
题目详情

输入一个表示整数的字符串,把该字符串转换成整数并输出,例如输入字符串"345",则输出整数345。

请完成函数StrToInt,实现字符串转换成整数的功能。



友情提醒

提交代码之前,请复查下你的程序,比如当给的字符串是如左边图片所示的时候,有考虑到么?

当然,它们各自对应的正确输出如右边图片所示(假定你是在32位系统下,编译环境是VS2008以上)

input:                                          output:

                           

庞果英雄会上的一题,如上描述,尝试解答如下:
public class StrToInt {public static int strToInt(String str) {// 首先,trim掉左右空格String strTrim = str.trim();// 返回值为0的情况if (strTrim.isEmpty() || strTrim.matches("(-|\\+)?[a-zA-Z]+")|| strTrim.matches("(-|\\+)?\\s+\\d+")|| strTrim.matches("-+-+\\d+")|| strTrim.matches("\\++\\++\\d+")|| strTrim.matches("-+-+[a-zA-Z]+")|| strTrim.matches("(-|\\+)+\\s+\\d+")|| strTrim.matches("\\++\\++[a-zA-Z]+")) {return 0;}// 带符号的正整数else if (strTrim.matches("\\++\\d+")) {            Long num=Long.parseLong(strTrim.substring(1));// 溢出时,返回最大值if (num>Integer.MAX_VALUE) {return Integer.MAX_VALUE;} else {return num.intValue();}}//不带符号的正整数else if(strTrim.matches("\\d+")){Long num=Long.parseLong(strTrim);// 溢出时,返回最大值if (num>Integer.MAX_VALUE) {return Integer.MAX_VALUE;} else {return num.intValue();}}// 带符号的负整数else if (strTrim.matches("-+\\d+")) {Long num=Long.parseLong(strTrim);// 溢出时,返回最小值if (num<Integer.MIN_VALUE) {return Integer.MIN_VALUE;} else {return num.intValue();}}// 带符号的先数字后字母的混合else if (strTrim.matches("(-|\\+)?\\d+([a-zA-Z]|\\s)+\\w*?")) {int index = 0;for (int i = 0; i < strTrim.length(); i++) {if (Character.isLetter(strTrim.charAt(i))|| Character.isSpaceChar(strTrim.charAt(i))) { // 用char包装类中的判断字母的方法判断每一个字符index = i;break;}}  Long num=Long.parseLong(strTrim.substring(0,index));// 溢出时,返回最大值if (num>Integer.MAX_VALUE) {return Integer.MAX_VALUE;} // 溢出时,返回最大值else if (num<Integer.MIN_VALUE) {return Integer.MIN_VALUE;} else {return num.intValue();}} else {System.out.println("你输入的字符串非法,不能转成数据型数据!返回值为0!");return 0;}}/** * @param args */public static void main(String[] args) {String str1 = "";String str2 = "1";String str3 = "+1";String str4 = "-1";String str5 = "123";String str6 = "-123";String str7 = "010";String str8 = "+00131204";String str9 = "-01324000";String str10 = "2147483647";String str11 = "-2147483647";String str12 = "-2147483648";String str13 = "2147483648";String str14 = "-2147483649";String str15 = "abc";String str16 = "-abc";String str17 = "1a";String str18 = "23a8f";String str19 = "-3924x8fc";String str20 = "   321";String str21 = "   -321";String str22 = "123  456";String str23 = "123   ";String str24 = "   - 321";String str25 = "   +4488";String str26 = "  +  413";String str27 = " ++c";String str28 = " ++1";String str29 = " --2";String str30 = "  -2";System.out.println("1结果="+strToInt(str1));System.out.println("2结果="+strToInt(str2));System.out.println("3结果="+strToInt(str3));System.out.println("4结果="+strToInt(str4));System.out.println("5结果="+strToInt(str5));System.out.println("6结果="+strToInt(str6));System.out.println("7结果="+strToInt(str7));System.out.println("8结果="+strToInt(str8));System.out.println("9结果="+strToInt(str9));System.out.println("10结果="+strToInt(str10));System.out.println("11结果="+strToInt(str11));System.out.println("12结果="+strToInt(str12));System.out.println("13结果="+strToInt(str13));System.out.println("14结果="+strToInt(str14));System.out.println("15结果="+strToInt(str15));System.out.println("16结果="+strToInt(str16));System.out.println("17结果="+strToInt(str17));System.out.println("18结果="+strToInt(str18));System.out.println("19结果="+strToInt(str19));System.out.println("20结果="+strToInt(str20));System.out.println("21结果="+strToInt(str21));System.out.println("22结果="+strToInt(str22));System.out.println("23结果="+strToInt(str23));System.out.println("24结果="+strToInt(str24));System.out.println("25结果="+strToInt(str25));System.out.println("26结果="+strToInt(str26));System.out.println("27结果="+strToInt(str27));System.out.println("28结果="+strToInt(str28));System.out.println("29结果="+strToInt(str29));System.out.println("30结果="+strToInt(str30));}}