Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Expressao {
- private static String exp = "({5+1})";
- public static boolean validador(String str) {
- Pilha pilha = new Pilha(str.length());
- for (int i = 0; i < str.length(); i++) {
- if (str.charAt(i) == '{' || str.charAt(i) == '(' || str.charAt(i) == '[') {
- pilha.empilha(str.charAt(i));
- }
- else if(str.charAt(i) == '}' && pilha.topo() == '{' || str.charAt(i) == ')' && pilha.topo() == '(' ||
- str.charAt(i) == ']' && pilha.topo() == '[') {
- pilha.desempliha();
- }
- }
- return pilha.vazia();
- }
- public static void main(String[] args) {
- System.out.println(validador(exp));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment