2022-04-03 21:16:05 +02:00
|
|
|
import java.util.ArrayList;
|
2022-04-06 23:19:13 +02:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
2022-04-03 21:16:05 +02:00
|
|
|
|
|
|
|
public class Aufgabe1_Kellerautomat {
|
|
|
|
private static String[] words = {"ZZO", "ZZOZZOO", "ZZZOO", "ZZOO", "ZZOZZO", "ZOZ"};
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
for (String word : words) {
|
|
|
|
calculate(word);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean calculate(String word) {
|
2022-04-06 23:19:13 +02:00
|
|
|
List<Character> wordChars = new ArrayList<>();
|
|
|
|
for (char symbol : word.toCharArray()) {
|
|
|
|
wordChars.add(symbol);
|
|
|
|
}
|
2022-04-03 21:16:05 +02:00
|
|
|
ArrayList<Character> stack = new ArrayList<>();
|
|
|
|
stack.add('$');
|
|
|
|
|
|
|
|
int state = 0; //case 0 - 5 = q0 - q5, case -1 = Abfallzustand
|
|
|
|
|
2022-04-06 23:19:13 +02:00
|
|
|
while (wordChars.size() > 0 || state >= 3) {
|
|
|
|
char symbol;
|
2022-04-03 21:16:05 +02:00
|
|
|
Character stackSymbol = stack.remove(stack.size() - 1);
|
|
|
|
switch (state) {
|
|
|
|
case 0:
|
2022-04-06 23:19:13 +02:00
|
|
|
if (wordChars.remove(0) == 'Z' && stackSymbol == '$') {
|
2022-04-03 21:16:05 +02:00
|
|
|
stack.add('$');
|
|
|
|
stack.add('Z');
|
|
|
|
state = 1;
|
|
|
|
} else {
|
|
|
|
dontAccept(word);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
2022-04-06 23:19:13 +02:00
|
|
|
if (wordChars.remove(0) == 'Z' && stackSymbol == 'Z') {
|
2022-04-03 21:16:05 +02:00
|
|
|
stack.add('Z');
|
|
|
|
stack.add('Z');
|
|
|
|
state = 2;
|
|
|
|
} else {
|
|
|
|
dontAccept(word);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
2022-04-06 23:19:13 +02:00
|
|
|
symbol = wordChars.remove(0);
|
2022-04-06 23:54:20 +02:00
|
|
|
if (isOperator(symbol) && stackSymbol == 'Z') {
|
2022-04-03 21:16:05 +02:00
|
|
|
state = 3;
|
|
|
|
} else if (symbol == 'Z' && stackSymbol == 'Z') {
|
|
|
|
stack.add('Z');
|
|
|
|
stack.add('Z');
|
|
|
|
state = 2;
|
|
|
|
} else if (symbol == 'Z' && stackSymbol == '$') {
|
|
|
|
stack.add('$');
|
|
|
|
stack.add('Z');
|
|
|
|
state = 2;
|
|
|
|
} else {
|
|
|
|
dontAccept(word);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3:
|
2022-04-06 23:19:13 +02:00
|
|
|
if (stackSymbol == 'Z') {
|
2022-04-03 21:16:05 +02:00
|
|
|
stack.add('Z');
|
2022-04-06 23:19:13 +02:00
|
|
|
state = 4;
|
2022-04-03 21:16:05 +02:00
|
|
|
} else {
|
|
|
|
dontAccept(word);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
2022-04-06 23:19:13 +02:00
|
|
|
case 4:
|
|
|
|
if (wordChars.size() > 0) {
|
|
|
|
symbol = wordChars.remove(0);
|
|
|
|
if (symbol == 'Z' && stackSymbol == 'Z') {
|
|
|
|
stack.add('Z');
|
|
|
|
stack.add('Z');
|
|
|
|
state = 2;
|
2022-04-06 23:54:20 +02:00
|
|
|
} else if (isOperator(symbol) && stackSymbol == 'Z') {
|
2022-04-06 23:19:13 +02:00
|
|
|
state = 3;
|
|
|
|
} else {
|
|
|
|
dontAccept(word);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (stackSymbol == 'Z') {
|
|
|
|
state = 5;
|
|
|
|
} else {
|
|
|
|
dontAccept(word);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
if (stackSymbol == '$') {
|
|
|
|
stack.add('$');
|
|
|
|
state = 6;
|
|
|
|
} else {
|
|
|
|
dontAccept(word);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
accept(word);
|
|
|
|
return true;
|
2022-04-03 21:16:05 +02:00
|
|
|
|
2022-04-06 23:19:13 +02:00
|
|
|
}
|
2022-04-03 21:16:05 +02:00
|
|
|
}
|
2022-04-06 23:19:13 +02:00
|
|
|
dontAccept(word);
|
|
|
|
return false;
|
2022-04-03 21:16:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 ");
|
|
|
|
}
|
|
|
|
|
2022-04-06 23:54:20 +02:00
|
|
|
private static boolean isOperator(char symbol){
|
|
|
|
return symbol == '+' || symbol == '*';
|
|
|
|
}
|
|
|
|
|
2022-04-03 21:16:05 +02:00
|
|
|
}
|