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/EmelianovaED/homework_3/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
I didn't know what to write, so I collected funny phrases from the games.
If you want guarantees, buy a toaster!
I won't give you the stone.
Oh, save him, Doctor! A pretty face is all he has!
Wasted.
I've seen some shit!
166 changes: 166 additions & 0 deletions sem2/EmelianovaED/homework_3/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

std::vector<std::string> lines_orig;
std::vector<std::string> lines_rem;
std::vector<std::string> lines_rep;
std::vector<int> len_lines;
std::string result_serch;
int char_count = 0;
// ��������� �����
std::vector<std::string> readFile(const std::string& filename) {
std::vector<std::string> lines_in;
std::ifstream file_txt(filename.c_str());
std::string line_in;

if (!file_txt.is_open()) {
std::cout << "Error open input.txt" << filename << std::endl;
return lines_in;
}
while (std::getline(file_txt, line_in)) {
lines_in.push_back(line_in);
}
file_txt.close();
return lines_in;
}
// �������� �������� ������
void removeShortLines(std::vector<std::string>& lines, int threshold) {
auto is_short = [threshold](const std::string& str) {
return str.length() < static_cast<size_t>(threshold);
};
lines.erase(std::remove_if(lines.begin(), lines.end(), is_short),
lines.end());
}
// ������ �������� ������� �� _
void replaceSpaces(std::vector<std::string>& lines) {
auto replace_char = [](char ch) {
if (ch == ' ') {
return '_';
}
return ch;
};

auto all_strings = [&replace_char](std::string& str) {
std::transform(str.begin(), str.end(), str.begin(), replace_char);
};

std::for_each(lines.begin(), lines.end(), all_strings);
}
// ����� ������ �� �����
std::string findWord(const std::vector<std::string>& lines,
const std::string& word) {
auto contains_word = [&word](const std::string& str) {
return str.find(word) != std::string::npos;
};
auto id_ln = std::find_if(lines.begin(), lines.end(), contains_word);

if (id_ln != lines.end()) {
return *id_ln;
}
return "�� �������";
}

// ������� ��� �������� �������� ��� _
int countCharsWithoutUnderscore(const std::vector<std::string>& lines) {
int count_ch = 0;
auto count_in_string = [](const std::string& str) {
int c = 0;
for (size_t j = 0; j < str.length(); j++) {
if (str[j] != '_') {
c++;
}
}
return c;
};
auto accumulate_ch = [&count_ch, &count_in_string](const std::string& str) {
count_ch += count_in_string(str);
};

std::for_each(lines.begin(), lines.end(), accumulate_ch);

return count_ch;
}

// ������� ��� ��������� ���� �����
std::vector<int> getLengths(const std::vector<std::string>& lines) {
std::vector<int> lens_ln;
lens_ln.resize(lines.size());
auto getLength = [](const std::string& str) {
return static_cast<int>(str.length());
};
std::transform(lines.begin(), lines.end(), lens_ln.begin(), getLength);

return lens_ln;
}

// ������� ��� ������ ����������� � output.txt
void writeFullResults() {
std::ofstream file_out("output.txt");

if (!file_out.is_open()) {
std::cout << "������: �� ���� ������� output.txt" << std::endl;
return;
}

file_out << "Lines in input.txt" << std::endl;
for (size_t i = 0; i < lines_orig.size(); i++) {
file_out << lines_orig[i] << std::endl;
}

file_out << std::endl << "Lines after delete short strings" << std::endl;
for (size_t i = 0; i < lines_rem.size(); i++) {
file_out << lines_rem[i] << std::endl;
}

file_out << std::endl << "Lines after replace" << std::endl;
for (size_t i = 0; i < lines_rep.size(); i++) {
file_out << lines_rep[i] << std::endl;
}

file_out << std::endl << "Result serch: ";
file_out << result_serch << std::endl;

file_out << std::endl << "Count char: ";
file_out << char_count << std::endl;

file_out << std::endl << "Lens lines" << std::endl;
for (size_t i = 0; i < len_lines.size(); i++) {
file_out << "Line " << i + 1 << ": " << len_lines[i] << " chars"
<< std::endl;
}

file_out.close();
}

// ������� ������� ���������
void processLines(std::vector<std::string>& lines) {
lines_orig = lines;

int threshold_ln;
std::cout << "Enter a threshold for the length of the string: ";
std::cin >> threshold_ln;

removeShortLines(lines, threshold_ln);
lines_rem = lines;

replaceSpaces(lines);
lines_rep = lines;

std::string word_us;
std::cout << "Enter the search word: ";
std::cin >> word_us;
result_serch = findWord(lines, word_us);

char_count = countCharsWithoutUnderscore(lines);

len_lines = getLengths(lines);

writeFullResults();
}
int main() {
std::vector<std::string> lines = readFile("input.txt");
processLines(lines);
}
29 changes: 29 additions & 0 deletions sem2/EmelianovaED/homework_3/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Lines in input.txt
I didn't know what to write, so I collected funny phrases from the games.
If you want guarantees, buy a toaster!
I won't give you the stone.
Oh, save him, Doctor! A pretty face is all he has!
Wasted.
I've seen some shit!

Lines after delete short strings
I didn't know what to write, so I collected funny phrases from the games.
If you want guarantees, buy a toaster!
I won't give you the stone.
Oh, save him, Doctor! A pretty face is all he has!

Lines after replace
I_didn't_know_what_to_write,_so_I_collected_funny_phrases_from_the_games.
If_you_want_guarantees,_buy_a_toaster!
I_won't_give_you_the_stone.
Oh,_save_him,_Doctor!_A_pretty_face_is_all_he_has!

Result serch: I_won't_give_you_the_stone.

Count char: 154

Lens lines
Line 1: 73 chars
Line 2: 38 chars
Line 3: 27 chars
Line 4: 50 chars