Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 3rd, 2012  |  syntax: None  |  size: 3.63 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /**
  2.  * @author Carlos Eduardo Saraiva
  3.  */
  4.  
  5. import java.io.IOException;
  6. import java.util.Scanner;
  7.  
  8. public class Carlos {
  9.  
  10.     public static int top = -1;
  11.     public static char[] pilha = new char[100];
  12.  
  13.     public static void main(String[] args) {
  14.         boolean continuar = true;
  15.         System.out.println("PARENTHESES VERIFICATOR 3001! VER. 000000000000000(humpf)000000000.1");
  16.        
  17.         while  (continuar){
  18.  
  19.         char[] expressao = new char[10];
  20.         int tamanho = entradaCaractere(expressao);
  21.        
  22.  
  23.         balanceamento(tamanho, expressao);
  24.                
  25.         if(empty()){
  26.             System.out.println("Está balanceada!");
  27.         }
  28.         else{
  29.             System.out.println("Não está balanceada! Danger, danger, system collapsing.. bleep..bleep..");
  30.         }
  31.        
  32.         continuar = saida();
  33.        
  34.         }
  35.     }
  36.     //Verficação do balancemento dos parenteses, colchetes e chaves
  37.     public static void balanceamento(int tamanho, char[] expressao){
  38.         char auxList[] = {'(', ')', '[', ']', '{', '}'};
  39.         int i, j;
  40.        
  41.         for (i = 0; i <= tamanho; i++) {
  42.             for (j = 0; j < auxList.length; j++) {
  43.  
  44.                 if (verificao(expressao, i) && expressao[i] == auxList[j]) {
  45.  
  46.                     pop();
  47.  
  48.                 } else {
  49.  
  50.                     if (expressao[i] == auxList[j]) {
  51.  
  52.                         push(expressao[i]);
  53.  
  54.                     }
  55.                 }
  56.             }
  57.         }
  58.        
  59.     }
  60.    
  61.     public static boolean verificao(char expressao[], int i) {
  62.  
  63.         if ((top > -1 && (expressao[i] == ')' && pilha[top] == '(' || expressao[i] == ']' && pilha[top] == '[' || expressao[i] == '}' && pilha[top] == '{'))) {
  64.                return true;
  65.         }
  66.         else {
  67.  
  68.             return false;
  69.         }
  70.  
  71.     }
  72.    
  73.     public static boolean saida(){
  74.         Scanner in = new Scanner(System.in);
  75.         System.out.println("Deseja verificar outra expresssao? (Tecle 's para continuar, qualquer outra tecla para sair)");
  76.         String op = in.next();
  77.  
  78.         if("s".equals(op) || "S".equals(op)){
  79.             return true;
  80.         }
  81.         else{
  82.             System.out.println("bye!");
  83.             return false;
  84.         }
  85.        
  86.     }
  87.    
  88.     //Método que recebe a entrada do usuário, que digitará a expressão matemática.
  89.     public static int entradaCaractere(char vetCar[]) {
  90.  
  91.         char c = 0;
  92.         int i = 0;
  93.                
  94.         System.out.println("\n\nDigite a expressão aritimética a ser avaliada:\n\n");
  95.  
  96.         while (i < vetCar.length && c != '\n') {
  97.             try {
  98.                 c = (char) System.in.read();
  99.  
  100.             } catch (IOException erro) {
  101.  
  102.                 System.err.println(erro.getMessage());
  103.             }
  104.             vetCar[i++] = c;
  105.         }
  106.  
  107.         return i - 1;
  108.  
  109.     }
  110.    
  111.     //Métodos relacionados a manipulação da pilha
  112.     public static void push(char elemento) {
  113.         if (!full()) {
  114.             top++;
  115.             pilha[top] = elemento;
  116.         }
  117.     }
  118.  
  119.     public static void pop() {
  120.         if (!empty()) {
  121.             top = top - 1;
  122.            
  123.         }
  124.     }
  125.  
  126.     public static boolean empty() {
  127.         if (top < 0) {
  128.             return true;
  129.         } else {
  130.             return false;
  131.         }
  132.     }
  133.  
  134.     public static boolean full() {
  135.  
  136.         return top == pilha.length - 1;
  137.  
  138.     }
  139.  
  140.     public static void mostraPilha() {
  141.  
  142.         for (int i = 0; i <= top; i++) {
  143.  
  144.             if (i == 0) {
  145.                 System.out.print("[  ");
  146.             }
  147.  
  148.             if (i < top) {
  149.                 System.out.print(pilha[i] + ", ");
  150.             } else {
  151.                 System.out.print(pilha[i] + "  ]\n");
  152.             }
  153.         }
  154.     }
  155. }