Difference between revisions of "Finite State Machine"

From Progzoo
Jump to: navigation, search
(Even Binary Numbers)
(Even Binary Numbers)
Line 2: Line 2:
 
==Even Binary Numbers==
 
==Even Binary Numbers==
  
<question class='P'>
+
<question class='EvenBinary'>
 
<prog><![CDATA[
 
<prog><![CDATA[
 
public class EvenBinary {
 
public class EvenBinary {
  
public static void main(String[] args) {
+
  public static void main(String[] args) {
String [] ls = {"10", "111", "01"};  
+
    String [] ls = {"10", "111", "01"};  
for (String s : ls)
+
    for (String s : ls)
System.out.printf("%10s, %s\n", s,sm(s));
+
      System.out.printf("%10s, %s\n", s,sm(s));
}
+
}
 
 
public enum States {START,FAIL,A,B};
+
  public enum States {START,FAIL,A,B};
public static boolean sm(String s)
+
  public static boolean sm(String s)
{
+
  {
States state = States.START;
+
    States state = States.START;
int i=0;
+
    int i=0;
while (i<s.length() && state!=States.FAIL)
+
    while (i<s.length() && state!=States.FAIL)
{
+
    {
char c = s.charAt(i);
+
      char c = s.charAt(i);
switch (state){
+
      switch (state){
case START:
+
        case START:
if (c=='1')
+
          if (c=='1')
state = States.B;
+
            state = States.B;
else
+
          else
state = States.FAIL;
+
            state = States.FAIL;
break;
+
          break;
case A:
+
        case A:
if (c=='0')
+
          if (c=='0')
state = States.A;
+
            state = States.A;
else if (c=='1')
+
          else if (c=='1')
state = States.B;
+
            state = States.B;
state = States.FAIL;
+
          else
break;
+
            state = States.FAIL;
case B:
+
          break;
if (c=='0')
+
        case B:
state = States.A;
+
          if (c=='0')
else if (c=='1')
+
            state = States.A;
state = States.B;
+
          else if (c=='1')
else
+
            state = States.B;
state = States.FAIL;
+
          else
break;
+
            state = States.FAIL;
}
+
          break;
i++;
+
      }
}
+
      i++;
return state==States.A;
+
    }
}
+
    return state==States.A;
 +
  }
 
}
 
}
 
]]></prog>
 
]]></prog>

Revision as of 19:24, 16 February 2012

Finite State Machines

Even Binary Numbers

The function fact takes a single integer input. The result is that number times by each of the numbers smaller - down to one.

fact(1) =   1         =   1
fact(2) =   1*2       =   2
fact(3) =   1*2*3     =   6
fact(4) =   1*2*3*4   =  24
fact(5) =   1*2*3*4*5 = 120

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

What is the function


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


What is the function


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

What is the function


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