Operatoren + und * hinzugefügt (ohne Berechnung) in Aufgabe2_Kellerautomat

Tests in Aufgabe2
This commit is contained in:
romanschenk37 2022-04-06 23:58:57 +02:00
parent 03fc5b9712
commit 4607df33b7
2 changed files with 42 additions and 14 deletions

View File

@ -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*"));
}
}

View File

@ -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<Character> 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';
}
}