-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturingmachine.java
72 lines (60 loc) · 1.97 KB
/
turingmachine.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
64
65
66
67
68
69
70
71
72
import java.util.Arrays;
import java.util.Scanner;
public class TuringMachine {
public static void main(String[] args) {
int[][] transitions = {
{1, 5, 4, 5, 3},
{1, 2, 5, 5, 1},
{2, 5, 5, 0, 2},
{5, 5, 4, 5, 3},
{4, 4, 4, 4, 4},
{5, 5, 5, 5, 5}
};
String[] symbols = {"0", "1", " ", "x", "y"};
int[][] pointer = {
{1, 1, 0, 1, 1},
{1, 0, 1, 1, 1},
{0, 1, 1, 1, 0},
{1, 1, 0, 1, 1},
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1}
};
int[][] replace = {
{3, 3, 3, 3, 4},
{0, 4, 3, 3, 4},
{0, 3, 3, 3, 4},
{0, 1, 2, 3, 4},
{0, 1, 2, 3, 4},
{0, 1, 2, 3, 4}
};
int state = 0;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the string: ");
String input = scanner.nextLine();
input = input + " ";
char[] tape = input.toCharArray();
int head = 0;
System.out.print("Tape: ");
System.out.println(Arrays.toString(tape));
System.out.println("State = " + state + ", Head = " + head);
while (state < 4) {
int c = state;
int symbol = Arrays.asList(symbols).indexOf(String.valueOf(tape[head]));
tape[head] = symbols[replace[c][symbol]].toCharArray()[0];
state = transitions[c][symbol];
if (pointer[c][symbol] == 0) {
head -= 1;
} else {
head += 1;
}
System.out.print("Tape: ");
System.out.println(Arrays.toString(tape));
System.out.println("State = " + state + ", Head = " + head);
if (state == 4) {
System.out.println("The given string passes");
} else {
System.out.println("The given string does not pass");
}
}
}
}