Advertisement
GobiernoFederal

Analizador Lexico Pilas

Sep 17th, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.21 KB | None | 0 0
  1. ===============================CLASE PILA==========================================
  2. //razabinaria@gmail.com
  3. package Paquete;
  4.  
  5. public class Pila {
  6.    
  7.     class Nodo {
  8.         char simbolo;
  9.         Nodo sig;
  10.     }
  11.    
  12.     private Nodo raiz;
  13.    
  14.     Pila () {
  15.         raiz=null;
  16.     }
  17.    
  18.     public void insertar(char x) {
  19.         Nodo nuevo;
  20.         nuevo = new Nodo();
  21.         nuevo.simbolo = x;
  22.         if (raiz==null)
  23.         {
  24.             nuevo.sig = null;
  25.             raiz = nuevo;
  26.         }
  27.         else
  28.         {
  29.             nuevo.sig = raiz;
  30.             raiz = nuevo;
  31.         }
  32.     }
  33.    
  34.     public char extraer ()
  35.     {
  36.         if (raiz!=null)
  37.         {
  38.             char informacion = raiz.simbolo;
  39.             raiz = raiz.sig;
  40.             return informacion;
  41.         }
  42.         else
  43.         {
  44.             return Character.MAX_VALUE;
  45.         }
  46.     }  
  47.    
  48.     public boolean vacia() {
  49.         if (raiz==null) {
  50.             return true;
  51.         } else {
  52.             return false;
  53.         }
  54.     }
  55. }
  56.  
  57. //razabinaria@gmail.com
  58.  
  59.  
  60. ===================================================CLASE FORMULA====================================
  61.  
  62. //razabinaria@gmail.com
  63.  
  64. package Paquete;
  65.  
  66. import javax.swing.*;
  67. import java.awt.event.*;
  68. public class Formula extends JFrame implements ActionListener {
  69.     private JTextField tf1;
  70.     private JButton boton1;
  71.     public Formula() {
  72.         setLayout(null);
  73.         tf1=new JTextField("{2*(4-5)-{3*4}-[4-5]}");
  74.         tf1.setBounds(10,10,230,30);
  75.         add(tf1);
  76.         boton1=new JButton("Verificar fórmula.");
  77.         boton1.setBounds(10,70,180,30);
  78.         add(boton1);
  79.         boton1.addActionListener(this);
  80.     }
  81.    
  82.     public void actionPerformed(ActionEvent e) {
  83.         if (e.getSource()==boton1) {
  84.            if (balanceada()) {
  85.                setTitle("Está correctamente balanceada.");
  86.            } else {
  87.                setTitle("No está correctamente balanceada.");
  88.            }
  89.         }
  90.     }
  91. //razabinaria@gmail.com
  92.     public boolean balanceada() {
  93.         Pila pila1;  
  94.         pila1 = new Pila ();    
  95.         String cadena=tf1.getText();
  96.         for (int f = 0 ; f < cadena.length() ; f++)
  97.         {
  98.             if (cadena.charAt(f) == '(' || cadena.charAt(f) == '[' || cadena.charAt(f) == '{') {
  99.                 pila1.insertar(cadena.charAt(f));
  100.             } else {
  101.                 if (cadena.charAt(f)==')') {
  102.                     if (pila1.extraer()!='(') {
  103.                         return false;
  104.                     }
  105.                 } else {
  106.                     if (cadena.charAt(f)==']') {
  107.                         if (pila1.extraer()!='[') {
  108.                             return false;
  109.                         }
  110.                     } else {
  111.                         if (cadena.charAt(f)=='}') {
  112.                             if (pila1.extraer()!='{') {
  113.                                 return false;
  114.                             }
  115.                         }
  116.                     }
  117.                 }
  118.         }  
  119.         }
  120.         if (pila1.vacia()) {
  121.             return true;
  122.         } else {
  123.         return false;
  124.         }
  125.     }  
  126.    
  127.    
  128.     public static void main(String[] ar) {
  129.         Formula formula1=new Formula();
  130.         formula1.setBounds(0,0,350,140);
  131.         formula1.setVisible(true);
  132.     }
  133.    
  134. }
  135. //razabinaria@gmail.com
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement