Advertisement
Amorf

Untitled

Mar 16th, 2021
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.00 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.util.Scanner;
  8.  
  9. public class Main {
  10.  
  11.     static Scanner scanConsole;
  12.     final static int console = 1;
  13.     final static int file = 2;
  14.     public static int chooseInput(){
  15.         boolean isIncorrect;
  16.         int choose = 0;
  17.         do {
  18.             isIncorrect = false;
  19.             try {
  20.                 choose = Integer.parseInt(scanConsole.nextLine());
  21.             } catch (Exception e) {
  22.                 System.out.print("Ошибка ввода! Повторите выбор: ");
  23.                 isIncorrect = true;
  24.             }
  25.             if (!isIncorrect && choose != console && choose != file) {
  26.                 System.out.print("Ошибка ввода! Повторите выбор: ");
  27.                 isIncorrect = true;
  28.             }
  29.         } while(isIncorrect);
  30.         return choose;
  31.     }
  32.  
  33.     public static String inputPath(int choose) {
  34.         String path = "";
  35.         if (choose == file){
  36.             boolean isIncorrect;
  37.             System.out.println("Введите абсолютную ссылку на файл: ");
  38.             do {
  39.                 isIncorrect = false;
  40.                 path = scanConsole.nextLine();
  41.                 File outputFile = new File(path);
  42.                 if (!outputFile.exists()) {
  43.                     System.out.println("Файл не найден! Введите абсолютную ссылку на файл: ");
  44.                     isIncorrect = true;
  45.                 }
  46.                 if (!outputFile.canWrite()){
  47.                     System.out.println("Некорректный файл! Введите абсолютную ссылку на файл: ");
  48.                     isIncorrect = true;
  49.                 }
  50.             } while (isIncorrect);
  51.         }
  52.         return path;
  53.     }
  54.  
  55.     public static int checkFile(int choose, String path){
  56.         if (choose == file){
  57.             File file = new File(path);
  58.             if (file.length() == 0){
  59.                 choose = console;
  60.                 System.out.println("Файл пуст. Ввод данных будет производиться с консоли.");
  61.             }
  62.         }
  63.         return choose;
  64.     }
  65.     public static String inputString(int choose, String path) throws FileNotFoundException {
  66.         String str;
  67.         if (choose == console) {
  68.             str = inputStringConsole();
  69.         } else {
  70.             str = inputStringFile(path);
  71.         }
  72.         return str;
  73.     }
  74.  
  75.     public static String inputStringConsole() {
  76.         boolean isIncorrect = false;
  77.         String str = "";
  78.         int i;
  79.         do {
  80.             System.out.print("Введите последовательность символов: ");
  81.             isIncorrect = false;
  82.             try {
  83.                 str = scanConsole.nextLine();
  84.             } catch (Exception e) {
  85.                 System.out.print("Ошибка ввода!");
  86.                 isIncorrect = true;
  87.             }
  88.             i = 0;
  89.             while (!isIncorrect & i < str.length()){
  90.                 if (str.charAt(i) != 'A' && str.charAt(i) != 'B' && str.charAt(i) != 'C'){
  91.                     System.out.print("Ошибка ввода!");
  92.                     isIncorrect = true;
  93.                 }
  94.                 i++;
  95.             }
  96.         } while(isIncorrect);
  97.         return str;
  98.     }
  99.  
  100.     public static String inputStringFile(String path) throws FileNotFoundException {
  101.         String str = "";
  102.         int i = 0;
  103.         boolean isIncorrect = false;
  104.         File file = new File(path);
  105.         Scanner scanFile = new Scanner(file);
  106.         try {
  107.             str = scanFile.nextLine();
  108.         } catch (Exception e) {
  109.             System.out.println("Ошибка ввода!");
  110.             str = inputStringConsole();
  111.         }
  112.         str.trim();
  113.         while (!isIncorrect & i < str.length()){
  114.             if (str.charAt(i) != 'A' && str.charAt(i) != 'B' && str.charAt(i) != 'C'){
  115.                 System.out.print("Ошибка ввода!");
  116.                 isIncorrect = true;
  117.             }
  118.             i++;
  119.         }
  120.         scanFile.close();
  121.         return str;
  122.     }
  123.  
  124.     public static String changeString(String str){
  125.         int check= -1;
  126.         check = str.indexOf("AAAA");
  127.         if (check != -1){
  128.             String[] strArr = str.split("AAAA");
  129.             str = "";
  130.             for (int i = 0; i < strArr.length; i++){
  131.                 str += strArr[i];
  132.             }
  133.             changeString(str);
  134.         }
  135.         check = str.indexOf("ABC");
  136.         if (check != -1){
  137.             String[] strArr = str.split("ABC");
  138.             str = "";
  139.             for (int i = 0; i < strArr.length; i++){
  140.                 str += strArr[i];
  141.             }
  142.             changeString(str);
  143.         }
  144.         check = str.indexOf("BABA");
  145.         if (check != -1){
  146.             int counter = 0;
  147.             char[] strToArray1 = new char[str.length()];
  148.             char[] strToArray2 = new char[str.length()];
  149.             for (int i = 0;i < check;i++){
  150.                 strToArray1[i] = str.charAt(i);
  151.             }
  152.             for (int i = check + 2; i < str.length();i++){
  153.                 strToArray2[counter] = str.charAt(i);
  154.                 counter++;
  155.             }
  156.             String str1 = new String(strToArray1);
  157.             String str2 = new String(strToArray2);
  158.             str = str1 + str2;
  159.             changeString(str);
  160.         }
  161.         return str;
  162.     }
  163.     public static void outputStringFile(String str, String path) throws IOException {
  164.         FileWriter writer = new FileWriter(path, false);
  165.         if (str.length() == 0){
  166.             writer.write("Строка пуста");
  167.         } else {
  168.             writer.write("Строка после преобразований имеет вид: " + str);
  169.             System.out.println("Данные записаны в файл");
  170.         }
  171.         writer.close();
  172.     }
  173.  
  174.     public static void outputStringConsole(String str){
  175.         if (str.length() == 0) {
  176.             System.out.println("Строка пуста");
  177.         } else {
  178.             System.out.print("Строка после преобразований имеет вид: " + str);
  179.         }
  180.     }
  181.     public static void outputString(String path, int choose, String str) throws IOException {
  182.         if (choose == file){
  183.             path = inputPath(choose);
  184.             choose = checkFile(choose, path);
  185.         }
  186.         if (choose == console){
  187.             outputStringConsole(str);
  188.         } else {
  189.             outputStringFile(str, path);
  190.         }
  191.     }
  192.  
  193.     public static void main(String[] args) throws IOException {
  194.         System.out.println("Строка символов состоит из английских букв A, B и С. Разработать рекурсивную процедуру, преобразующую данную строку по правилам:\n1)Удаляет четыре подряд идущих букв А\n2)Удаляет из последовательности ВАВА одну пару ВА\n3)Удаляет комбинацию АВС\nПреобразования выполнять до тех пор,пока  ни одной из перечисленных комбинаций не останется.");
  195.         System.out.println("Выберите, откуда будет производиться ввод:\n1.Консоль\n2.Файл");
  196.         scanConsole = new Scanner(System.in);
  197.         int choose = chooseInput();
  198.         String path = inputPath(choose);
  199.         choose = checkFile(choose, path);
  200.         String str = inputString(choose, path);
  201.         str = changeString(str);
  202.         System.out.println("Выберите, куда будет производиться вывод:\n1.Консоль\n2.Файл");
  203.         choose = chooseInput();
  204.         outputString(path, choose, str);
  205.         scanConsole.close();
  206.     }
  207. }
  208.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement