-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
123 lines (98 loc) · 3.51 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iomanip>
std::vector<std::string> readLinesFromFile(const std::string& filename);
void printTextWithLineNumbers(const std::vector<std::string>& lines);
std::string connectLines(const std::vector<std::string>& lines);
std::vector<std::string> extractWords(const std::string& text);
void generateCrossReferences(const std::vector<std::string>& lines);
int main() {
try {
const std::string filename = "test.txt";
std::vector<std::string> lines = readLinesFromFile(filename);
std::cout << "Text with line numbers:\n";
printTextWithLineNumbers(lines);
std::cout << "\nCross-references table:\n";
generateCrossReferences(lines);
}
catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return -1;
}
return 0;
}
std::vector<std::string> readLinesFromFile(const std::string& filename) {
std::vector<std::string> lines;
std::ifstream file(filename);
if (!file) {
throw std::runtime_error("Failed to open the file.");
}
std::string line;
while (std::getline(file, line)) {
lines.push_back(line);
}
return lines;
}
void printTextWithLineNumbers(const std::vector<std::string>& lines) {
for (int i = 0; i < lines.size(); ++i) {
std::cout << i + 1 << ": " << lines[i] << std::endl;
}
}
void generateCrossReferences(const std::vector<std::string>& lines) {
std::map<std::string, std::vector<int>> wordTable;
std::vector<std::string> words = extractWords(connectLines(lines));
for (int i = 0; i < lines.size(); ++i) {
const std::string& line = lines[i];
for (const std::string& word : words) {
std::string lowerWord = word;
std::transform(lowerWord.begin(), lowerWord.end(), lowerWord.begin(), ::tolower);
std::string lowerLine = line;
std::transform(lowerLine.begin(), lowerLine.end(), lowerLine.begin(), ::tolower);
if (lowerLine.find(lowerWord) != std::string::npos) {
wordTable[word].push_back(i + 1);
}
}
}
std::cout << std::setw(15) << std::left << "Words:" << "Lines:\n";
for (const std::string& word : words) {
std::cout << std::setw(15) << std::left << word;
const std::vector<int>& lineNumbers = wordTable[word];
std::vector<int> uniqueLineNumbers(lineNumbers.begin(), lineNumbers.end());
std::sort(uniqueLineNumbers.begin(), uniqueLineNumbers.end());
for (int lineNumber : uniqueLineNumbers) {
std::cout << lineNumber << " ";
}
std::cout << '\n';
}
}
std::string connectLines(const std::vector<std::string>& lines) {
std::string text;
for (const std::string& line : lines) {
text += line + " ";
}
return text;
}
std::vector<std::string> extractWords(const std::string& text) {
std::vector<std::string> words;
std::istringstream iss(text);
std::string word;
while (iss >> word) {
std::string cleanedWord;
for (char c : word) {
if (std::isalpha(static_cast<unsigned char>(c))) {
cleanedWord += std::tolower(c);
}
}
if (!cleanedWord.empty()) {
words.push_back(cleanedWord);
}
}
std::sort(words.begin(), words.end());
words.erase(std::unique(words.begin(), words.end()), words.end());
return words;
}