Difference between revisions of "Finite State Machine"

From Progzoo
Jump to: navigation, search
(Even Binary Numbers - Absracted)
 
(2 intermediate revisions by the same user not shown)
Line 53: Line 53:
  
  
==Even Binary Numbers - Abtsracted==
+
==Even Binary Numbers - Abstracted==
  
 
<question className='EvenBinary'>
 
<question className='EvenBinary'>
Line 94: Line 94:
 
</question>
 
</question>
  
==What is the function==
+
==Divisible by Three (decimal)==
  
<question class='P'>
+
<question className='Div3'>
 
<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 0;
+
    else
+
      return 2+f(i-1);
+
  }
+
}
+
]]></prog>
+
</question>
+
  
 
+
   public static void main(String[] args) {
==What is the function==
+
    String [] ls = {"10", "111", "01","123"};
 
+
    for (String s : ls)
<question class='P'>
+
      System.out.printf("%10s, %s\n", s,sm(s));
<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));
+
 
   }
 
   }
 
+
   public enum States {FAIL,A,B,C};
   static int f(int i){
+
   public static boolean sm(String s)
    if (i==0)
+
      return 1;
+
    else
+
      return 2*f(i-1);
+
  }
+
}
+
]]></prog>
+
</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]