Aufgabe2_Kellerautomat fertig gestellt (Funktion)

This commit is contained in:
romanschenk37 2022-04-07 00:25:44 +02:00
parent 4607df33b7
commit 10678bc3cd
2 changed files with 52 additions and 45 deletions

View File

@ -6,9 +6,9 @@ class Aufgabe2_KellerautomatTest {
@Test @Test
void calculate() { void calculate() {
assertEquals('Z', Aufgabe2_Kellerautomat.calculate("ZZ+ZZ+ZZ+ZZ+***")); assertEquals(6664, Aufgabe2_Kellerautomat.calculate("34+62+89+43+***"));
assertEquals('Z', Aufgabe2_Kellerautomat.calculate("ZZ+ZZ+ZZZ+ZZZZ++Z++++++")); assertEquals(58, Aufgabe2_Kellerautomat.calculate("31+78+987+1214++7++++++"));
assertEquals('$', Aufgabe2_Kellerautomat.calculate("ZZ+*")); assertEquals(-1, Aufgabe2_Kellerautomat.calculate("34+*"));
assertEquals('$', Aufgabe2_Kellerautomat.calculate("Z+Z+Z*Z*")); assertEquals(-1, Aufgabe2_Kellerautomat.calculate("8+9+7*2*"));
} }
} }

View File

@ -12,131 +12,138 @@ public class Aufgabe2_Kellerautomat {
} }
} }
public static char calculate(String word) { public static int calculate(String word) {
List<Character> wordChars = new ArrayList<>(); List<Character> wordChars = new ArrayList<>();
for (char symbol : word.toCharArray()) { for (char symbol : word.toCharArray()) {
wordChars.add(symbol); wordChars.add(symbol);
} }
ArrayList<Character> stack = new ArrayList<>(); ArrayList<Integer> stack = new ArrayList<>();
stack.add('$'); stack.add(-1);
int state = 0; //case 0 - 5 = q0 - q5, case -1 = Abfallzustand int state = 0; //case 0 - 5 = q0 - q5, case -1 = Abfallzustand
char operator = '$'; char operator = '$';
char firstNumber = '$'; int firstNumber = 0;
char secondNumber = '$'; int secondNumber = 0;
char result = '$'; int result = -1;
while (wordChars.size() > 0 || state >= 3) { while (wordChars.size() > 0 || state >= 3) {
char symbol; char symbol;
Character stackSymbol = stack.remove(stack.size() - 1); int stackSymbol = stack.remove(stack.size() - 1);
switch (state) { switch (state) {
case 0: case 0:
if (wordChars.remove(0) == 'Z' && stackSymbol == '$') { symbol = wordChars.remove(0);
stack.add('$'); if (!isOperator(symbol) && stackSymbol == -1) {
stack.add('Z'); stack.add(-1);
stack.add(Character.getNumericValue(symbol));
state = 1; state = 1;
} else { } else {
dontAccept(word); dontAccept(word);
return '$'; return -1;
} }
break; break;
case 1: case 1:
if (wordChars.remove(0) == 'Z' && stackSymbol == 'Z') { symbol = wordChars.remove(0);
stack.add('Z'); if (!isOperator(symbol) && stackSymbol > -1) {
stack.add('Z'); stack.add(stackSymbol);
stack.add(Character.getNumericValue(symbol));
state = 2; state = 2;
} else { } else {
dontAccept(word); dontAccept(word);
return '$'; return -1;
} }
break; break;
case 2: case 2:
symbol = wordChars.remove(0); symbol = wordChars.remove(0);
if (isOperator(symbol) && stackSymbol == 'Z') { if (isOperator(symbol) && stackSymbol > -1) {
operator = symbol; operator = symbol;
firstNumber = stackSymbol; firstNumber = stackSymbol;
state = 3; state = 3;
} else if (symbol == 'Z' && stackSymbol == 'Z') { } else if (!isOperator(symbol) && stackSymbol > -1) {
stack.add('Z'); stack.add(stackSymbol);
stack.add('Z'); stack.add(Character.getNumericValue(symbol));
state = 2; state = 2;
} else if (symbol == 'Z' && stackSymbol == '$') { } else if (!isOperator(symbol) && stackSymbol == -1) {
stack.add('$'); stack.add(-1);
stack.add('Z'); stack.add(Character.getNumericValue(symbol));
state = 2; state = 2;
} else { } else {
dontAccept(word); dontAccept(word);
return '$'; return -1;
} }
break; break;
case 3: case 3:
if (stackSymbol == 'Z') { if (stackSymbol > -1) {
secondNumber = stackSymbol; secondNumber = stackSymbol;
stack.add(doOperation(operator, firstNumber, secondNumber)); stack.add(doOperation(operator, firstNumber, secondNumber));
state = 4; state = 4;
} else { } else {
dontAccept(word); dontAccept(word);
return '$'; return -1;
} }
break; break;
case 4: case 4:
if (wordChars.size() > 0) { if (wordChars.size() > 0) {
symbol = wordChars.remove(0); symbol = wordChars.remove(0);
if (symbol == 'Z' && stackSymbol == 'Z') { if (!isOperator(symbol) && stackSymbol > -1) {
stack.add('Z'); stack.add(stackSymbol);
stack.add('Z'); stack.add(Character.getNumericValue(symbol));
state = 2; state = 2;
} else if (isOperator(symbol) && stackSymbol == 'Z') { } else if (isOperator(symbol) && stackSymbol > -1) {
operator = symbol; operator = symbol;
firstNumber = stackSymbol; firstNumber = stackSymbol;
state = 3; state = 3;
} else { } else {
dontAccept(word); dontAccept(word);
return '$'; return -1;
} }
} else { } else {
if (stackSymbol == 'Z') { if (stackSymbol > -1) {
result = stackSymbol; result = stackSymbol;
state = 5; state = 5;
} else { } else {
dontAccept(word); dontAccept(word);
return '$'; return -1;
} }
} }
break; break;
case 5: case 5:
if (stackSymbol == '$') { if (stackSymbol == -1) {
stack.add('$'); stack.add(-1);
state = 6; state = 6;
} else { } else {
dontAccept(word); dontAccept(word);
return '$'; return -1;
} }
break; break;
case 6: case 6:
accept(word); accept(word, result);
return result; return result;
} }
} }
dontAccept(word); dontAccept(word);
return '$'; return -1;
} }
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");
} }
private static void accept(String word) { private static void accept(String word, int result) {
System.out.println("Word: " + word + "\naccepted "); System.out.println("Word: " + word + "\naccepted\nResult " + result);
} }
private static boolean isOperator(char symbol){ private static boolean isOperator(char symbol){
return symbol == '+' || symbol == '*'; return symbol == '+' || symbol == '*';
} }
private static char doOperation(char doOperation, char firstNumber, char secondNumber){ private static int doOperation(char operator, int firstNumber, int secondNumber){
return 'Z'; if(operator == '+'){
return firstNumber + secondNumber;
} else if(operator == '*'){
return firstNumber * secondNumber;
}
return -1;
} }
} }