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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
#include <iostream>
#include <vector>
#include <list>
#include <string>
// Write a find-and-replace operation for Documents
using Line = std::vector<char>;
using listIter = std::list<Line>::iterator;
using lineIter = Line::iterator;
class Text_iterator {
public:
listIter ln;
lineIter pos;
Text_iterator(listIter ll, lineIter pp)
: ln(ll), pos(pp) { }
char& operator*() { return *pos; }
Text_iterator& operator++();
bool operator==(const Text_iterator& other) const
{
return ln == other.ln && pos == other.pos;
}
bool operator!=(const Text_iterator& other) const
{
return !(*this == other);
}
};
//*****************************
Text_iterator& Text_iterator::operator++()
{
++pos;
if (pos == ln->end()) {
++ln; // "ln" pointing to the next vector/line
pos = ln->begin();
}
return *this;
}
//************************
class Document {
public:
std::list<Line> lines;
Document() { lines.push_back(Line{}); }
Text_iterator begin()
{
return Text_iterator(lines.begin(), lines.begin()->begin());
}
Text_iterator end()
{
listIter last = lines.end();
--last; // We know that the document is not empty
return Text_iterator(last, last->end());
}
};
//******************************************************
std::istream& operator>>(std::istream& is, Document& d)
{
char ch;
while (is.get(ch)) // why a loop and not an if?
{
d.lines.back().push_back(ch);
if (ch == '\n')
d.lines.push_back(Line{}); // An empty vector of char with uninitialized values
}
if (d.lines.back().size()) d.lines.push_back(Line{}); // Add final empty line
return is;
}
//******************************
Text_iterator my_find(Text_iterator first, Text_iterator last, const char& ch)
{
for (auto p = first; p != last; ++p)
if (*p == ch)
return p;
return last;
}
//*****************************************************************
bool match(Text_iterator p, Text_iterator last, const std::string& s)
{
for (int i = 0; i < s.size(); i++, ++p)
if (*p != s[i] || p == last) return false;
return true;
}
//********************************************
Text_iterator find_txt(Text_iterator first, Text_iterator last, const std::string& s)
{
if (s.empty()) return last; // can't find an empty string
char first_char = s[0];
while (true) {
auto p = my_find(first, last, first_char);
if (p == last || match(p, last, s)) return p;
first = ++p;
}
}
//*****************************************
template<class Iter>
void my_advance(Iter& p, int n)
{
while (n > 0) { ++p; --n; }
}
//*************************************
bool erase_line(Document& d, int n)
{
if (n < 0 || d.lines.size() - 1 <= n) return false;
auto p = d.lines.begin();
my_advance(p, n);
d.lines.erase(p);
return true;
}
//********************************
void replace(Text_iterator p, Document& d, Line s)
{
p.ln = d.lines.erase(p.ln);
d.lines.insert(p.ln, s);
}
//*************************
int main()
{
int num, n;
Line l;
bool b = true;
std::string s;
Document d;
auto p = d.begin();
std::cout << "Please enter characters for your list. Type ^z at the end.\n";
std::cin >> d;
std::cin.clear();
std::cout << "1: Searching a line.\n";
std::cout << "2: Erasing a line.\n";
std::cout << "3: Replacing a line.\n";
std::cout << "4: Printing the valuse.\n";
std::cout << "0: Exit.\n";
while (b)
{
if (!(std::cin >> n)) {
std::cout << "Out of Range or bad input!\n";
std::cin.clear();
std::cin.ignore(INT_MAX, '\n');
}
switch (n)
{
case 1:
std::cout << "Enter your line to search.\n";
std::cin.ignore();
getline(std::cin, s);
p = find_txt(d.begin(), d.end(), s);
if (p == d.end()) std::cout << "Not found!\n";
else std::cout << "Found!\n\n";
break;
case 2:
std::cout << "Please enter the number of the line: ";
if (std::cin >> num && erase_line(d, num - 1)) std::cout << "Erased!\n";
else {
std::cout << "Out of Range or bad input!\n";
std::cin.clear();
std::cin.ignore(INT_MAX, '\n');
}
break;
case 3:
std::cout << "Please enter your current line: ";
std::cin.ignore();
getline(std::cin, s);
p = find_txt(d.begin(), d.end(), s);
if (p == d.end()) { std::cout << "Ther is no such a line!\n"; break; }
std::cout << "Enter your new line: ";
getline(std::cin, s);
copy(s.begin(), s.end(), back_inserter(l));
l.push_back('\n');
replace(p, d, l);
break;
case 4:
for (p = d.begin(); p != d.end(); ++p)
std::cout << *p;
break;
case 0: b = false; break;
default: std::cout << "Something wrong was entered. Try again!\n";
break;
}
}
system("pause");
return 0;
}
|