-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumericJudge.java
More file actions
134 lines (119 loc) · 4.65 KB
/
numericJudge.java
File metadata and controls
134 lines (119 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package offer;
public class numericJudge {
/**
* 题目:请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。
*
* @param string
* @return
*/
public static boolean isNumeric(String string) {
if (string == null || string.length() == 0) {
return false;
}
int index = 0;
if (string.charAt(index) == '+' || string.charAt(index) == '-') {
index++;
}
// 已经到达字符串的末尾了。只有正负号
if (index >= string.length()) {
return false;
}
boolean numeric = true;
//先找出非数字的位置
index = scanDigits(string, index);
// 还未到字符串的末尾
if (index < string.length()) {
// 如果是小数点
if (string.charAt(index) == '.') {
// 移动到下一个位置
index++;
index = scanDigits(string, index);
// 已经到了字符串的末尾了
if (index == string.length()) {
numeric = true;
}
// 还未到字符串结束位置
else if (index < string.length() &&
(string.charAt(index) == 'e' || string.charAt(index) == 'E')) {
numeric = isExponential(string, index);
} else {
//非e E ,而是其他非数字字符
numeric = false;
}
}
// 如果是指数标识
else if (string.charAt(index) == 'e' || string.charAt(index) == 'E') {
numeric = isExponential(string, index);
} else {
numeric = false;
}
return numeric;
}
// 已经到了字符串的末尾了,说明其没有指数部分
else {
return true;
}
}
/**
* 判断是否是科学计数法的结尾部分,如E5,e5,E+5,e-5,e(E)后面接整数
*
* @param string 字符串
* @param index 开始匹配的位置
* @return 匹配的结果
*/
private static boolean isExponential(String string, int index) {
/* if (index >= string.length() ||
(string.charAt(index) != 'e' && string.charAt(index) != 'E')) {
return false;
}*/
// 移动到下一个要处理的位置
index++;
// 到达字符串的末尾,就返回false
if (index >= string.length()) {
return false;
}
if (string.charAt(index) == '+' || string.charAt(index) == '-') {
index++;
}
// 到达字符串的末尾,就返回false
if (index >= string.length()) {
return false;
}
index = scanDigits(string, index);
// 如果已经处理到了的数字的末尾就认为是正确的指数
return index >= string.length();
}
/**
* 扫描字符串部分的数字部分
*
* @param string 字符串
* @param index 开始扫描的位置
* @return 从扫描位置开始第一个数字字符的位置
*/
private static int scanDigits(String string, int index) {
while (index < string.length() && string.charAt(index) >= '0' && string.charAt(index) <= '9') {
index++;
}
//返回的是非数字的下标,当index == length说明去全部都是数字
return index;
}
public static void main(String[] args) {
System.out.println(isNumeric("100a") + "[" + true + "]");
System.out.println(isNumeric("123.45e+6") + "[" + true + "]");
System.out.println(isNumeric("+500") + "[" + true + "]");
System.out.println(isNumeric("5e2") + "[" + true + "]");
System.out.println(isNumeric("3.1416") + "[" + true + "]");
System.out.println(isNumeric("600.") + "[" + true + "]");
System.out.println(isNumeric("-.123") + "[" + true + "]");
System.out.println(isNumeric("-1E-16") + "[" + true + "]");
System.out.println(isNumeric("100") + "[" + true + "]");
System.out.println(isNumeric("1.79769313486232E+308") + "[" + true + "]");
System.out.println();
System.out.println(isNumeric("12e+") + "[" + false + "]");
System.out.println(isNumeric("1a3.14") + "[" + false + "]");
System.out.println(isNumeric("1+23") + "[" + false + "]");
System.out.println(isNumeric("1.2.3") + "[" + false + "]");
System.out.println(isNumeric("+-5") + "[" + false + "]");
System.out.println(isNumeric("12e+5.4") + "[" + false + "]");
}
}