diff --git a/sem2/DrozdovaAA/mini_homework_3/input.txt b/sem2/DrozdovaAA/mini_homework_3/input.txt new file mode 100644 index 00000000..3f95ed8e --- /dev/null +++ b/sem2/DrozdovaAA/mini_homework_3/input.txt @@ -0,0 +1,6 @@ +Hello world +C++ programming is fun +Lambda expressions +STL algorithms +Short +A very long string just for testing purposes \ No newline at end of file diff --git a/sem2/DrozdovaAA/mini_homework_3/output.txt b/sem2/DrozdovaAA/mini_homework_3/output.txt new file mode 100644 index 00000000..d1ba4f78 --- /dev/null +++ b/sem2/DrozdovaAA/mini_homework_3/output.txt @@ -0,0 +1,32 @@ +original vector: +Hello world +C++ programming is fun +Lambda expressions +STL algorithms +Short +A very long string just for testing purposes + + vector after removing short lines: +Hello world +C++ programming is fun +Lambda expressions +STL algorithms +Short +A very long string just for testing purposes + + vector after replacing spaces with '_': +Hello_world +C++_programming_is_fun +Lambda_expressions +STL_algorithms +Short +A_very_long_string_just_for_testing_purposes + + search result for word 'Hello' +Hello_world + + total characters without '_': +101 + + vector of string lengths: +11 22 18 14 5 44 diff --git a/sem2/DrozdovaAA/mini_homework_3/sem2_mini_hw_3.cpp b/sem2/DrozdovaAA/mini_homework_3/sem2_mini_hw_3.cpp new file mode 100644 index 00000000..43795109 --- /dev/null +++ b/sem2/DrozdovaAA/mini_homework_3/sem2_mini_hw_3.cpp @@ -0,0 +1,114 @@ +#include +#include +#include +#include +#include + +void printStringVector(const std::vector& vec, + std::ofstream& out) { + for (const auto& str : vec) { + out << str << "\n"; + } +} + +void runProgram() { + std::ifstream file_contents("input.txt"); + std::vector original_lines; + std::string current_line; + + while (std::getline(file_contents, + current_line)) { // считываем в вектор с текущей строкой + original_lines.push_back(current_line); + } + file_contents.close(); + + // получаем данные от пользователя + int length_threshold = 0; + std::cout << "length threshold: "; + std::cin >> length_threshold; + + std::string search_word; + std::cout << "search word: "; + std::cin >> search_word; + + std::ofstream out_file("output.txt"); + if (!out_file.is_open()) { + std::cerr << "recording error\n"; + return; + } + + out_file << "original vector:\n"; + printStringVector(original_lines, out_file); + + // 1 - удаление всех строк меньше заданного значения + std::vector filtered_lines = original_lines; // копия строк + filtered_lines.erase( + std::remove_if(filtered_lines.begin(), filtered_lines.end(), + [length_threshold](const std::string& str) { + return str.length() < + static_cast(length_threshold); + }), + filtered_lines.end()); + + out_file << "\n vector after removing short lines:\n"; + printStringVector(filtered_lines, out_file); + + // 2 - замена пробелов на _ + std::vector underscored_lines = + filtered_lines; // копия отфитрованного + std::for_each( + underscored_lines.begin(), underscored_lines.end(), + [](std::string& str) { std::replace(str.begin(), str.end(), ' ', '_'); }); + + out_file << "\n vector after replacing spaces with '_': \n"; + printStringVector(underscored_lines, out_file); + + // 3 - поиск строки, содержащей слово пользователя + auto find_it = + std::find_if(filtered_lines.begin(), filtered_lines.end(), + [&search_word](const std::string& str) { + return str.find(search_word) != std::string::npos; + }); + + out_file << "\n search result for word '" << search_word << "' \n"; + if (find_it != filtered_lines.end()) { + // индекс найденной строки + auto index = std::distance(filtered_lines.begin(), find_it); + out_file << underscored_lines[index] << "\n"; + } else { + out_file << "word not found\n"; + } + + // 4 - общее кол-во символов без учёта _ + size_t total_chars = 0; + std::for_each(underscored_lines.begin(), underscored_lines.end(), + [&total_chars](const std::string& str) { + total_chars += std::count_if(str.begin(), str.end(), + [](char c) { return c != '_'; }); + }); + + out_file << "\n total characters without '_': \n"; + out_file << total_chars << "\n"; + + // 5 - вектор чисел с длинами строк + std::vector string_lengths; + string_lengths.reserve(underscored_lines.size()); + std::transform( + underscored_lines.begin(), underscored_lines.end(), + std::back_inserter(string_lengths), + [](const std::string& str) { return static_cast(str.length()); }); + + out_file << "\n vector of string lengths: \n"; + for (int len : string_lengths) { + out_file << len << " "; + } + out_file << "\n"; + + out_file.close(); + std::cout << "program completed" << std::endl; +} + +int main() { + runProgram(); + return 0; +}