Filip_Markoski

4.Lab4.2 Modified XML code (Solved)

Nov 5th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.09 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.NoSuchElementException;
  4.  
  5. interface Stack<E> {
  6.  
  7.     // The elements of the Stack are any kind of objects
  8.  
  9.     // Access methods:
  10.  
  11.     public boolean isEmpty();
  12.     // Returns true only if the stack is empty.
  13.  
  14.     public E peek();
  15.     // Returns the element on the top od the stack.
  16.  
  17.     // Transformation methods:
  18.  
  19.     public void clear();
  20.     // Clears the stack.
  21.  
  22.     public void push(E x);
  23.     // Adds x on the top of the stack.
  24.  
  25.     public E pop();
  26.     // Removes and returns the element on the top.
  27. }
  28.  
  29. class ArrayStack<E> implements Stack<E> {
  30.     private E[] elems;
  31.     private int depth;
  32.  
  33.     @SuppressWarnings("unchecked")
  34.     public ArrayStack(int maxDepth) {
  35.         // Creating new empty stack
  36.         elems = (E[]) new Object[maxDepth];
  37.         depth = 0;
  38.     }
  39.  
  40.  
  41.     public boolean isEmpty() {
  42.         // Returns true only if the stack is empty.
  43.  
  44.         return (depth == 0);
  45.     }
  46.  
  47.  
  48.     public E peek() {
  49.         // Returns the element on the top od the stack.
  50.         if (depth == 0)
  51.             throw new NoSuchElementException();
  52.         return elems[depth - 1];
  53.     }
  54.  
  55.  
  56.     public void clear() {
  57.         // Clears the stack.
  58.         for (int i = 0; i < depth; i++) elems[i] = null;
  59.         depth = 0;
  60.     }
  61.  
  62.  
  63.     public void push(E x) {
  64.         // Adds x on the top of the stack.
  65.         elems[depth++] = x;
  66.     }
  67.  
  68.  
  69.     public E pop() {
  70.         // Removes and returns the element on the top.
  71.         if (depth == 0)
  72.             throw new NoSuchElementException();
  73.         E topmost = elems[--depth];
  74.         elems[depth] = null;
  75.         return topmost;
  76.     }
  77. }
  78.  
  79. public class CheckXML {
  80.  
  81.     public static void main(String[] args) throws Exception {
  82.  
  83.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  84.         String s = br.readLine();
  85.         int n = Integer.parseInt(s);
  86.         String[] redovi = new String[n];
  87.  
  88.         for (int i = 0; i < n; i++)
  89.             redovi[i] = br.readLine();
  90.  
  91.         int isValid = 1;
  92.         ArrayStack<String> stackOpenTags = new ArrayStack<String>(1000);
  93.        
  94.         for (int i = 0; i < redovi.length; i++) {
  95.             if (redovi[i].charAt(0) == '[' &&
  96.                 redovi[i].charAt(1) != '/' &&
  97.                 redovi[i].charAt(redovi[i].length() - 1) == ']') {
  98.                 stackOpenTags.push(redovi[i]);
  99.             }
  100.             if (redovi[i].charAt(1) == '/') {
  101.                 if (!stackOpenTags.isEmpty()){
  102.                     String temp = stackOpenTags.pop();
  103.                     String opening = temp.substring(1, temp.length()-1);
  104.  
  105.                     String closing = redovi[i].substring(2, redovi[i].length()-1);
  106.  
  107.                     if (!opening.equals(closing)){
  108.                         isValid = 0;
  109.                         break;
  110.                     }
  111.  
  112.                 } else {
  113.                     isValid = 0;
  114.                     break;
  115.                 }
  116.             }
  117.         }
  118.  
  119.  
  120.         System.out.println(isValid);
  121.  
  122.         br.close();
  123.     }
  124. }
Add Comment
Please, Sign In to add comment