Difference between revisions of "Finite State Machine"

From Progzoo
Jump to: navigation, search
(Even Binary Numbers)
 
(5 intermediate revisions by the same user not shown)
Line 52: Line 52:
 
</question>
 
</question>
  
==What is the function==
 
  
<question class='P'>
+
==Even Binary Numbers - Abstracted==
 +
 
 +
<question className='EvenBinary'>
 
<prog><![CDATA[
 
<prog><![CDATA[
public class P{
+
import java.util.HashMap;
   public static void main(String argv[])
+
public class EvenBinary{
 +
 
 +
   public static void main(String[] args) {
 +
    String [] ls = {"10", "111", "01"};
 +
    for (String s : ls)
 +
      System.out.printf("%10s, %s\n", s,sm(s));
 +
}
 +
 +
  public enum States {S,A,B};
 +
  public static boolean sm(String s)
 
   {
 
   {
    for (int i=0;i<5;i++)
+
    HashMap<States,HashMap<String,States>> d =  
      System.out.printf("%d%8d\n",i,f(i));
+
      new HashMap<States,HashMap<String,States>>();
  }
+
    d.put(States.S,new HashMap<String,States>());
 
+
    d.put(States.A,new HashMap<String,States>());
  static int f(int i){
+
    d.put(States.B,new HashMap<String,States>());
     if (i==0)
+
    d.get(States.S).put("1",States.B);
       return 0;
+
    d.get(States.A).put("0",States.A);
     else
+
    d.get(States.A).put("1",States.B);
      return 2+f(i-1);
+
    d.get(States.B).put("0",States.A);
 +
    d.get(States.B).put("1",States.B);
 +
   
 +
    States state = States.S;
 +
    int i=0;
 +
    while (i<s.length() && state!=null)
 +
     {
 +
      String c = s.substring(i,i+1);
 +
      state = d.get(state).get(c);
 +
       i++;
 +
     }
 +
    return state==States.A;
 
   }
 
   }
 
}
 
}
Line 73: Line 94:
 
</question>
 
</question>
  
 +
==Divisible by Three (decimal)==
  
==What is the function==
+
<question className='Div3'>
 
+
<question class='P'>
+
 
<prog><![CDATA[
 
<prog><![CDATA[
public class P{
 
  public static void main(String argv[])
 
  {
 
    for (int i=0;i<5;i++)
 
      System.out.printf("%d%8d\n",i,f(i));
 
  }
 
  
   static int f(int i){
+
public class Div3 {
     if (i==0)
+
 
      return 1;
+
   public static void main(String[] args) {
     else
+
     String [] ls = {"10", "111", "01","123"};  
       return 2*f(i-1);
+
     for (String s : ls)
 +
       System.out.printf("%10s, %s\n", s,sm(s));
 
   }
 
   }
}
+
  public enum States {FAIL,A,B,C};
]]></prog>
+
   public static boolean sm(String s)
</question>
+
==What is the function==
+
 
+
<question class='P'>
+
<prog><![CDATA[
+
public class P{
+
   public static void main(String argv[])
+
 
   {
 
   {
    for (int i:new int[]{2, 42, 111, 2753})
+
    States state = States.A;
      System.out.printf("%5d%8d\n",i,f(i));
+
    int i=0;
  }
+
    while (i<s.length() &&
 
+
        state!=States.FAIL)
  static int f(int i){
+
    {
    if (i==0)
+
      char c = s.charAt(i);
      return 0;
+
      switch (state){
    else
+
      case A:
       return (i%10)+f(i/10);
+
        if (c=='0' || c=='3' || c=='6' || c=='9')
 +
          state = States.A;
 +
        else if (c=='1'||c=='4'||c=='7')
 +
          state = States.B;
 +
        else if (c=='2'||c=='5'||c=='8')
 +
          state = States.C;
 +
        break;
 +
      case B:
 +
        if (c=='0' || c=='3' || c=='6' || c=='9')
 +
          state = States.B;
 +
        else if (c=='1'||c=='4'||c=='7')
 +
          state = States.C;
 +
        else if (c=='2'||c=='5'||c=='8')
 +
          state = States.A;
 +
        break;
 +
       case C:
 +
        if (c=='0' || c=='3' || c=='6' || c=='9')
 +
          state = States.C;
 +
        else if (c=='1'||c=='4'||c=='7')
 +
          state = States.A;
 +
        else if (c=='2'||c=='5'||c=='8')
 +
          state = States.B;
 +
        break;
 +
      }
 +
      i++;
 +
    }
 +
    return state==States.A;
 
   }
 
   }
 
}
 
}
 
 
 
]]></prog>
 
]]></prog>
 
</question>
 
</question>

Latest revision as of 16:03, 1 March 2012

Finite State Machines

Even Binary Numbers


[Font] [Default] [Show] [Resize] [History] [Profile]


Even Binary Numbers - Abstracted


[Font] [Default] [Show] [Resize] [History] [Profile]

Divisible by Three (decimal)


[Font] [Default] [Show] [Resize] [History] [Profile]