diff --git a/src/Aufgabe1_Kellerautomat.java b/src/Aufgabe1_Kellerautomat.java index 6473ef5..41ff12c 100644 --- a/src/Aufgabe1_Kellerautomat.java +++ b/src/Aufgabe1_Kellerautomat.java @@ -1,4 +1,7 @@ import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; public class Aufgabe1_Kellerautomat { private static String[] words = {"ZZO", "ZZOZZOO", "ZZZOO", "ZZOO", "ZZOZZO", "ZOZ"}; @@ -10,17 +13,21 @@ public class Aufgabe1_Kellerautomat { } public static boolean calculate(String word) { - char[] wordChars = word.toCharArray(); + List wordChars = new ArrayList<>(); + for (char symbol : word.toCharArray()) { + wordChars.add(symbol); + } ArrayList stack = new ArrayList<>(); stack.add('$'); 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); switch (state) { case 0: - if (symbol == 'Z' && stackSymbol == '$') { + if (wordChars.remove(0) == 'Z' && stackSymbol == '$') { stack.add('$'); stack.add('Z'); state = 1; @@ -30,7 +37,7 @@ public class Aufgabe1_Kellerautomat { } break; case 1: - if (symbol == 'Z' && stackSymbol == 'Z') { + if (wordChars.remove(0) == 'Z' && stackSymbol == 'Z') { stack.add('Z'); stack.add('Z'); state = 2; @@ -40,6 +47,7 @@ public class Aufgabe1_Kellerautomat { } break; case 2: + symbol = wordChars.remove(0); if (symbol == 'O' && stackSymbol == 'Z') { state = 3; } else if (symbol == 'Z' && stackSymbol == 'Z') { @@ -56,42 +64,53 @@ public class Aufgabe1_Kellerautomat { } break; case 3: - if (symbol == 'O' && stackSymbol == 'Z') { - state = 3; - } else if (symbol == 'Z' && stackSymbol == 'Z') { + if (stackSymbol == 'Z') { stack.add('Z'); - stack.add('Z'); - state = 2; + 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; + } } - - if (state == 3 && stack.size() >= 1 && stack.remove(stack.size() - 1) == 'Z') { - state = 4; - } else { - dontAccept(word); - return false; - } - - if (state == 4 && stack.size() >= 1 && stack.remove(stack.size() - 1) == '$') { - state = 5; - } else { - dontAccept(word); - return false; - } - - if (state == 5) { - accept(word); - return true; - } else { - dontAccept(word); - return false; - } - + dontAccept(word); + return false; } private static void dontAccept(String word) { diff --git a/src/Kellerautomat.java b/src/Kellerautomat.java new file mode 100644 index 0000000..913c25f --- /dev/null +++ b/src/Kellerautomat.java @@ -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 wordChars = new ArrayList<>(); + for (char symbol : word.toCharArray()) { + wordChars.add(symbol); + } + ArrayList 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 == '*'; + } + +}