⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fatest.java

📁 creating a finite automata
💻 JAVA
字号:
import java.util.*;

 class State{
        String state_ID;
        String state_type;
     State(String id,String type){
          state_ID=id;
          state_type=type;
      }
     
    public String toString(){
       return (this.state_ID);
    }
 }//end of class State

 class Alphabet{
       char alpha;
    Alphabet(char a){
         alpha=a;
    }
   public boolean equals(Object o){
       boolean result=false;
       Alphabet a=(Alphabet)o;
       if(this.alpha==a.alpha){
          result=true; 
       }
      return result;
   }
    
 }//end of class Alphabet


 class FA{

       State states[]=new State[9];
       Alphabet alphabets[]=new Alphabet[8];
       Map FA=new HashMap();
  
       public void input_State(){
         String id="q0";
         String type="start";
         states[0]=new State(id,type);

         for(int i=1;i<4;i++){
            id="q"+i;
            type="none";
            states[i]=new State(id,type);
          }  
        for(int i=4;i<6;i++){
         id="q"+i;
         type="accept";
         states[i]=new State(id,type);
        }
        for(int i=6;i<8;i++){
         id="q"+i;
         type="none";
         states[i]=new State(id,type);
        }

         id="q8";
         type="accept";
         states[8]=new State(id,type);

      }//end of  input_State() 

       public void input_Alphabet(){
             
       
        alphabets[0]=new Alphabet('r');
        alphabets[1]=new Alphabet('e');
        alphabets[2]=new Alphabet('a');
        alphabets[3]=new Alphabet('d');
        alphabets[4]=new Alphabet('s');
        alphabets[5]=new Alphabet('i');
        alphabets[6]=new Alphabet('n');
        alphabets[7]=new Alphabet('g');
       
      }//end of  input_Alphabet() 
  
   public void input_Transition_Function(){
          
          for(int i=0;i<5;i++){
              State st=states[i];
              Alphabet al=alphabets[i];
              State value=states[i+1];
              Set key=new LinkedHashSet();
              key.add(al);
              key.add(st);
              FA.put(key,value);
              if(i==4){
                 st=states[i];
                 al=alphabets[i+1];
                 value=states[i+2];
                 key=new LinkedHashSet();
                 key.add(al);
                 key.add(st);
                 FA.put(key,value);

               }
         }
          for(int i=6;i<8;i++){
              State st=states[i];
              Alphabet al=alphabets[i];
              State value=states[i+1];
              Set key=new LinkedHashSet();
              key.add(al);
              key.add(st);
              FA.put(key,value);
         }
   }//end of input_Transition_Function()

  private State find_Next_State(Set key){
         State nextState=null;
        Object obj[]=new Object[2];
        int i=0;
        Iterator it=key.iterator();
        while(it.hasNext()){
           obj[i++]=it.next();       
        }
        i=0;
        Alphabet alpha_key=(Alphabet)obj[0];
        State state_key=(State)obj[1]; 
        
        Set set_Of_Keys=FA.keySet();
        Iterator it_1=set_Of_Keys.iterator();
       loop :  while(it_1.hasNext()){
           Set isKey=(Set)it_1.next();
           Iterator it_2=isKey.iterator();
           while(it_2.hasNext()){
              obj[i++]=it_2.next();       
            } 
           i=0;
        Alphabet alpha_iskey=(Alphabet)obj[0];
        State state_iskey=(State)obj[1];      
        if((alpha_key).equals(alpha_iskey) & (state_key.state_ID).equalsIgnoreCase(state_iskey.state_ID)){
            nextState=(State)FA.get(isKey);
            break loop;
          } 
        } 
      return nextState; 
    
     }//end of find_Next_State() 

   public void read(){
      String input_String;
      State start_State=states[0];
      
      System.out.println("Enter a String of Alphabets(r,e,a,d,s,i,n,g) for the Automata :: ");
      Scanner input=new Scanner(System.in);
      input_String=input.nextLine();
      System.out.println("The transition of states ::");
      System.out.print(start_State);
     for(int i=0;i<input_String.length();i++){
         Alphabet a=new Alphabet(input_String.charAt(i));
         Set temp=new LinkedHashSet();
         temp.add(a);
         temp.add(start_State);
         start_State=find_Next_State(temp);
         System.out.print("===>"+start_State);
     } 
       System.out.println();
   try{
      if((start_State.state_type).equalsIgnoreCase("accept")){
          System.out.println();
          System.out.println("The given String is successfully read.");
          System.out.println(input_String+" is a valid English word.");
         System.out.println("Category : Verb ");
         System.out.println("Root : "+input_String.substring(0,4));
          if(input_String.length()>4){
          System.out.println("Affix : "+input_String.substring(4));
          }
       }else{
          System.out.println("The given String is not recognized.");
       }  
   }catch(NullPointerException e){
       System.out.println("The given string is not recognized.");
      }
     
    }//end of read()
    

    
 }//end of FA 


  public class FATest{
   public static void main(String []args){
        FA fa=new FA();
        fa.input_State();
        fa.input_Alphabet();
        fa.input_Transition_Function();
        fa.read();
         
}
}
     

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -