Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions sem2/ZolotovskiiED/homework_3/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Hello world
C++ programming is fun
Lambda expressions
STL algorithms
Short
A very long string just for testing purposes
114 changes: 114 additions & 0 deletions sem2/ZolotovskiiED/homework_3/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#include <algorithm>
#include <cstddef>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

int main() {
std::ifstream file_input("input.txt");
std::ofstream file_output("output.txt");

std::vector<std::string> 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<std::string> 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<std::string> 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<std::string> 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<std::string> 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<int> 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;
}
30 changes: 30 additions & 0 deletions sem2/ZolotovskiiED/homework_3/output.txt
Original file line number Diff line number Diff line change
@@ -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