Advertisement
venik2405

lab4_2

Feb 28th, 2021
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.18 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Scanner;
  3.  
  4. public class lab4_2 {
  5.  
  6.     static Scanner scConsole = new Scanner(System.in);
  7.     public static int sum;
  8.  
  9.     static int matrixSizeInput() {
  10.         final int MIN_VALUE = 1;
  11.         final int MAX_VALUE = 8;
  12.         boolean isIncorrect;
  13.         int size = 0;
  14.         System.out.println("Введите разрядность числа");
  15.         do {
  16.             isIncorrect = false;
  17.             try {
  18.                 size = Integer.parseInt(scConsole.nextLine());
  19.             } catch (Exception e) {
  20.                 System.out.println("Введите натуральное число");
  21.                 isIncorrect = true;
  22.             }
  23.             if ((!isIncorrect) && ((size < MIN_VALUE) || (size > MAX_VALUE))) {
  24.                 System.out.println("\"Пожалуйста, введите натуральное число меньшее 9!");
  25.                 isIncorrect = true;
  26.             }
  27.         } while (isIncorrect);
  28.         return size;
  29.     }
  30.  
  31.     static int chooseOutputSource() {
  32.         Scanner in = new Scanner(System.in);
  33.         boolean isIncorrect;
  34.         int choice = 0;
  35.         do {
  36.             System.out.println("Выберите, куда будет производится вывод: 1 - файл, 2 - консоль.");
  37.             isIncorrect = false;
  38.             try {
  39.                 choice = Integer.parseInt(in.nextLine());
  40.             } catch (Exception e) {
  41.                 System.out.println("Введите корректное значение");
  42.                 isIncorrect = true;
  43.             }
  44.             if (!isIncorrect && choice != 1 && choice != 2) {
  45.                 System.out.println("Введите 1 или 2");
  46.                 isIncorrect = true;
  47.             }
  48.         } while (isIncorrect);
  49.         return choice;
  50.     }
  51.  
  52.     static void reverseArr(int[] numArr) {
  53.         for (int i = 0; i < numArr.length / 2; i++) {
  54.             int temp = numArr[i];
  55.             numArr[i] = numArr[numArr.length - 1 - i];
  56.             numArr[numArr.length - 1 - i] = temp;
  57.         }
  58.     }
  59.  
  60.     static int[] fillNumFromConsole(int size) {
  61.         final int MIN_NUM_VALUE = 1;
  62.         final int MAX_NUM_VALUE = 9;
  63.         boolean isIncorrect;
  64.         int[] numArr = new int[size];
  65.         for (int i = 0; i < size; i++) {
  66.             do {
  67.                 isIncorrect = false;
  68.                 numArr[i] = inputDigitFromConsole(i);
  69.                 if ((!isIncorrect) && ((numArr[i] < MIN_NUM_VALUE) || (numArr[i] > MAX_NUM_VALUE))) {
  70.                     System.out.println("Пожалуйста, введите натуральное число меньшее 9!");
  71.                     isIncorrect = true;
  72.                 }
  73.             } while (isIncorrect);
  74.         }
  75.         reverseArr(numArr);
  76.         return numArr;
  77.     }
  78.  
  79.     private static int inputDigitFromConsole(int digit) {
  80.         Scanner scConsole = new Scanner(System.in);
  81.         boolean isIncorrect;
  82.         int num = 0;
  83.         do {
  84.             System.out.print("Введите разряд номер: ");
  85.             System.out.println(digit + 1);
  86.             isIncorrect = false;
  87.             try {
  88.                 num = Integer.parseInt(scConsole.nextLine());
  89.             } catch (Exception e) {
  90.                 System.out.println("Введите число");
  91.                 isIncorrect = true;
  92.             }
  93.         } while (isIncorrect);
  94.         return num;
  95.     }
  96.  
  97.     static String getInputFileLocation() {
  98.         boolean isIncorrect;
  99.         String location;
  100.         Scanner file = null;
  101.         do {
  102.             isIncorrect = false;
  103.             System.out.println("Введите путь к файлу");
  104.             location = scConsole.nextLine();
  105.             File inputFile = new File(location);
  106.             if (!inputFile.exists()) {
  107.                 System.out.println("Файл не существует, введите снова");
  108.                 isIncorrect = true;
  109.             }
  110.         } while (isIncorrect);
  111.         if (file != null)
  112.             System.out.println("Файл успешно открыт");
  113.         return location;
  114.     }
  115.  
  116.     static int getSizeFromfile(String location) throws FileNotFoundException {
  117.         int size;
  118.         File inputFile = new File(location);
  119.         boolean isInCorrect = false;
  120.         Scanner fileIn = new Scanner(inputFile);
  121.         if (inputFile.length() == 0) {
  122.             System.out.println("Файл пуст, введите данные с консоли");
  123.             size = matrixSizeInput();
  124.         } else {
  125.             try {
  126.                 size = fileIn.nextInt();
  127.             } catch (Exception I0Exception) {
  128.                 System.out.println("Разрядность числа указана не верно");
  129.                 System.out.println("Введите разряд числа с консоли");
  130.                 size = matrixSizeInput();
  131.                 fileIn.next();
  132.                 isInCorrect = true;
  133.             }
  134.             if (isInCorrect && ((size < 1) || (size > 8))) {
  135.                 System.out.println("Размер числа не может быть меньше 1 или больше 8");
  136.                 System.out.println("Введите размер числа с консоли");
  137.                 size = matrixSizeInput();
  138.                 fileIn.next();
  139.             }
  140.         }
  141.         return size;
  142.     }
  143.  
  144.     static int[] inputNumFromFile(int size, int pos, String location) throws FileNotFoundException {
  145.         int[] numArr;
  146.         boolean isInCorrect = false;
  147.         File inputFile = new File(location);
  148.         Scanner fileIn = new Scanner(inputFile);
  149.         if (inputFile.length() == 0) {
  150.             System.out.println("Файл пуст, введите данные с консоли");
  151.             numArr = fillNumFromConsole(size);
  152.         } else {
  153.             for (int j = 0;j < pos; j++){
  154.                 fileIn.nextLine();
  155.             }
  156.             numArr = new int[size];
  157.             for (int i = 0; i < size; i++) {
  158.                 try {
  159.                     numArr[i] = fileIn.nextInt();
  160.                 } catch (Exception I0Exception) {
  161.                     System.out.println("Разряд номер: " + (i + 1) + " указан не верно.");
  162.                     System.out.println("Введите элемент с консоли");
  163.                     numArr[i] = inputDigitFromConsole(i);
  164.                 }
  165.                 if ((numArr[i] < 1) | (numArr[i] > 9)) {
  166.                     System.out.println("Разряд числа не может быть меньше 1 или больше 8");
  167.                     System.out.println("Введите цифру с консоли");
  168.                     numArr[i] = inputDigitFromConsole(i);
  169.                 }
  170.             }
  171.             reverseArr(numArr);
  172.         }
  173.         return numArr;
  174.     }
  175.  
  176.     static int chooseInput() {
  177.         boolean isIncorrect;
  178.         String line;
  179.         do {
  180.             isIncorrect = false;
  181.             System.out.println("Хотите ли вы извлечь массив из файла? (y/n)");
  182.             line = scConsole.nextLine().toLowerCase();
  183.             if (!line.equals("y") && !line.equals("n") && !line.equals("")) {
  184.                 isIncorrect = true;
  185.                 System.out.println("Введите корректное значение");
  186.             }
  187.         } while (isIncorrect);
  188.         if (line.equals("y") || line.equals("")) {
  189.             return 0;
  190.         } else {
  191.             return 1;
  192.         }
  193.     }
  194.  
  195.     static void print(int[] arrays) {
  196.         for (int i = arrays.length - 1; i >= 0; i--) {
  197.             System.out.print(arrays[i] + " ");
  198.         }
  199.         System.out.println();
  200.     }
  201.  
  202.     static void findSum(int[] firstNum, int[] secondNum, int digit) {
  203.         int order = 1;
  204.         for (int i = 0; i < digit; i++) {
  205.             order = order * 10;
  206.         }
  207.         if (digit < firstNum.length) {
  208.             int prevDig = ((firstNum[digit] + secondNum[digit]) % 10) * order;
  209.             int nextDig = ((firstNum[digit] + secondNum[digit]) / 10) * order * 10;
  210.             sum = sum + nextDig + prevDig;
  211.             findSum(firstNum, secondNum, digit + 1);
  212.         }
  213.     }
  214.  
  215.     private static PrintWriter getOutputFileLocation() {
  216.         boolean isIncorrect;
  217.         String location;
  218.         PrintWriter file = null;
  219.         do {
  220.             isIncorrect = false;
  221.             System.out.println("Введите путь к файлу:");
  222.             location = scConsole.nextLine();
  223.             try {
  224.                 file = new PrintWriter(location);
  225.             } catch (FileNotFoundException e) {
  226.                 isIncorrect = true;
  227.                 System.out.println("Файл по этому пути не найден");
  228.             }
  229.         } while (isIncorrect);
  230.         if (file != null)
  231.             System.out.println("Файл успешно открыт");
  232.         return file;
  233.     }
  234.  
  235.     private static void outputResultToFile(int size, int[] firstNum, int[] secondNum) throws IOException {
  236.         PrintWriter out = getOutputFileLocation();
  237.         out.println("Массив А:");
  238.         for (int i = size - 1; i >= 0; i--){
  239.             out.print(firstNum[i] + " ");
  240.         }
  241.         out.println();
  242.         out.println("Массив В:");
  243.         for (int i = size - 1; i >= 0; i--){
  244.             out.print(secondNum[i] + " ");
  245.         }
  246.         out.println();
  247.         out.print("Сумма чисел: ");
  248.         out.print(sum);
  249.         out.close();
  250.         System.out.println("Успешно сохранено");
  251.     }
  252.  
  253.  
  254.     static void outputResult(int source, int size, int [] firstNum, int[] secondNum) throws IOException {
  255.         switch (source) {
  256.             case 1:
  257.                 outputResultToFile(size, firstNum, secondNum);
  258.                 break;
  259.             case 2:
  260.                 System.out.println("Число А");
  261.                 print(firstNum);
  262.                 System.out.println("Число В");
  263.                 print(secondNum);
  264.                 System.out.print("Сумма чисел: ");
  265.                 System.out.println(sum);
  266.                 break;
  267.         }
  268.     }
  269.  
  270.     public static void main(String[] args) throws IOException {
  271.         System.out.println("Данная программа находит сумму двух чисел");
  272.         int chosenInput = chooseInput();
  273.         int[] firstNum;
  274.         int[] secondNum;
  275.         int size;
  276.         if (chosenInput == 0) {
  277.             String location = getInputFileLocation(); // D:\Delphi\fileinput.txt
  278.             size = getSizeFromfile(location);
  279.             firstNum = inputNumFromFile(size,1,  location);
  280.             secondNum = inputNumFromFile(size,2, location);
  281.         } else {
  282.             size = matrixSizeInput();
  283.             System.out.println("Введите число А");
  284.             firstNum = fillNumFromConsole(size);
  285.             System.out.println("Введите число В");
  286.             secondNum = fillNumFromConsole(size);
  287.         }
  288.         int digit = 0;
  289.         findSum(firstNum, secondNum, digit);
  290.         int choiсe = chooseOutputSource();
  291.         outputResult(choiсe, size, firstNum, secondNum);
  292.     }
  293. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement