Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.47 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 void main(String[] args) throws IOException {
  9. String str;
  10. str = inputInProgram();
  11. outputFromProgram(str);
  12. }
  13.  
  14. private static boolean isMakeChoice() {
  15. System.out.println("Введите 1 для ввода с консоли или 2, чтобы использовать файл.");
  16. Scanner in = new Scanner(System.in);
  17. boolean correct;
  18. String choiceNum;
  19. do {
  20. choiceNum = in.nextLine();
  21. correct = !choiceNum.equals("1") && !choiceNum.equals("2");
  22. if (correct) {
  23. System.out.println("Вы можете ввести только 1 или 2, повторите попытку:");
  24. }
  25. } while (correct);
  26. boolean choice = false;
  27. if (choiceNum.equals("1")) {
  28. choice = true;
  29. }
  30. return choice;
  31. }
  32.  
  33. private static String inputInProgram() throws IOException {
  34. boolean isInputMethod = isMakeChoice();
  35. String str;
  36. if (isInputMethod) {
  37. str = inputFromConsole();
  38. } else {
  39. str = inputFromFile();
  40. }
  41. return str;
  42. }
  43.  
  44. private static String inputFromConsole() {
  45. System.out.println("Введите вашу строку:");
  46. Scanner arrayIn = new Scanner(System.in);
  47. String str;
  48. boolean correct;
  49. do {
  50. str = arrayIn.nextLine();
  51. correct = isIncorrectInput(str);
  52. if (correct) {
  53. System.out.println("Ваша строка не содержит скобок, введите строку со скобками:");
  54. correct = true;
  55. }
  56. } while (correct);
  57. return str;
  58. }
  59.  
  60. private static String inputFromFile() throws IOException {
  61. Scanner arrayIn = new Scanner(System.in);
  62. System.out.println("Введите путь к файлу:");
  63. String str = "";
  64. boolean correct;
  65. do {
  66. String filePath = arrayIn.nextLine();
  67. try {
  68. BufferedReader in = new BufferedReader(new FileReader(filePath));
  69. str = in.readLine();
  70. in.close();
  71. correct = isIncorrectInput(str);
  72. if (correct) {
  73. System.out.println("Ваш файл не содержит скобок, откуройте файл со скобками:");
  74. correct = true;
  75. }
  76. } catch (FileNotFoundException fnf) {
  77. System.out.println("Перепроверьте введенный путь и повторите попытку:");
  78. correct = true;
  79. }
  80. } while (correct || isIncorrectInput(str));
  81. return str;
  82. }
  83.  
  84. private static void outputFromProgram(String str) {
  85. boolean outputChoice = isMakeChoice();
  86. if (outputChoice) {
  87. showProgramAnswer(str);
  88. } else {
  89. outputInFile(str);
  90. }
  91. }
  92.  
  93. private static void showProgramAnswer(String str) {
  94. System.out.print("Ваша строка: " + str);
  95. System.out.print("\n" + makeLogicOfProgram(isIncorrectInput(str), str));
  96. }
  97.  
  98. private static void outputInFile(String str) {
  99. Scanner in = new Scanner(System.in);
  100. System.out.println("Учтите, что ваши данные должны быть записаны в первой строке.");
  101. System.out.println("Введите путь к файлу:");
  102. boolean correct = false;
  103. do {
  104. String filePath = in.nextLine();
  105. try {
  106. FileWriter output = new FileWriter(filePath);
  107. output.write("Ваша строка: " + str);
  108. output.write("line.separator");
  109. output.write(makeLogicOfProgram(isIncorrectInput(str), str));
  110. output.close();
  111. } catch (FileNotFoundException fnf) {
  112. System.out.println("Не удается найти указанный файл, повторите попытку:");
  113. correct = true;
  114. } catch (IOException ioe){
  115. System.out.println("Файл с таким именем уже существует, повторите попытку:");
  116. correct = true;
  117. }
  118. } while (correct);
  119. }
  120.  
  121. private static String makeLogicOfProgram(boolean inputIsIncorrect, String str) {
  122. if (inputIsIncorrect) {
  123. System.out.println("Здесь нет скобок.");
  124. } else {
  125. char[] arrayOfBrackets = new char[1];
  126. arrayOfBrackets = makeBracketArray(arrayOfBrackets, str);
  127. if (isNoBalance(arrayOfBrackets)) {
  128. str = "Нет баланса.";
  129. }
  130. if (isBracketsBalanced(arrayOfBrackets)) {
  131. str = "Баланс имеется.";
  132. }
  133. }
  134. return str;
  135. }
  136.  
  137. private static char[] makeBracketArray(char[] array, String str) {
  138. int count = 1;
  139. for (int i = 0; i < str.length(); i++) {
  140. if (str.charAt(i) == '(') {
  141. array[count - 1] = '(';
  142. count++;
  143. array = addAnIndex(array);
  144. }
  145. if (str.charAt(i) == ')') {
  146. array[count - 1] = ')';
  147. count++;
  148. array = addAnIndex(array);
  149. }
  150. }
  151. return array;
  152. }
  153.  
  154. private static boolean isNoBalance(char[] array) {
  155. boolean result = false;
  156. if (array.length % 2 == 0) {
  157. result = true;
  158. } else {
  159. for (int i = 0; i < array.length; i++) {
  160. if (array[0] == ')' || array[array.length - 1] == '(') {
  161. result = true;
  162. }
  163. }
  164. }
  165. return result;
  166. }
  167.  
  168. private static boolean isBracketsBalanced(char[] array) {
  169. boolean result = false;
  170. int leftCountL = 0;
  171. int rightCountL = 0;
  172. int leftCountR = 0;
  173. int rightCountR = 0;
  174. for (int i = 0; i < (array.length / 2); i++) {
  175. if (array[i] == '(') {
  176. leftCountL++;
  177. }
  178. if (array[i] == ')') {
  179. rightCountL++;
  180. }
  181. }
  182. for (int i = (array.length / 2); i < array.length; i++) {
  183. if (array[i] == '(') {
  184. leftCountR++;
  185. }
  186. if (array[i] == ')') {
  187. rightCountR++;
  188. }
  189. }
  190. if (leftCountL == rightCountR && leftCountR == rightCountL) {
  191. result = true;
  192. }
  193. return result;
  194. }
  195.  
  196. private static char[] addAnIndex(char[] array) {
  197. char[] newArray = new char[array.length + 1];
  198. int count = 0;
  199. for (int i = 0; i < array.length; i++) {
  200. newArray[i] = array[count];
  201. count++;
  202. }
  203. return newArray;
  204. }
  205.  
  206. private static boolean isIncorrectInput(String str) {
  207. boolean result = true;
  208. for (int i = 0; i < str.length() && result; ) {
  209. if (str.charAt(i) == ')' || str.charAt(i) == '(') {
  210. result = false;
  211. } else {
  212. i++;
  213. }
  214. }
  215. return result;
  216. }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement