-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTape.java
More file actions
57 lines (49 loc) · 1.62 KB
/
Copy pathTape.java
File metadata and controls
57 lines (49 loc) · 1.62 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
import java.util.LinkedList;
public class Tape {
private final LinkedList<String> tape = new LinkedList<>(); // La bande de l'automate.
private int index = 0; // L'index de la position courante de la tete de lecture.
public int getIndex() {
return index;
}
public Tape(String entry) {
// Creation de la bande à partir d'une chaine de caractères initiale.
if (entry.length() == 0) {
this.tape.add("#");
return;
}
for (int i = 0; i < entry.length(); i++) {
this.tape.add(String.valueOf(entry.charAt(i)));
}
}
public String read() {
// Retourne le caractère à la position courante de la tete de lecture.
return tape.get(index);
}
public void write(String replace, String direction) {
// Remplace le caractère à la position courante de la tete de lecture.
tape.set(index, replace);
// Déplace la tete de lecture dans la direction indiquée par la variable.
if (direction.equals("L")) {
--index;
if (index < 0) {
tape.addFirst("#");
index = 0;
}
}
if (direction.equals("R")) {
++index;
if (index >= tape.size() - 1) {
tape.addLast("#");
}
}
}
@Override
public String toString() {
// Retourne la bande sous forme de chaine de caractères.
StringBuilder strBuffer = new StringBuilder();
for (String str : tape) {
strBuffer.append(str);
}
return strBuffer.toString();
}
}