暑假用Java写的一段很简单的识别浮点数的代码。不想翻译了就直接放上来,大家对照状态图看源程序便知道是怎么回事了。
EBNF:
<floating-point)->{(0|1|2|3|4|5|6|7|8|9)}+.{(0|1|2|3|4|5|6|7|8|9)}+[e(+|-){(0|1|2|3|4|5|6|7|8|9)}+]
Definition of floating-point literals in Java:
Java对于浮点数的定义如下:
A floating point literal have the following parts: a decimal integer, a decimal point, a fraction, an exponent (starts with an “e” or E followed by an integer, which could be preceded by “+” or “-”).
A floating-point literal can have the following parts: a decimal integer, a decimal point (“.”), a fraction (another decimal number), an exponent, and a type suffix. The exponent part is an “e” or “E” followed by an integer, which can be signed (preceded by “+” or “-”). A floating-point literal must have at least one digit, plus either a decimal point or “e” (or “E”).
According to the definition, the state diagram of floating-point parser is:
有穷状态机图如下:
Click to view the source code. 点击查看源代码。package fp; import java.io.Console; /** * * @author Ivon */ public class Main { char nextChar; public static void main(String[] args) { int x=0, y=0, z=0; /*x is used to record the position of the decimal point * y is used to recored the position of "e" and "E" */ Console c = System.console(); if (c == null) { System.err.println("No console."); System.exit(1); } String thenumber = c.readLine("Enter a number: "); if (isDigit(thenumber,0)== -1){ System.out.println(thenumber + " is an integer."); }else //The next step is to judge if the input is only a number with decimal point { x=isDigit(thenumber,0); if (thenumber.charAt(x)== '.'){ x=isNumberWithPoint(thenumber,x); if (x>=0) { if (thenumber.charAt(x)=='e'||thenumber.charAt(x)=='E'){ isValidExp(thenumber,x); }else{ System.out.println(thenumber + " is not a valid float point number."); } } }else if(thenumber.charAt(x)== 'E'||thenumber.charAt(x)== 'e'){ isValidExp(thenumber, x); }else{ System.out.println(thenumber + " is not a valid float point number."); } } } public static int isDigit(String s, int number){ int isDigitFlag=-999; for (int i=number; i<s.length();i++){ if ( !Character.isDigit(s.charAt(i)) ) { isDigitFlag = i; break; }else{ isDigitFlag = -1; } } return (isDigitFlag); } public static int isNumberWithPoint(String thenumber,int x){ if (x+1==thenumber.length()){ System.out.println(thenumber+" is a number with decimal point."); return -1; } if (isDigit(thenumber, x+1) == -1){ System.out.println(thenumber+" is a number with decimal point."); return -1; }else{ return isDigit(thenumber, x+1); //return the position of the next non-digit } } public static int isValidExp(String thenumber, int x){ if (x+1==thenumber.length()){ System.out.println(thenumber+" is a valid float point number."); return 1; } if(thenumber.charAt(x+1)=='+'||thenumber.charAt(x+1)=='-'){ x++; } if (isDigit(thenumber, x+1) == -1){ System.out.println(thenumber+" is a valid float point number."); }else{ System.out.println(thenumber+" is not a valid float point number."); } return 1; } }Test Case:
以下是测试用例:Integer:
11 – Pass
hello - Fail
$$$ - fail
Number with float point:
11.11 – Pass
11..11 – fail
1abc1 – fail
Float Point Number:
11e – Pass
11w – fail
11e11 – pass
11ee11 – fail
11e-1 – pass
11e+1 – pass
11e++1 – pass
11e+1w – fail
Should you have any better approach, please don't hesitate to correct me.
如果有误,欢迎指正

4 Comments
源程序呢?
话说你现在用JAVA啦?
是不是外国人写程序都用JAVA哒?
回复
ivon 八月 8th, 2010:
恩?源程序我放着了,很短的一个东西
国外政府和商用软件全是Java
但也不是最流行,各有千秋。。。
回复
。。。我看到了。。。研究下。。。
回复
好精悍的程序呀。。。考虑的真完全。。。
不过有没有可能是负整数或者负浮点乃?
还有跟我一样不喜欢用递归。。。
回复
ivon 八月 10th, 2010:
hmmm….I forgot that part…. My teacher just asked us to test positive numbers… But that’s easy to fix, just make the prefix “-” or “+” and valid input and… that’s it…
回复
ps突然发现我在同昨天的你讲话。。。
回复