Advertisement
luishenriique

Main - HashLFC

May 16th, 2014
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.56 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Stack;
  3.  
  4. public class Main {
  5.  
  6.     public static void main(String[] args) {
  7.         Helper.printMembers();
  8.         for(;;)
  9.         labelTrans:{
  10.             HashMap<String, String> hsTransicoes = new HashMap<String, String>();
  11.  
  12.             String[] automatoTeste = Helper.readArchive().split(";");
  13.             String[][] strTransicoes = new String[automatoTeste.length][];
  14.  
  15.             for (int i = 0; i < automatoTeste.length; i++) {
  16.                 strTransicoes[i] = automatoTeste[i].replaceAll("[>{};]", "").split("[-]");
  17.                 hsTransicoes.put(strTransicoes[i][0], strTransicoes[i][1]);
  18.             }
  19.  
  20.             for(;;)
  21.             labelStr:{
  22.                 String estInicial = strTransicoes[0][1].substring(0, strTransicoes[0][1].indexOf(",") + 1);
  23.                 Stack<String> pilha = new Stack<String>();
  24.                 String raiz = null;
  25.                 String prox = null;
  26.                 String[] operations = {null, null};
  27.  
  28.                 System.out.print("Insira a sentença: ");
  29.                 String sentenca = Helper.readKeyboard();
  30.                 System.out.println();
  31.  
  32.                 int strLength = sentenca.length();
  33.  
  34.                 for (int i = 0; i < sentenca.length() + 1; i++) {
  35.                     raiz = (estInicial + (strLength == 0 ? "E" : sentenca.charAt(i)) + "," + (pilha.isEmpty() ? "Z" : pilha.peek()) + ")");
  36.  
  37.                     System.out.print(estInicial);
  38.                     if (strLength != 0)
  39.                         for (int j = i; j < sentenca.length(); j++)
  40.                                 System.out.print(sentenca.charAt(j));
  41.                     else
  42.                         System.out.print("E");
  43.                     System.out.print(",");
  44.                     for (int j = pilha.size(); j > 0; j--)
  45.                         System.out.print(pilha.isEmpty() ? "Z" : pilha.get(j - 1));
  46.                     System.out.print("Z)");
  47.  
  48.                     if (hsTransicoes.containsKey(raiz)) {
  49.                         prox = hsTransicoes.get(raiz);
  50.                         if (prox.length() > 0) {
  51.                             estInicial = prox.substring(0, prox.indexOf(",") + 1);
  52.                             operations = prox.replaceAll("[()]", "").split(",");
  53.                             if (operations[1].length() > 1)
  54.                                 for (int j = 0; j < (operations[1].length() - 1); j++)
  55.                                     pilha.push(Character.toString(operations[1].charAt(j)));
  56.                             else if (operations[1].equals("E") && !pilha.empty())
  57.                                 pilha.pop();
  58.                             if (strLength != 0)
  59.                                 strLength--;
  60.                             System.out.println("->" + prox);
  61.                         }
  62.                     } else {
  63.                         operations[0] = "stop";
  64.                         break;
  65.                     }
  66.                 }
  67.                 System.out.println();
  68.                 System.out.println("Sentenca vazia: " + ((strLength == 0) ? "SIM" : "NAO"));
  69.                 System.out.println("Estado final: " + (operations[0].endsWith("f") ? "SIM" : "NAO"));
  70.                 System.out.println(((strLength == 0) && operations[0].endsWith("f")) ? "ACEITA" : "REJEITA");
  71.                 System.out.println();
  72.  
  73.                 switch (Helper.menu()) {
  74.                     case 1: break labelStr;
  75.                     case 2: break labelTrans;
  76.                     default: System.exit(0);
  77.                 }
  78.             }
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement