I_LIKE_COFFEE

скобки

Oct 28th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.83 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class StackArray {
  5.     private int size;
  6.     private char[] array;
  7.     private int top;
  8.  
  9.     public StackArray(int s){//конструктор стека
  10.         size = s;
  11.         array = new char[size];
  12.         top = -1;
  13.     }
  14.  
  15.     public boolean isEmpty(){//проверяем стек на пустоту
  16.         return (top == -1);
  17.     }
  18.  
  19.     public void push(char a){//добавление элемента в стек
  20.         top++;
  21.         array[top] = a;
  22.     }
  23.  
  24.     public char pop(){//удаление элемента из стека
  25.         if (top == -1) {
  26.             System.out.println("Stack is empty");
  27.             return '\0';
  28.         }
  29.         else {
  30.             return array[top--];
  31.         }
  32.     }
  33.  
  34.     public int pick(){//верхушка элемента
  35.         if (top == -1) {
  36.             System.out.println("Stack is empty");
  37.             return 0;
  38.         }
  39.         else {
  40.             return array[top];
  41.         }
  42.     }
  43.  
  44.  
  45.     public static boolean checkPair(char el1, char el2){//чекаем пару
  46.         if (el1 == '(' && el2 == ')')
  47.  
  48.             return true;
  49.  
  50.         else if (el1 == '{' && el2 == '}')
  51.  
  52.             return true;
  53.  
  54.         else if (el1 == '[' && el2 == ']')
  55.  
  56.             return true;
  57.  
  58.         else
  59.  
  60.             return false;
  61.  
  62.     }
  63.  
  64.  
  65.  
  66.  
  67.     public static boolean balance(int a, ArrayList<Character> myArray){//самое интересное, проверяем баланс
  68.         StackArray stack = new StackArray(a);
  69.  
  70.         for (int i = 0; i < a; i++){
  71.             if (myArray.get(i) == '{' || myArray.get(i) == '(' || myArray.get(i) == '['){//если собка открывающаяся
  72.                 stack.push(myArray.get(i));
  73.             }
  74.             if (myArray.get(i) == '}' || myArray.get(i) == ')' || myArray.get(i) == ']'){//если скобка закрывающаяся
  75.                 if (stack.isEmpty())//если стек пустой, то вернем ложь
  76.                     return false;
  77.                 else if (!checkPair(stack.pop(), myArray.get(i)))
  78.                     return false;
  79.             }
  80.  
  81.         }
  82.         return true;
  83.  
  84.     }
  85.  
  86.  
  87.  
  88.     public static void main(String[] args) {
  89.  
  90.         Scanner reader = new Scanner(System.in);
  91.         int n = reader.nextInt();
  92.         System.out.println("Количество элементов: " + n);
  93.  
  94.         ArrayList<Character> myArray = new ArrayList<>(n);
  95.         for (int i = 0; i < n; i++){
  96.             char el = reader.next().charAt(0);
  97.             myArray.add(i, el);
  98.             System.out.print(myArray.get(i) + " ");
  99.         }
  100.  
  101.         if (balance(n, myArray))
  102.  
  103.             System.out.println("Balanced ");
  104.  
  105.         else
  106.  
  107.             System.out.println("Not Balanced ");
  108.     }
  109. }
Add Comment
Please, Sign In to add comment