Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- public class Main {
- public static void main(String[] args) {
- String automatoValido = "\\(q\\d+f?,\\w*\\)->\\{?q\\d+f?(,q\\d+f?)*\\}?;?";
- String funcoesTransicao = "(q0f,a)->q1;(q1,b)->q2f;(q2f,a)->q3f;(q3f,a)->q1;(q3f,b)->q2f";
- String sentencaEntrada = "ab";
- String[] funcoes = new String[funcoesTransicao.split(";").length];
- ArrayList<Estado> estados = new ArrayList<>();
- if (!funcoesTransicao.matches("(" + automatoValido + ")*")) {
- System.out.println("Entrada invalida!");
- return;
- } else {
- funcoes = funcoesTransicao.split(";");
- }
- String[][] funcao = new String[funcoes.length][];
- for (int i = 0; i < funcoes.length; i++)
- funcao[i] = funcoes[i].replaceAll("[()>{};]", "").split("[,-]");
- String tipoautomato = (funcoesTransicao.contains("{")) ? "AFN" : "AFD";
- System.out.println("Encontrada(s) transição(ões) do tipo: " + tipoautomato);
- // Adicionando estados
- for (int i = 0; i < funcao.length; i++) {
- boolean estadoExists = false;
- for (int j = 0; j < estados.size(); j++)
- if (funcao[i][0].matches(estados.get(j).getNomeEstado())) {
- estadoExists = true;
- break;
- }
- if(!estadoExists)
- estados.add(new Estado(funcao[i][0].endsWith("f"), funcao[i][0]));
- }
- // Adicionando próximos estados com repectivo caractere de transição
- for (int i = 0; i < funcao.length; i++) {
- int idxA = -1;
- ArrayList<Integer> idxs = new ArrayList<Integer>();
- for (int j = 0; j < estados.size(); j++){
- if(funcao[i][0].matches(estados.get(j).getNomeEstado()))
- idxA = j;
- for (int z = 2; z < funcao[i].length; z++)
- if (funcao[i][z].matches(estados.get(j).getNomeEstado()))
- idxs.add(j);
- }
- for (int idx : idxs)
- estados.get(idxA).addproxEstado(estados.get(idx), funcao[i][1]);
- }
- if (tipoautomato.equals("AFD")) {
- AutomatoFinitoDeterministico automato = new AutomatoFinitoDeterministico(estados.get(0));
- System.out.println(automato.pertenceounao(sentencaEntrada));
- } else {
- AutomatoFinitoNaoDeterministico automato = new AutomatoFinitoNaoDeterministico(estados.get(0));
- System.out.println(automato.pertenceounao(sentencaEntrada));
- for (int z = 0; z < automato.getListadePassos().size(); z++)
- System.out.println(automato.getListadePassos().get(z));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment