Operatoren + und * hinzugefügt in Aufgabe1_Kellerautomat

This commit is contained in:
romanschenk37 2022-04-06 23:54:20 +02:00
parent 43539ca662
commit 03fc5b9712
2 changed files with 8 additions and 4 deletions

View File

@ -6,12 +6,12 @@ class Aufgabe1_KellerautomatTest {
@Test @Test
void calculate() { void calculate() {
String[] acceptableWords = {"ZZO", "ZZOZZOO", "ZZZOO"}; String[] acceptableWords = {"ZZ+", "ZZ*ZZ+*", "ZZZ*+"};
for (String word : acceptableWords) { for (String word : acceptableWords) {
assertTrue(Aufgabe1_Kellerautomat.calculate(word)); assertTrue(Aufgabe1_Kellerautomat.calculate(word));
} }
String[] notAcceptableWords = {"ZZOO", "ZZOZZO", "ZOZ"}; String[] notAcceptableWords = {"ZZ*+", "ZZ+ZZ*", "Z*Z"};
for (String word : notAcceptableWords) { for (String word : notAcceptableWords) {
assertFalse(Aufgabe1_Kellerautomat.calculate(word)); assertFalse(Aufgabe1_Kellerautomat.calculate(word));
} }

View File

@ -48,7 +48,7 @@ public class Aufgabe1_Kellerautomat {
break; break;
case 2: case 2:
symbol = wordChars.remove(0); symbol = wordChars.remove(0);
if (symbol == 'O' && stackSymbol == 'Z') { if (isOperator(symbol) && stackSymbol == 'Z') {
state = 3; state = 3;
} else if (symbol == 'Z' && stackSymbol == 'Z') { } else if (symbol == 'Z' && stackSymbol == 'Z') {
stack.add('Z'); stack.add('Z');
@ -79,7 +79,7 @@ public class Aufgabe1_Kellerautomat {
stack.add('Z'); stack.add('Z');
stack.add('Z'); stack.add('Z');
state = 2; state = 2;
} else if (symbol == 'O' && stackSymbol == 'Z') { } else if (isOperator(symbol) && stackSymbol == 'Z') {
state = 3; state = 3;
} else { } else {
dontAccept(word); dontAccept(word);
@ -121,4 +121,8 @@ public class Aufgabe1_Kellerautomat {
System.out.println("Word: " + word + "\naccepted "); System.out.println("Word: " + word + "\naccepted ");
} }
private static boolean isOperator(char symbol){
return symbol == '+' || symbol == '*';
}
} }