From 10678bc3cd204a3a1e20b5b17b202acf44b1f409 Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Thu, 7 Apr 2022 00:25:44 +0200 Subject: [PATCH] Aufgabe2_Kellerautomat fertig gestellt (Funktion) --- Test/Aufgabe2_KellerautomatTest.java | 8 +-- src/Aufgabe2_Kellerautomat.java | 89 +++++++++++++++------------- 2 files changed, 52 insertions(+), 45 deletions(-) diff --git a/Test/Aufgabe2_KellerautomatTest.java b/Test/Aufgabe2_KellerautomatTest.java index 961bdff..7bd0679 100644 --- a/Test/Aufgabe2_KellerautomatTest.java +++ b/Test/Aufgabe2_KellerautomatTest.java @@ -6,9 +6,9 @@ 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*")); + assertEquals(6664, Aufgabe2_Kellerautomat.calculate("34+62+89+43+***")); + assertEquals(58, Aufgabe2_Kellerautomat.calculate("31+78+987+1214++7++++++")); + assertEquals(-1, Aufgabe2_Kellerautomat.calculate("34+*")); + assertEquals(-1, Aufgabe2_Kellerautomat.calculate("8+9+7*2*")); } } \ No newline at end of file diff --git a/src/Aufgabe2_Kellerautomat.java b/src/Aufgabe2_Kellerautomat.java index f0a015f..a813e24 100644 --- a/src/Aufgabe2_Kellerautomat.java +++ b/src/Aufgabe2_Kellerautomat.java @@ -12,131 +12,138 @@ public class Aufgabe2_Kellerautomat { } } - public static char calculate(String word) { + public static int calculate(String word) { List wordChars = new ArrayList<>(); for (char symbol : word.toCharArray()) { wordChars.add(symbol); } - ArrayList stack = new ArrayList<>(); - stack.add('$'); + ArrayList stack = new ArrayList<>(); + stack.add(-1); int state = 0; //case 0 - 5 = q0 - q5, case -1 = Abfallzustand char operator = '$'; - char firstNumber = '$'; - char secondNumber = '$'; - char result = '$'; + int firstNumber = 0; + int secondNumber = 0; + int result = -1; while (wordChars.size() > 0 || state >= 3) { char symbol; - Character stackSymbol = stack.remove(stack.size() - 1); + int stackSymbol = stack.remove(stack.size() - 1); switch (state) { case 0: - if (wordChars.remove(0) == 'Z' && stackSymbol == '$') { - stack.add('$'); - stack.add('Z'); + symbol = wordChars.remove(0); + if (!isOperator(symbol) && stackSymbol == -1) { + stack.add(-1); + stack.add(Character.getNumericValue(symbol)); state = 1; } else { dontAccept(word); - return '$'; + return -1; } break; case 1: - if (wordChars.remove(0) == 'Z' && stackSymbol == 'Z') { - stack.add('Z'); - stack.add('Z'); + symbol = wordChars.remove(0); + if (!isOperator(symbol) && stackSymbol > -1) { + stack.add(stackSymbol); + stack.add(Character.getNumericValue(symbol)); state = 2; } else { dontAccept(word); - return '$'; + return -1; } break; case 2: symbol = wordChars.remove(0); - if (isOperator(symbol) && stackSymbol == 'Z') { + if (isOperator(symbol) && stackSymbol > -1) { operator = symbol; firstNumber = stackSymbol; state = 3; - } else if (symbol == 'Z' && stackSymbol == 'Z') { - stack.add('Z'); - stack.add('Z'); + } else if (!isOperator(symbol) && stackSymbol > -1) { + stack.add(stackSymbol); + stack.add(Character.getNumericValue(symbol)); state = 2; - } else if (symbol == 'Z' && stackSymbol == '$') { - stack.add('$'); - stack.add('Z'); + } else if (!isOperator(symbol) && stackSymbol == -1) { + stack.add(-1); + stack.add(Character.getNumericValue(symbol)); state = 2; } else { dontAccept(word); - return '$'; + return -1; } break; case 3: - if (stackSymbol == 'Z') { + if (stackSymbol > -1) { secondNumber = stackSymbol; stack.add(doOperation(operator, firstNumber, secondNumber)); state = 4; } else { dontAccept(word); - return '$'; + return -1; } break; case 4: if (wordChars.size() > 0) { symbol = wordChars.remove(0); - if (symbol == 'Z' && stackSymbol == 'Z') { - stack.add('Z'); - stack.add('Z'); + if (!isOperator(symbol) && stackSymbol > -1) { + stack.add(stackSymbol); + stack.add(Character.getNumericValue(symbol)); state = 2; - } else if (isOperator(symbol) && stackSymbol == 'Z') { + } else if (isOperator(symbol) && stackSymbol > -1) { operator = symbol; firstNumber = stackSymbol; state = 3; } else { dontAccept(word); - return '$'; + return -1; } } else { - if (stackSymbol == 'Z') { + if (stackSymbol > -1) { result = stackSymbol; state = 5; } else { dontAccept(word); - return '$'; + return -1; } } break; case 5: - if (stackSymbol == '$') { - stack.add('$'); + if (stackSymbol == -1) { + stack.add(-1); state = 6; } else { dontAccept(word); - return '$'; + return -1; } break; case 6: - accept(word); + accept(word, result); return result; } } dontAccept(word); - return '$'; + return -1; } 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 void accept(String word, int result) { + System.out.println("Word: " + word + "\naccepted\nResult " + result); } private static boolean isOperator(char symbol){ return symbol == '+' || symbol == '*'; } - private static char doOperation(char doOperation, char firstNumber, char secondNumber){ - return 'Z'; + private static int doOperation(char operator, int firstNumber, int secondNumber){ + if(operator == '+'){ + return firstNumber + secondNumber; + } else if(operator == '*'){ + return firstNumber * secondNumber; + } + return -1; } }