Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.42 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.*;
  4. import java.util.Scanner;
  5.  
  6. public class Main {
  7.  
  8.     public static int Pntr = 0;
  9.  
  10.     public static void main(String[] args) throws Exception{
  11.         System.out.println("This program will check the balance of the brackets in the lines");
  12.         IsBracketCorrect();
  13.     }
  14.  
  15.     public static char OpenBracket(final char Ch) {
  16.         switch (Ch) {
  17.             case (')'): return('(');
  18.             case (']'): return('[');
  19.             case ('}'): return ('{');
  20.             default: return ('0');
  21.         }
  22.     }
  23.  
  24.     public static void Push(char[] MassStack, char Ch ) {
  25.         MassStack[Pntr] = Ch;
  26.         Pntr++;
  27.     }
  28.  
  29.     public static char Pop(char[] MassStack) {
  30.         Pntr--;
  31.         if (Pntr > -1) {
  32.             return (MassStack[Pntr]);
  33.         }
  34.         else {
  35.             return ('0');
  36.         }
  37.     }
  38.  
  39.     public static boolean Proc(String StrSrc) {
  40.         int i = 0;
  41.         Pntr = 0;
  42.         char [] MassStack = new char[100];
  43.         boolean Balance = true;
  44.         while ((i <  StrSrc.length()) && Balance) {
  45.             if ((StrSrc.charAt(i) == '(') || (StrSrc.charAt(i) == '[') || (StrSrc.charAt(i) == '{')) {
  46.                 Push(MassStack, StrSrc.charAt(i));
  47.             }
  48.             else {
  49.                 if ((StrSrc.charAt(i) == ')') || (StrSrc.charAt(i) == ']') || (StrSrc.charAt(i) == '}')) {
  50.                     if (Pop(MassStack) != OpenBracket(StrSrc.charAt(i))) {
  51.                         Balance = false;
  52.                     }
  53.                 }
  54.             }
  55.             i++;
  56.         }
  57.         return((Pntr == 0) && Balance);
  58.     }
  59.  
  60.     public static boolean isStrCorrect(String StrSrc) {
  61.         boolean isCorrect = false;
  62.         for (int i = 0; i <  StrSrc.length(); i++) {
  63.             if ((StrSrc.charAt(i) == '(') || (StrSrc.charAt(i) == '[') || (StrSrc.charAt(i) == '{') || (StrSrc.charAt(i) == ')') || (StrSrc.charAt(i) == ']') || (StrSrc.charAt(i) == '}')) {
  64.                 isCorrect = true;
  65.             }
  66.         }
  67.         return (isCorrect);
  68.     }
  69.  
  70.     public static String FileNameInputRead() throws Exception {
  71.         boolean isCorrect = true;
  72.         String FileName;
  73.         do {
  74.             System.out.println("Enter file name:");
  75.             Scanner scan = new Scanner(System.in);
  76.             FileName = scan.next();
  77.             File FileIn = new File(FileName);
  78.             if (FileIn.exists()) {
  79.                 isCorrect = false;
  80.             }
  81.             else {
  82.                 System.out.println("This file does not exist!");
  83.             }
  84.         }
  85.         while (isCorrect);
  86.         return (FileName);
  87.     }
  88.  
  89.     public static void IsBracketCorrect() throws Exception {
  90.         String FileName = FileNameInputRead();
  91.         String StrSrc;
  92.         BufferedReader buff = new BufferedReader(new FileReader(FileName));
  93.         StrSrc = buff.readLine();
  94.         do {
  95.             System.out.println(StrSrc);
  96.             if (isStrCorrect(StrSrc)) {
  97.                 if (Proc(StrSrc)) {
  98.                     System.out.println("Brackets agreed.");
  99.                 }
  100.                 else {
  101.                     System.out.println("Brackets are not agreed.");
  102.                 }
  103.             }
  104.             else {
  105.                 System.out.println("There are no brackets in the line!");
  106.             }
  107.             StrSrc = buff.readLine();
  108.         }
  109.         while (StrSrc != null);
  110.         buff.close();
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement