Aufgabe1_KellerautomatTest überarbeitet.

This commit is contained in:
romanschenk37 2022-04-06 23:19:13 +02:00
parent 5309a7a555
commit 43539ca662
2 changed files with 179 additions and 32 deletions

View File

@ -1,4 +1,7 @@
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Aufgabe1_Kellerautomat { public class Aufgabe1_Kellerautomat {
private static String[] words = {"ZZO", "ZZOZZOO", "ZZZOO", "ZZOO", "ZZOZZO", "ZOZ"}; private static String[] words = {"ZZO", "ZZOZZOO", "ZZZOO", "ZZOO", "ZZOZZO", "ZOZ"};
@ -10,17 +13,21 @@ public class Aufgabe1_Kellerautomat {
} }
public static boolean calculate(String word) { public static boolean calculate(String word) {
char[] wordChars = word.toCharArray(); List<Character> wordChars = new ArrayList<>();
for (char symbol : word.toCharArray()) {
wordChars.add(symbol);
}
ArrayList<Character> stack = new ArrayList<>(); ArrayList<Character> stack = new ArrayList<>();
stack.add('$'); stack.add('$');
int state = 0; //case 0 - 5 = q0 - q5, case -1 = Abfallzustand int state = 0; //case 0 - 5 = q0 - q5, case -1 = Abfallzustand
for (char symbol : wordChars) { while (wordChars.size() > 0 || state >= 3) {
char symbol;
Character stackSymbol = stack.remove(stack.size() - 1); Character stackSymbol = stack.remove(stack.size() - 1);
switch (state) { switch (state) {
case 0: case 0:
if (symbol == 'Z' && stackSymbol == '$') { if (wordChars.remove(0) == 'Z' && stackSymbol == '$') {
stack.add('$'); stack.add('$');
stack.add('Z'); stack.add('Z');
state = 1; state = 1;
@ -30,7 +37,7 @@ public class Aufgabe1_Kellerautomat {
} }
break; break;
case 1: case 1:
if (symbol == 'Z' && stackSymbol == 'Z') { if (wordChars.remove(0) == 'Z' && stackSymbol == 'Z') {
stack.add('Z'); stack.add('Z');
stack.add('Z'); stack.add('Z');
state = 2; state = 2;
@ -40,6 +47,7 @@ public class Aufgabe1_Kellerautomat {
} }
break; break;
case 2: case 2:
symbol = wordChars.remove(0);
if (symbol == 'O' && stackSymbol == 'Z') { if (symbol == 'O' && stackSymbol == 'Z') {
state = 3; state = 3;
} else if (symbol == 'Z' && stackSymbol == 'Z') { } else if (symbol == 'Z' && stackSymbol == 'Z') {
@ -56,43 +64,54 @@ public class Aufgabe1_Kellerautomat {
} }
break; break;
case 3: case 3:
if (symbol == 'O' && stackSymbol == 'Z') { if (stackSymbol == 'Z') {
state = 3;
} else if (symbol == 'Z' && stackSymbol == 'Z') {
stack.add('Z'); stack.add('Z');
stack.add('Z');
state = 2;
} else {
dontAccept(word);
return false;
}
break;
}
}
if (state == 3 && stack.size() >= 1 && stack.remove(stack.size() - 1) == 'Z') {
state = 4; state = 4;
} else { } else {
dontAccept(word); dontAccept(word);
return false; return false;
} }
break;
if (state == 4 && stack.size() >= 1 && stack.remove(stack.size() - 1) == '$') { case 4:
if (wordChars.size() > 0) {
symbol = wordChars.remove(0);
if (symbol == 'Z' && stackSymbol == 'Z') {
stack.add('Z');
stack.add('Z');
state = 2;
} else if (symbol == 'O' && stackSymbol == 'Z') {
state = 3;
} else {
dontAccept(word);
return false;
}
} else {
if (stackSymbol == 'Z') {
state = 5; state = 5;
} else { } else {
dontAccept(word); dontAccept(word);
return false; return false;
} }
}
if (state == 5) { break;
accept(word); case 5:
return true; if (stackSymbol == '$') {
stack.add('$');
state = 6;
} else { } else {
dontAccept(word); dontAccept(word);
return false; return false;
} }
break;
case 6:
accept(word);
return true;
} }
}
dontAccept(word);
return false;
}
private static void dontAccept(String word) { private static void dontAccept(String word) {
System.out.println("Word: " + word + "\nnot accepted"); System.out.println("Word: " + word + "\nnot accepted");

128
src/Kellerautomat.java Normal file
View File

@ -0,0 +1,128 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Kellerautomat {
private static String[] words = {"ZZO", "ZZOZZOO", "ZZZOO", "ZZOO", "ZZOZZO", "ZOZ"};
public static void main(String[] args) {
for (String word : words) {
calculate(word);
}
}
public static boolean calculate(String word) {
List<Character> wordChars = new ArrayList<>();
for (char symbol : word.toCharArray()) {
wordChars.add(symbol);
}
ArrayList<Character> stack = new ArrayList<>();
stack.add('$');
int state = 0; //case 0 - 5 = q0 - q5, case -1 = Abfallzustand
while (wordChars.size() > 0 || state >= 3) {
char symbol;
Character stackSymbol = stack.remove(stack.size() - 1);
switch (state) {
case 0:
if (wordChars.remove(0) == 'Z' && stackSymbol == '$') {
stack.add('$');
stack.add('Z');
state = 1;
} else {
dontAccept(word);
return false;
}
break;
case 1:
if (wordChars.remove(0) == 'Z' && stackSymbol == 'Z') {
stack.add('Z');
stack.add('Z');
state = 2;
} else {
dontAccept(word);
return false;
}
break;
case 2:
symbol = wordChars.remove(0);
if (symbol == 'O' && stackSymbol == 'Z') {
state = 3;
} else if (symbol == 'Z' && stackSymbol == 'Z') {
stack.add('Z');
stack.add('Z');
state = 2;
} else if (symbol == 'Z' && stackSymbol == '$') {
stack.add('$');
stack.add('Z');
state = 2;
} else {
dontAccept(word);
return false;
}
break;
case 3:
if (stackSymbol == 'Z') {
stack.add('Z');
state = 4;
} else {
dontAccept(word);
return false;
}
break;
case 4:
if (wordChars.size() > 0) {
symbol = wordChars.remove(0);
if (symbol == 'Z' && stackSymbol == 'Z') {
stack.add('Z');
stack.add('Z');
state = 2;
} else if (symbol == 'O' && stackSymbol == 'Z') {
state = 3;
} else {
dontAccept(word);
return false;
}
} else {
if (stackSymbol == 'Z') {
state = 5;
} else {
dontAccept(word);
return false;
}
}
break;
case 5:
if (stackSymbol == '$') {
stack.add('$');
state = 6;
} else {
dontAccept(word);
return false;
}
break;
case 6:
accept(word);
return true;
}
}
dontAccept(word);
return false;
}
private static void dontAccept(String word) {
System.out.println("Word: " + word + "\nnot accepted");
}
private static void accept(String word) {
System.out.println("Word: " + word + "\naccepted ");
}
private static boolean isOperator(char symbol){
return symbol == '+' || symbol == '*';
}
}