luishenriique

Main - LFC

Feb 14th, 2014
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.32 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. public class Main {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         String automatoValido = "\\(q\\d+f?,\\w*\\)->\\{?q\\d+f?(,q\\d+f?)*\\}?;?";
  8.         String funcoesTransicao = "(q0f,a)->q1;(q1,b)->q2f;(q2f,a)->q3f;(q3f,a)->q1;(q3f,b)->q2f";
  9.         String sentencaEntrada = "ab";
  10.         String[] funcoes = new String[funcoesTransicao.split(";").length];
  11.  
  12.         ArrayList<Estado> estados = new ArrayList<>();
  13.  
  14.         if (!funcoesTransicao.matches("(" + automatoValido + ")*")) {
  15.             System.out.println("Entrada invalida!");
  16.             return;
  17.         } else {
  18.             funcoes = funcoesTransicao.split(";");
  19.         }
  20.  
  21.         String[][] funcao = new String[funcoes.length][];
  22.  
  23.         for (int i = 0; i < funcoes.length; i++)
  24.             funcao[i] = funcoes[i].replaceAll("[()>{};]", "").split("[,-]");
  25.  
  26.         String tipoautomato = (funcoesTransicao.contains("{")) ? "AFN" : "AFD";
  27.         System.out.println("Encontrada(s) transição(ões) do tipo: " + tipoautomato);
  28.  
  29.         // Adicionando estados
  30.         for (int i = 0; i < funcao.length; i++) {
  31.             boolean estadoExists = false;
  32.             for (int j = 0; j < estados.size(); j++)
  33.                 if (funcao[i][0].matches(estados.get(j).getNomeEstado())) {
  34.                     estadoExists = true;
  35.                     break;
  36.                 }
  37.             if(!estadoExists)
  38.                 estados.add(new Estado(funcao[i][0].endsWith("f"), funcao[i][0]));
  39.         }
  40.        
  41.         // Adicionando próximos estados com repectivo caractere de transição
  42.         for (int i = 0; i < funcao.length; i++) {
  43.             int idxA = -1;
  44.             ArrayList<Integer> idxs = new ArrayList<Integer>();
  45.             for (int j = 0; j < estados.size(); j++){
  46.                 if(funcao[i][0].matches(estados.get(j).getNomeEstado()))
  47.                     idxA = j;
  48.                 for (int z = 2; z < funcao[i].length; z++)
  49.                     if (funcao[i][z].matches(estados.get(j).getNomeEstado()))
  50.                         idxs.add(j);
  51.             }
  52.             for (int idx : idxs)
  53.                 estados.get(idxA).addproxEstado(estados.get(idx), funcao[i][1]);
  54.         }
  55.  
  56.         if (tipoautomato.equals("AFD")) {
  57.             AutomatoFinitoDeterministico automato = new AutomatoFinitoDeterministico(estados.get(0));
  58.             System.out.println(automato.pertenceounao(sentencaEntrada));
  59.         } else {
  60.             AutomatoFinitoNaoDeterministico automato = new AutomatoFinitoNaoDeterministico(estados.get(0));
  61.             System.out.println(automato.pertenceounao(sentencaEntrada));
  62.             for (int z = 0; z < automato.getListadePassos().size(); z++)
  63.                 System.out.println(automato.getListadePassos().get(z));
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment