fefetl08

Expressao_Pilha

Mar 18th, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.74 KB | None | 0 0
  1. public class Expressao {
  2.     private static String exp = "({5+1})";
  3.  
  4.     public static boolean validador(String str) {
  5.         Pilha pilha = new Pilha(str.length());
  6.         for (int i = 0; i < str.length(); i++) {
  7.             if (str.charAt(i) == '{' || str.charAt(i) == '(' || str.charAt(i) == '[') {
  8.                 pilha.empilha(str.charAt(i));
  9.             }
  10.             else if(str.charAt(i) == '}' && pilha.topo() == '{' || str.charAt(i) == ')' && pilha.topo() == '(' ||
  11.                     str.charAt(i) == ']' && pilha.topo() == '[') {
  12.                 pilha.desempliha();
  13.             }
  14.         }
  15.     return pilha.vazia();
  16.     }
  17.  
  18.     public static void main(String[] args) {
  19.         System.out.println(validador(exp));
  20.     }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment