-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMooreMachine
More file actions
64 lines (46 loc) · 1.89 KB
/
MooreMachine
File metadata and controls
64 lines (46 loc) · 1.89 KB
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
64
import java.io.*;
public class Moore_Machine {
public static void main(String args[]) throws Exception{
System.out.println("----VALUES AS READ FROM THE FILE----\n");
FileReader fr = new FileReader("E:\\Work\\IntelliJ Idea Projects\\src\\InputMoore.txt");
BufferedReader br = new BufferedReader(fr);
int startS = Integer.parseInt(br.readLine());
String inputs[] = br.readLine().split(",");
int q = Integer.parseInt(br.readLine());
int mat[][] = new int[q][inputs.length+1];
for(int i=0; i<q ; i++){
String str[] = br.readLine().split(" ");
int cnt=0;
for(int j=0; j<inputs.length+1; j++){
mat[i][j] = Integer.parseInt(str[cnt++]);
}
}
System.out.println("Start State = " + startS);
System.out.print("\nInputs = ");
for (String x : inputs)
System.out.print(x + ",");
System.out.println("\nTransition Matrix : \n");
for(int i=0; i<q; i++) {
for (int j = 0; j < inputs.length + 1; j++)
System.out.print(mat[i][j] + " ");
System.out.println();
}
System.out.println("\n-------------------------------");
System.out.println("Enter sentence :");
String sent = new BufferedReader(new InputStreamReader(System.in)).readLine();
String sum= Integer.toString(mat[startS][inputs.length]);
for(int i=0;i<sent.length();i++){
startS = nextState(startS, sent.charAt(i), mat, inputs);
sum += mat[startS][inputs.length];
}
System.out.println("Output is : "+ sum);
}
static int nextState(int startS, char s , int mat[][], String inputs[]){
for(int i=0;i<inputs.length;i++){
if(inputs[i].equals(s+"")){
return mat[startS][i];
}
}
return -1;
}
}