-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoore_Machine.java
63 lines (49 loc) · 1.28 KB
/
Moore_Machine.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.util.Scanner;
public class Moore_Machine {
private static Scanner input = new Scanner(System.in);
public static void main(String[] args)
{
System.out.println("Enter value of divisor: ");
int divisor = input.nextInt();
int tran[][] = new int[divisor][2];
System.out.print("Enter Starting state No: ");
int start = input.nextInt();
getTransitions(tran);
while(true)
{
System.out.print("Enter Divident: ");
int divident = input.nextInt();
System.out.println(divident + " mode " + divisor + " = " + getReminder(tran, Integer.toBinaryString(divident), start));
}//while
}//main()
static void getTransitions(int[][] tran)
{
int i=0;
for(int[] row : tran)
{
System.out.println("Enter transitions at State " + i + ":");
int j=0;
for(int col : row)
{
System.out.print("\tFor " + j + ": ");
tran[i][j] = input.nextInt();
j++;
}//for
i++;
}//for
}//getTransitions()
static int getReminder(int[][] tran, String str, int start)
{
int state = start;
int i=0;
while(i<str.length())
{
if(str.charAt(i) == '1')
state = tran[state][1];
else
state = tran[state][0];
i++;
}//while
return state;
}//getReminder
}//class