题目19:表示数值的字符串
题目描述:
请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串“+100”、“5e2”、“-123”、“3.1416”及“-1E-16”都表示数值,但“12e”、“1a3.14”、“1.2.3”、“+-5”及“12e+5.4”都不是。
题目分析:
表示数字的字符串遵循模式A[.[B]][e|EC]或者.B[e|EC],其中A为数值的整数部分,B紧跟着小数点为数值的小数部分,C紧跟着‘e’或者‘E’为数值的指数部分,在小数里面可能没有数值的整数部分,例如.123等于0.123,因此A不是必须的
判断一个字符是否符合上述模式时,首先尽可能多地扫描0~9的数位(有可能在起始处有‘+’、‘-’),也就是前面模式中表示数值整数A部分,如果遇到小数点‘.’,则开始扫描表示数值小数部分的B部分,如果遇到‘e’或者‘E’,则开始扫描表示数值指数的C部分。
代码实现:
package swordToOffer;
public class Num20_IsStringANumber {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("------1 false--------");
char[] str1 = null;
System.out.println(isStringNumeric(str1));
System.out.println("------2 false--------");
char[] str2 = {};
System.out.println(isStringNumeric(str2));
System.out.println("------3 false--------");
String str3 = "e.5";
char[] str3c= str3.toCharArray();
System.out.println(isStringNumeric(str3c));
System.out.println("-------4 false-------");
String str4 = "2e1.2";
char[] str4c = str4.toCharArray();
System.out.println(isStringNumeric(str4c));
System.out.println("-------5 true-------");
String str5 = "2e12";
char[] str5c = str5.toCharArray();
System.out.println(isStringNumeric(str5c));
System.out.println("-------6 true-------");
String str6 = "-2e12";
char[] str6c = str6.toCharArray();
System.out.println(isStringNumeric(str6c));
System.out.println("-------7 true-------");
String str7 = "-.2e2";
char[] str7c = str7.toCharArray();
System.out.println(isStringNumeric(str7c));
System.out.println("-------8 true-------");
String str8 = "-.2e-12";
char[] str8c = str8.toCharArray();
System.out.println(isStringNumeric(str8c));
System.out.println("-------9 true-------");
String str9 = "-1.e-2";
char[] str9c = str9.toCharArray();
System.out.println(isStringNumeric(str9c));
System.out.println("-------10 true-------");
String str10 = "-1.2";
char[] str10c = str10.toCharArray();
System.out.println(isStringNumeric(str10c));
System.out.println("-------11 true-------");
String str11 = "-.2";
char[] str11c = str11.toCharArray();
System.out.println(isStringNumeric(str11c));
}
/*
* 数字的基本格式为:(A.B E/e A) ,按顺序进行判断
* //A代表带符号整数,B代表不带符号整数
* 注意数组越界问题!
*/
public static boolean isStringNumeric(char[] str) {
if(str==null)
return false;
int[] index = new int[1];
index[0] = 0;//记录当前字符位置
boolean isNumeric = false; //记录是否是数值
//先判断A
isNumeric = isInteger(str,index);
//在判断B
if(index[0]<str.length&&(str[index[0]]=='.')) {
index[0]++;
//.B 和A.和 A.B形式均可匹配
isNumeric = isUnsignedInteger(str,index)||isNumeric;
}
//判断e后面的A 可+ -
if(index[0]<str.length&&(str[index[0]]=='e'||str[index[0]]=='E')) {
index[0]++;
isNumeric = isInteger(str,index)||isNumeric;
}
if(isNumeric&&index[0]==str.length) {
return true;
}else {
return false;
}
}
public static boolean isInteger(char[] str,int[] index) {
if(index[0]<str.length&&(str[index[0]]=='+'||str[index[0]]=='-')) {
index[0]++;
}
return isUnsignedInteger(str,index);
}
public static boolean isUnsignedInteger(char[] str,int[] index) {
int start = index[0];
//判断是否为数字 分别匹配A B A遮掩的数字
while(index[0]<str.length&&(str[index[0]]-'0'<=9&&str[index[0]]-'0'>=0)) {
index[0]++;
}
if(index[0]>start) {
return true;
}else {
return false;
}
}
}
考点分析:
- 对字符串的编程,使用String.toCharArray();
- 考虑数值整数、小数、指数部分的特点,那些部分可以出现正负号,那些部分不能出现