From 1c3417c6e304f5bb673335c792c42ad06f57bf6e Mon Sep 17 00:00:00 2001 From: cherednichenkobrand Date: Mon, 4 May 2026 17:53:05 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=B2=D0=B0=D0=BD=20=D0=A7=D0=B5=D1=80?= =?UTF-8?q?=D0=B5=D0=B4=D0=BD=D0=B8=D1=87=D0=B5=D0=BD=D0=BA=D0=BE=20=D1=81?= =?UTF-8?q?=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20=D0=BC=D0=B8=D0=BD=D0=B8=20?= =?UTF-8?q?=D0=B4=D0=B7=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sem2/Cherendichenko Ivan/mini_hw3/input.txt | 4 + sem2/Cherendichenko Ivan/mini_hw3/main.cpp | 17 +++ sem2/Cherendichenko Ivan/mini_hw3/output.txt | 30 ++++ .../mini_hw3/text_processor.cpp | 144 ++++++++++++++++++ .../mini_hw3/text_processor.h | 38 +++++ 5 files changed, 233 insertions(+) create mode 100644 sem2/Cherendichenko Ivan/mini_hw3/input.txt create mode 100644 sem2/Cherendichenko Ivan/mini_hw3/main.cpp create mode 100644 sem2/Cherendichenko Ivan/mini_hw3/output.txt create mode 100644 sem2/Cherendichenko Ivan/mini_hw3/text_processor.cpp create mode 100644 sem2/Cherendichenko Ivan/mini_hw3/text_processor.h diff --git a/sem2/Cherendichenko Ivan/mini_hw3/input.txt b/sem2/Cherendichenko Ivan/mini_hw3/input.txt new file mode 100644 index 00000000..0f2662ad --- /dev/null +++ b/sem2/Cherendichenko Ivan/mini_hw3/input.txt @@ -0,0 +1,4 @@ +SUMO TATAMI is the best game, worthy of the Game Awards. +The great Kondyusha is the best voice actor — give him an Oscar! +May Danya Grishin punish me for not following Google style. +SOLID is my bible. \ No newline at end of file diff --git a/sem2/Cherendichenko Ivan/mini_hw3/main.cpp b/sem2/Cherendichenko Ivan/mini_hw3/main.cpp new file mode 100644 index 00000000..8c825700 --- /dev/null +++ b/sem2/Cherendichenko Ivan/mini_hw3/main.cpp @@ -0,0 +1,17 @@ +#include +#include + +#include "../Header Files/text_processor.h" + +int main() { + std::size_t threshold = 0; + std::cout << "Enter minimum line length threshold: "; + std::cin >> threshold; + std::cin.ignore(); + std::string search_word; + std::cout << "Enter word to search: "; + std::getline(std::cin, search_word); + homework::runHomework("input.txt", "output.txt", threshold, search_word); + std::cout << "Done. Results written to output.txt\n"; + return 0; +} diff --git a/sem2/Cherendichenko Ivan/mini_hw3/output.txt b/sem2/Cherendichenko Ivan/mini_hw3/output.txt new file mode 100644 index 00000000..f58e42c2 --- /dev/null +++ b/sem2/Cherendichenko Ivan/mini_hw3/output.txt @@ -0,0 +1,30 @@ +===== SUMO TATAMI <3 <3 <3 ===== + +===== Mini-homework 3 (Variant 1) ===== + +1. Original lines from input.txt (4 lines): + [0] SUMO TATAMI is the best game, worthy of the Game Awards. + [1] The great Kondyusha is the best voice actor — give him an Oscar! + [2] May Danya Grishin punish me for not following Google style. + [3] SOLID is my bible. + +Threshold for short lines: 50 + +2. After removing short lines (3 lines): + [0] SUMO TATAMI is the best game, worthy of the Game Awards. + [1] The great Kondyusha is the best voice actor — give him an Oscar! + [2] May Danya Grishin punish me for not following Google style. + +3. After replacing spaces with '_' (3 lines): + [0] SUMO_TATAMI_is_the_best_game,_worthy_of_the_Game_Awards. + [1] The_great_Kondyusha_is_the_best_voice_actor_—_give_him_an_Oscar! + [2] May_Danya_Grishin_punish_me_for_not_following_Google_style. + +4. Search for word "SUMO": + found in: SUMO_TATAMI_is_the_best_game,_worthy_of_the_Game_Awards. + +5. Total characters (excluding '_'): 150 + +6. Line lengths (3 values): + [56, 66, 59] + diff --git a/sem2/Cherendichenko Ivan/mini_hw3/text_processor.cpp b/sem2/Cherendichenko Ivan/mini_hw3/text_processor.cpp new file mode 100644 index 00000000..ff901a79 --- /dev/null +++ b/sem2/Cherendichenko Ivan/mini_hw3/text_processor.cpp @@ -0,0 +1,144 @@ +#include "../Header Files/text_processor.h" + +#include +#include +#include +#include + +namespace homework { + +std::vector readLinesFromFile(const std::string& filename) { + std::vector lines; + std::ifstream input_file(filename); + std::string line; + while (std::getline(input_file, line)) { + lines.push_back(line); + } + return lines; +} + +std::vector filterShortLines(const std::vector& lines, + std::size_t threshold) { + std::vector result; + + auto isLongEnough = [threshold](const std::string& s) { + return s.length() >= threshold; + }; + std::copy_if(lines.begin(), lines.end(), std::back_inserter(result), + isLongEnough); + return result; +} + +std::vector replaceSpacesWithUnderscore( + const std::vector& lines) { + std::vector result; + result.reserve(lines.size()); + + auto replaceSpaces = [](std::string s) { + std::replace(s.begin(), s.end(), ' ', '_'); + return s; + }; + std::transform(lines.begin(), lines.end(), std::back_inserter(result), + replaceSpaces); + return result; +} + +std::string findFirstLineWithWord(const std::vector& lines, + const std::string& word) { + auto containsWord = [&word](const std::string& s) { + return s.find(word) != std::string::npos; + }; + auto it = std::find_if(lines.begin(), lines.end(), containsWord); + if (it != lines.end()) { + return *it; + } + return std::string(); +} + +std::size_t countCharactersWithoutUnderscore( + const std::vector& lines) { + return std::accumulate( + lines.begin(), lines.end(), static_cast(0), + [](std::size_t sum, const std::string& s) { + return sum + static_cast(std::count_if( + s.begin(), s.end(), [](char c) { return c != '_'; })); + }); +} + +std::vector computeLineLengths(const std::vector& lines) { + std::vector lengths; + lengths.reserve(lines.size()); + std::transform( + lines.begin(), lines.end(), std::back_inserter(lengths), + [](const std::string& s) { return static_cast(s.length()); }); + return lengths; +} + +namespace { + +void writeStringVector(std::ofstream& out, const std::string& title, + const std::vector& vec) { + out << title << " (" << vec.size() << " lines):\n"; + if (vec.empty()) { + out << " [empty]\n"; + } else { + for (std::size_t i = 0; i < vec.size(); ++i) { + out << " [" << i << "] " << vec[i] << "\n"; + } + } + out << "\n"; +} + +void writeIntVector(std::ofstream& out, const std::string& title, + const std::vector& vec) { + out << title << " (" << vec.size() << " values):\n ["; + for (std::size_t i = 0; i < vec.size(); ++i) { + out << vec[i]; + if (i + 1 < vec.size()) { + out << ", "; + } + } + out << "]\n\n"; +} + +} // namespace + +void writeResultsToFile(const std::string& filename, + const std::vector& original, + const std::vector& filtered, + const std::vector& replaced, + std::size_t threshold, const std::string& search_word, + const std::string& search_result, + std::size_t character_count, + const std::vector& lengths) { + std::ofstream out(filename); + out << "===== SUMO TATAMI <3 <3 <3 =====\n\n"; + out << "===== Mini-homework 3 (Variant 1) =====\n\n"; + writeStringVector(out, "1. Original lines from input.txt", original); + out << "Threshold for short lines: " << threshold << "\n\n"; + writeStringVector(out, "2. After removing short lines", filtered); + writeStringVector(out, "3. After replacing spaces with '_'", replaced); + out << "4. Search for word \"" << search_word << "\":\n"; + if (search_result.empty()) { + out << " [no line containing this word]\n\n"; + } else { + out << " found in: " << search_result << "\n\n"; + } + out << "5. Total characters (excluding '_'): " << character_count << "\n\n"; + writeIntVector(out, "6. Line lengths", lengths); +} + +void runHomework(const std::string& input_filename, + const std::string& output_filename, std::size_t threshold, + const std::string& search_word) { + auto original = readLinesFromFile(input_filename); + auto filtered = filterShortLines(original, threshold); + auto replaced = replaceSpacesWithUnderscore(filtered); + auto found = findFirstLineWithWord(replaced, search_word); + auto count = countCharactersWithoutUnderscore(replaced); + auto lengths = computeLineLengths(replaced); + writeResultsToFile(output_filename, original, filtered, replaced, threshold, + search_word, found, count, lengths); +} + +} // namespace homework diff --git a/sem2/Cherendichenko Ivan/mini_hw3/text_processor.h b/sem2/Cherendichenko Ivan/mini_hw3/text_processor.h new file mode 100644 index 00000000..1836226b --- /dev/null +++ b/sem2/Cherendichenko Ivan/mini_hw3/text_processor.h @@ -0,0 +1,38 @@ +#pragma once + +#include +#include +#include + +namespace homework { + +std::vector readLinesFromFile(const std::string& filename); + +std::vector filterShortLines(const std::vector& lines, + std::size_t threshold); + +std::vector replaceSpacesWithUnderscore( + const std::vector& lines); + +std::string findFirstLineWithWord(const std::vector& lines, + const std::string& word); + +std::size_t countCharactersWithoutUnderscore( + const std::vector& lines); + +std::vector computeLineLengths(const std::vector& lines); + +void writeResultsToFile(const std::string& filename, + const std::vector& original, + const std::vector& filtered, + const std::vector& replaced, + std::size_t threshold, const std::string& search_word, + const std::string& search_result, + std::size_t character_count, + const std::vector& lengths); + +void runHomework(const std::string& input_filename, + const std::string& output_filename, std::size_t threshold, + const std::string& search_word); + +} // namespace homework