Advertisement
MOISES_QUISPE_ROJAS

Estructura Datos 2021 - EJERCICIO 1 PROBAR STACK CON LISTA ENLAZADA

Oct 27th, 2021
1,103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.23 KB | None | 0 0
  1. /* Estructura de Datos - Facultad de Ingenieria - Universidad Nacional de Jujuy
  2.  *
  3.  * @Autor: Equipo 4.1
  4.  */
  5.  /*      @integrantes:                  |    @Carrera:             |   @LU:
  6.    Flores ,Cesar Ismael                 | Lic. en Sistemas         | 1782
  7.    Llampa, Ariel Angel Gabriel          | Ing. Informatica         | 8445
  8.    Machaca, Rodrigo Agustin             | Ing. Informatica         | 8512
  9.    Perez, Emanuel Ismael                | Ing. Informatica         | 8393
  10.    Quispe Rojas, Moises Esteban Nicolas | Ing. Informatica         | 7286
  11.  
  12. El código de un analizador de lenguaje de marcado es semejante al que se presenta en el caso ejemplo que
  13. controla la parentización simple o un ejercicio de parentización múltiple. El lenguaje de marcado más
  14. conocido es HTML - Hyper Text Markup Language; en ese lenguaje el principio de una marca se encuentra
  15. entre los símbolos “<” y “>” y el final de la marca también se encuentra entre esos símbolos sólo que
  16. después del símbolo “<” se encuentra el símbolo “/”; de este modo tenemos <H1>algún texto</H1> o
  17. <p>otro texto </p>, las “marcas” tienen significado para los programas que presentan o muestran en
  18. pantalla el texto HTML.
  19. Codifique lo que haga falta para obtener un analizador de lenguaje de marcado de hipertexto de manera
  20. que pueda procesar una cadena de caracteres en ese formato que esté incorporada en el programa y por
  21. supuesto se pueda cambiar por otra; el texto debe contener marcas anidadas.
  22. El código debe facilitar un mecanismo para incorporar nuevas marcas con facilidad.
  23. Se debe considerar el caso particular de ciertas marcas que no necesitan de la marca de cierre, como ser
  24. <br/>.
  25.  
  26.  */
  27. package List;
  28.  
  29. import java.util.ArrayList;
  30.  
  31. public class ED_E6 {
  32.            
  33.     public static void main(String[] args) {    
  34.         (new ED_E6()).open();
  35.     }
  36.  
  37.     public void open() {
  38.         System.out.println("Se analizara el siguiente HTML: ");
  39.         System.out.println("------------------------------");
  40.         System.out.println(html);
  41.         System.out.println("------------------------------");
  42.         if (validateFinalStartMark()) {
  43.             System.out.println("html BIEN escrito");
  44.         }else{
  45.             System.out.println("html MAL escrito");
  46.         }
  47.         System.out.println("Se obtubieron los siguientes Tags: ");
  48.         ArrayList<String> tags = getTangs();
  49.         System.out.println(tags);
  50.         // validateTags
  51.         String salida;
  52.         Stack<String> stack = new Stack<String>();
  53.         for(int i=0;i<tags.size();i++){
  54.             if(tags.get(i).charAt(1)=='/'){
  55.                 System.out.println(stack.peek()+" - "+tags.get(i));
  56.                 stack.pop();
  57.             }else{
  58.                 if(!tags.get(i).contains("/")){
  59.                     stack.push(tags.get(i));
  60.                 }
  61.             }
  62.         }
  63.         if(stack.empty()){
  64.             System.out.println("Cierre correcto");
  65.         }else{
  66.             System.out.println("ERROR Cierre incorrecto");
  67.         }
  68.     }
  69.    
  70.     public void validateTags(){
  71.        
  72.     }
  73.    
  74.     public ArrayList<String> getTangs(){
  75.         ArrayList<String> tagsObtained = new ArrayList<String>();
  76.         char letter;
  77.         int beginIndex=0, endIndex=0;        
  78.         boolean bandBegin=false,bandEnd=false;
  79.         for(int i=0;i<html.length();i++){
  80.             letter=html.charAt(i);
  81.             if(letter=='<'){
  82.                 beginIndex=i;
  83.                 bandBegin=true;
  84.             }else{
  85.                 if(letter=='>'){
  86.                     endIndex=i;
  87.                     bandEnd=true;
  88.                 }                
  89.             }
  90.             if(bandBegin && bandEnd){
  91.                 tagsObtained.add(html.substring(beginIndex, endIndex+1));
  92.                 bandBegin=false;
  93.                 bandEnd=false;
  94.             }
  95.         }
  96.         return tagsObtained;
  97.     }
  98.  
  99.     public boolean validateFinalStartMark() {
  100.         Stack<Character> stack = new Stack<Character>();
  101.         char c;        
  102.         for (int i = 0; i < html.length(); i++) {  
  103.             c = html.charAt(i);
  104.             if ((c == '<') && (html.charAt(i + 1) == '/')) {
  105.                 stack.push(c);
  106.             } else {
  107.                 if (c == '<') {
  108.                     stack.push(c);
  109.                 }
  110.             }
  111.             if (c == '>') {
  112.                 if (!stack.empty()) {
  113.                     stack.pop();
  114.                 } else {
  115.                     return false;
  116.                 }
  117.             }
  118.         }
  119.         return stack.size() == 0;
  120.     }
  121.  
  122.     // texto en HTML a analizar
  123. //    private String html
  124. //            = "<html>\n"
  125. //            + "<head>\n"
  126. //            + "<title>Mi pagina de ejemplo</title>\n"
  127. //            + "</head>\n"
  128. //            + "<body>\n"
  129. //            + "Aqui va otro contenido <br/>\n"
  130. //            + "</body>\n"
  131. //            + "</html>";
  132.    
  133.     private String html
  134.             ="<html>\n"
  135.             + "<head>\n"
  136.             + "<title>Title of tje document</title>\n"
  137.             + "</head>\n"
  138.             + "<body>\n"
  139.             + "<h1>This is a heading</h1>\n"
  140.             + "<p>This if a <br/> paragraph.</p>\n"
  141.             + "</body>\n"
  142.             + "</html>";
  143.  
  144. }
  145.  
  146.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement