The initial array and each flag
. 1 Private Final int [] [] State_table; 2 Private int Now_state = 0 ; . 3 Private Final int Char_m = 0; // represents 0-9 49-48 . 4 Private Final int Char_d =. 1; // represents 45 characters. . 5 Private Final int Char_e = 2; // represents the character e 69/101 . 6 Private Final int Char_j =. 3; // represents - character 46 is . 7 // spaces 32 . 8 . 9 Private Final int=. 1 Int_result_code; // Integer 10 Private Final int Float_result_code =. 3; // float . 11 Private Final int Scientif_result_code =. 6; // scientific notation 12 is Private Final int Scientif_result_code_2 =. 7; // scientific notation 13 // 0245 state is not output
State_table two-dimensional array is referred to in the previous chapter, for storing each state and the corresponding conversion relationships.
Now_state shows a state in which the current, where the state is initialized to 0.
The vertical axis represents the value of each Char_ *
The remaining output indicates the state can
Array initialization
1 FSM(){ 2 State_table=new int[8][4]; 3 int[] b={1,1,3,3,7,6,6,7}; 4 5 for(int i=0;i<8;i++) 6 Arrays.fill(State_table[i],-1);//填满 7 State_table[1][1]=2; 8 State_table[1][2]=4; 9 State_table[3][2]=4; 10 State_table[4][3]=5; 11 for(int i=1;i<=8;i++) 12 State_table[i-1][0]=b[i-1]; 13 }
My name is called FSM class so this is a constructor
Use Arrays.fill can easily fill all the array -1. However, this method only supports an array. So here we use a for loop to iterate over an array of 8
The final array like this
1 -1 -1 -1
1 2 4 -1
3 -1 -1 -1
3 -1 4 -1
7 -1 -1 5
6 -1 -1 -1
6 -1 -1 -1
7 -1 -1 -1
Analyzing the input line and each character belonging to (0 or 1? 2? 3?) 0123 corresponding to the defined at the outset Char_ *
1 //对输入的字符串进行类型判断 2 public void Input_Classificantion(String Nowline){ 3 for(int i=0;i<Nowline.length();i++) { 4 if(Nowline.charAt(i)>='0'&&Nowline.charAt(i)<='9') 5 Change_state(Now_state,Char_m ); 6 else if(Nowline.charAt(i)=='.') 7 Change_state(Now_state, Char_d); 8 else if(Nowline.charAt(i)=='e'||Nowline.charAt(i)=='E') 9 { Check_science(Nowline,i); 10 Change_state(Now_state, Char_e); 11 } 12 else if((Nowline.charAt(i)=='-')) 13 Change_state(Now_state, Char_j); 14 else if(Nowline.charAt(i)==' ') { 15 //排除同行的空格 16 do { 17 i=Nowline.indexOf(' ',i)+1;} 18 while(Nowline.charAt(i)==' '); 19 i--; 20 Input(); 21 } 22 is the else 23 is {System.out.println ( "input contains illegal characters" ); 24 Erroe_code =. 1 ;} 25 } 26 is 27 the Input (); 28 }
Here there have been a total of three methods, namely,
Change_state(int Old_state,int Char_code)
Input()
Check_science
Change_state(int Old_state,int Char_code)
This method is used in which the current state and the corresponding two-dimensional table Char_ * using specific implementation as follows
. 1 Private Boolean the Output = to false ; // whether the output flag 2 Private Boolean the Check = to false ; // Check whether the norms written in scientific notation . 3 . 4 Private int Erroe_code = 0;
1 // to read the corresponding value determined by the current state of the value of the array 2 Private void Change_state ( int old_state, int Char_code) { . 3 Now_state = State_table [old_state] [Char_code]; . 4 . 5 IF (Now_state Now_state == 0 || || 2. 4 Now_state == == || == Now_state. 5) // non-output state . 6 the output = to false ; . 7 the else IF (Now_state == -. 1 ) { . 8 System.out.println ( "input error" ); . 9 Erroe_code = 2 ; 10 System.exit (. 1 ); . 11 } 12 is the else // output state 13 is the Output = to true ; 14 15 }
Input()
1 // The type of the current state of the output corresponding to this state 2 Private void the Input () { . 3 IF (the Output == to true ) { . 4 IF (Now_state == Int_result_code) . 5 System.out.println ( "integer" ); . 6 the else IF (Now_state == Float_result_code) . 7 System.out.println ( "float" ); . 8 the else IF (the Check) { . 9 IF (Now_state == Scientif_result_code) 10 negative System.out.println ( "scientific notation " ); 11 the else IF (Now_state == Scientif_result_code_2) 12 is System.out.println ( "scientific notation" ); 13 is } 14 the else 15 System.out.println ( "scientific notation format error" ); 16 } . 17 the else { 18 is the System. out.println ( "input error" ); . 19 Erroe_code = 2 ; 20 is System.exit (. 1 ); 21 is } 22 is Now_state = 0 ; 23 is } 24
Check flag which represents the scientific notation format compliance