diff --git a/sem2/ZolotovskiiED/homework_3/input.txt b/sem2/ZolotovskiiED/homework_3/input.txt new file mode 100644 index 00000000..3c03a9ca --- /dev/null +++ b/sem2/ZolotovskiiED/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 diff --git a/sem2/ZolotovskiiED/homework_3/main.cpp b/sem2/ZolotovskiiED/homework_3/main.cpp new file mode 100644 index 00000000..cd7ddb7d --- /dev/null +++ b/sem2/ZolotovskiiED/homework_3/main.cpp @@ -0,0 +1,114 @@ +#include +#include +#include +#include +#include +#include +#include + +int main() { + std::ifstream file_input("input.txt"); + std::ofstream file_output("output.txt"); + + std::vector start_lines; + std::string line; + + std::size_t max_len_line = 0; + std::string search_word; + + std::cin >> max_len_line; + std::cin >> search_word; + + while (std::getline(file_input, line)) { + start_lines.push_back(line); + } + + file_output << "Исходный вектор:\n"; + + std::for_each(start_lines.begin(), start_lines.end(), + [&file_output](const std::string& current_line) { + file_output << current_line << "\n"; + }); + + std::vector short_lines = start_lines; + + short_lines.erase( + std::remove_if(short_lines.begin(), short_lines.end(), + [max_len_line](const std::string& current_line) { + return current_line.length() < max_len_line; + }), + short_lines.end()); + + file_output << "\nВектор после удаления коротких строк:\n"; + + std::for_each(short_lines.begin(), short_lines.end(), + [&file_output](const std::string& current_line) { + file_output << current_line << "\n"; + }); + + std::vector space_lines = short_lines; + + std::for_each(space_lines.begin(), space_lines.end(), [](std::string& current_line) { + std::replace(current_line.begin(), current_line.end(), ' ', '_'); + }); + + file_output << "\nВектор после замены пробелов на '_':\n"; + + std::for_each(space_lines.begin(), space_lines.end(), + [&file_output](const std::string& current_line) { + file_output << current_line << "\n"; + }); + + std::vector found_lines; + + std::copy_if(space_lines.begin(), space_lines.end(), std::back_inserter(found_lines), + [&search_word](const std::string& current_line) { + return current_line.find(search_word) != std::string::npos; + }); + + file_output << "\nРезультат поиска заданного слова:\n"; + + if (!found_lines.empty()) { + std::for_each(found_lines.begin(), found_lines.end(), + [&file_output](const std::string& current_line) { + file_output << current_line << "\n"; + }); + } else { + file_output << "Строки с заданным словом не найдены\n"; + } + + std::vector count_lines = space_lines; + + std::for_each(count_lines.begin(), count_lines.end(), [](std::string& current_line) { + current_line.erase(std::remove(current_line.begin(), current_line.end(), '_'), + current_line.end()); + }); + + int count_symbols = 0; + + std::for_each(count_lines.begin(), count_lines.end(), + [&count_symbols](const std::string& current_line) { + count_symbols += current_line.length(); + }); + + file_output << "\nОбщее количество символов без '_':\n"; + file_output << count_symbols << "\n"; + + std::vector len_lines; + + std::transform(space_lines.begin(), space_lines.end(), std::back_inserter(len_lines), + [](const std::string& current_line) { + return current_line.length(); + }); + + file_output << "\nВектор длин строк:\n"; + + std::for_each(len_lines.begin(), len_lines.end(), [&file_output](int current_len) { + file_output << current_len << " "; + }); + + file_input.close(); + file_output.close(); + + return 0; +} diff --git a/sem2/ZolotovskiiED/homework_3/output.txt b/sem2/ZolotovskiiED/homework_3/output.txt new file mode 100644 index 00000000..197675b8 --- /dev/null +++ b/sem2/ZolotovskiiED/homework_3/output.txt @@ -0,0 +1,30 @@ +Исходный вектор: +Hello world +C++ programming is fun +Lambda expressions +STL algorithms +Short +A very long string just for testing purposes + +Вектор после удаления коротких строк: +Hello world +C++ programming is fun +Lambda expressions +STL algorithms +A very long string just for testing purposes + +Вектор после замены пробелов на '_': +Hello_world +C++_programming_is_fun +Lambda_expressions +STL_algorithms +A_very_long_string_just_for_testing_purposes + +Результат поиска заданного слова: +C++_programming_is_fun + +Общее количество символов без '_': +96 + +Вектор длин строк: +11 22 18 14 44 \ No newline at end of file