diff --git a/Test/Aufgabe2_KellerautomatTest.java b/Test/Aufgabe2_KellerautomatTest.java new file mode 100644 index 0000000..961bdff --- /dev/null +++ b/Test/Aufgabe2_KellerautomatTest.java @@ -0,0 +1,14 @@ +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class Aufgabe2_KellerautomatTest { + + @Test + void calculate() { + assertEquals('Z', Aufgabe2_Kellerautomat.calculate("ZZ+ZZ+ZZ+ZZ+***")); + assertEquals('Z', Aufgabe2_Kellerautomat.calculate("ZZ+ZZ+ZZZ+ZZZZ++Z++++++")); + assertEquals('$', Aufgabe2_Kellerautomat.calculate("ZZ+*")); + assertEquals('$', Aufgabe2_Kellerautomat.calculate("Z+Z+Z*Z*")); + } +} \ No newline at end of file diff --git a/src/Kellerautomat.java b/src/Aufgabe2_Kellerautomat.java similarity index 76% rename from src/Kellerautomat.java rename to src/Aufgabe2_Kellerautomat.java index 913c25f..f0a015f 100644 --- a/src/Kellerautomat.java +++ b/src/Aufgabe2_Kellerautomat.java @@ -3,7 +3,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; -public class Kellerautomat { +public class Aufgabe2_Kellerautomat { private static String[] words = {"ZZO", "ZZOZZOO", "ZZZOO", "ZZOO", "ZZOZZO", "ZOZ"}; public static void main(String[] args) { @@ -12,7 +12,7 @@ public class Kellerautomat { } } - public static boolean calculate(String word) { + public static char calculate(String word) { List wordChars = new ArrayList<>(); for (char symbol : word.toCharArray()) { wordChars.add(symbol); @@ -21,6 +21,10 @@ public class Kellerautomat { stack.add('$'); int state = 0; //case 0 - 5 = q0 - q5, case -1 = Abfallzustand + char operator = '$'; + char firstNumber = '$'; + char secondNumber = '$'; + char result = '$'; while (wordChars.size() > 0 || state >= 3) { char symbol; @@ -33,7 +37,7 @@ public class Kellerautomat { state = 1; } else { dontAccept(word); - return false; + return '$'; } break; case 1: @@ -43,12 +47,14 @@ public class Kellerautomat { state = 2; } else { dontAccept(word); - return false; + return '$'; } break; case 2: symbol = wordChars.remove(0); - if (symbol == 'O' && stackSymbol == 'Z') { + if (isOperator(symbol) && stackSymbol == 'Z') { + operator = symbol; + firstNumber = stackSymbol; state = 3; } else if (symbol == 'Z' && stackSymbol == 'Z') { stack.add('Z'); @@ -60,16 +66,17 @@ public class Kellerautomat { state = 2; } else { dontAccept(word); - return false; + return '$'; } break; case 3: if (stackSymbol == 'Z') { - stack.add('Z'); + secondNumber = stackSymbol; + stack.add(doOperation(operator, firstNumber, secondNumber)); state = 4; } else { dontAccept(word); - return false; + return '$'; } break; case 4: @@ -79,18 +86,21 @@ public class Kellerautomat { stack.add('Z'); stack.add('Z'); state = 2; - } else if (symbol == 'O' && stackSymbol == 'Z') { + } else if (isOperator(symbol) && stackSymbol == 'Z') { + operator = symbol; + firstNumber = stackSymbol; state = 3; } else { dontAccept(word); - return false; + return '$'; } } else { if (stackSymbol == 'Z') { + result = stackSymbol; state = 5; } else { dontAccept(word); - return false; + return '$'; } } break; @@ -100,17 +110,17 @@ public class Kellerautomat { state = 6; } else { dontAccept(word); - return false; + return '$'; } break; case 6: accept(word); - return true; + return result; } } dontAccept(word); - return false; + return '$'; } private static void dontAccept(String word) { @@ -125,4 +135,8 @@ public class Kellerautomat { return symbol == '+' || symbol == '*'; } + private static char doOperation(char doOperation, char firstNumber, char secondNumber){ + return 'Z'; + } + }