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
void calculate() {
String[] acceptableWords = {"ZZO", "ZZOZZOO", "ZZZOO"};
String[] acceptableWords = {"ZZ+", "ZZ*ZZ+*", "ZZZ*+"};
for (String word : acceptableWords) {
assertTrue(Aufgabe1_Kellerautomat.calculate(word));
}
String[] notAcceptableWords = {"ZZOO", "ZZOZZO", "ZOZ"};
String[] notAcceptableWords = {"ZZ*+", "ZZ+ZZ*", "Z*Z"};
for (String word : notAcceptableWords) {
assertFalse(Aufgabe1_Kellerautomat.calculate(word));
}

View File

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