Created
May 22, 2025 06:50
-
-
Save untainsYD/947c786a35f027b7d1f465435b6174ea to your computer and use it in GitHub Desktop.
Laboratory 5, Task 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package lab5; | |
| import lab5.sorting.IntegerSorter; | |
| import java.io.File; | |
| import java.io.IOException; | |
| import java.time.LocalDateTime; | |
| import java.time.format.DateTimeFormatter; | |
| /** | |
| * Демонстрація сортування цілих чисел за сумою цифр. | |
| * Читає числа з файлу, сортує за збільшенням та зменшенням суми цифр. | |
| */ | |
| public class Task2 { | |
| private static final String OUTPUT_DIR = "results/" + | |
| LocalDateTime.now().format( | |
| DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss") | |
| ) + "/"; | |
| public static void main(String[] args) { | |
| // Створення папки results з timestamp, якщо не існує | |
| File resultsDir = new File(OUTPUT_DIR); | |
| if (!resultsDir.exists()) { | |
| resultsDir.mkdirs(); | |
| System.out.println("Створено папку для результатів: " + OUTPUT_DIR); | |
| } | |
| System.out.println("====== Лабораторна робота №5 - Завдання 2 ======"); | |
| System.out.println("Сортування цілих чисел за сумою цифр"); | |
| System.out.println("Результати будуть збережені у: " + OUTPUT_DIR + "\n"); | |
| try { | |
| // Імена файлів | |
| String inputFile = OUTPUT_DIR + "input_numbers.txt"; | |
| String ascendingFile = OUTPUT_DIR + "sorted_ascending.txt"; | |
| String descendingFile = OUTPUT_DIR + "sorted_descending.txt"; | |
| // Створення тестового файлу з числами | |
| System.out.println("=== Створення тестового файлу ==="); | |
| IntegerSorter.createTestFile(inputFile); | |
| System.out.println("\n=== Обробка файлу та сортування ==="); | |
| // Виконання основного завдання | |
| IntegerSorter.sortIntegersByDigitSum(inputFile, ascendingFile, descendingFile); | |
| System.out.println("\n====== Завершення демонстрації ======"); | |
| System.out.println("Усі результати збережено у папці: " + OUTPUT_DIR); | |
| // Виведення списку створених файлів | |
| File[] files = resultsDir.listFiles(); | |
| if (files != null && files.length > 0) { | |
| System.out.println("\nСтворені файли:"); | |
| for (File file : files) { | |
| System.out.println(" - " + file.getName()); | |
| } | |
| } | |
| } catch (IOException e) { | |
| System.err.println("Помилка роботи з файлами: " + e.getMessage()); | |
| e.printStackTrace(); | |
| } catch (Exception e) { | |
| System.err.println("Загальна помилка: " + e.getMessage()); | |
| e.printStackTrace(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment