Amorf

Untitled

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