65,210
社区成员
发帖
与我相关
我的任务
分享
class Text_Editor
{
private:
typedef vector<string> Line;
typedef vector<Line> Page;
typedef vector<Page> Text;
string content;
int page_count;
int line_count;
public:
Line line;
Page page;
Text text;
public:
Text_Editor();
Text_Editor(string&);
Text_Editor(ifstream&);
bool sort();
bool destroy();
void retrive();
friend string::iterator &Goto(Text_Editor&, int, int, int);
string::iterator &insert(string::iterator&, string::iterator&, string::iterator&);
string::iterator &erase(string::iterator&, string::iterator&);
string::iterator &erase(string::iterator&);
string ©s(string::iterator&, string::iterator&);
string::iterator &paste(string::iterator&, string::iterator&, string::iterator&, string::iterator&);
friend std::ostream& operator<<(std::ostream&, Text_Editor&);
};
Text_Editor::Text_Editor()
{
line_count = page_count = 0;
}
Text_Editor::Text_Editor(string &s)
{
line_count = page_count = 0;
swap(content,s);
sort();
}
Text_Editor::Text_Editor(ifstream &fin)
{
while (getline(fin, content));
sort();
}
bool Text_Editor::sort()
{
int i = 0;
int j = 0;
if (!text.empty())
text.clear();
auto temp_begin = content.begin();
auto temp_end = content.begin();
for (; j < content.size(); j++)
{
if (content[j] == '\n' || j%70 == 0||content.size()-j==0)
{
line_count++;
temp_end += j;
string stemp(temp_begin, temp_end);
line.push_back(stemp);
page.push_back(line);
temp_begin += stemp.size();
stemp.clear();
line.clear();
temp_end = content.begin();
i++;
if (i % 20 == 0||j==content.size())
{
page_count++;
text.push_back(page);
page.clear();
}
}
}
return true;
}
bool Text_Editor::destroy()
{
text.clear();
}
string::iterator &Goto(Text_Editor &te, int npage, int nline, int nstr)
{
int len = 0;
for (Text_Editor::Text::iterator ttemp = te.text.begin(); ttemp < te.text.begin() + npage; ttemp++)
{
if (ttemp != te.text.begin() + npage)
{
for (Text_Editor::Page::iterator ptemp = ttemp->begin(); ptemp != ttemp->end(); ptemp++)
{
Text_Editor::Line::iterator ltemp = ptemp->begin();
string stemp(ltemp->begin(), ltemp->end());
len += stemp.size();
}
}
else
{
for (Text_Editor::Page::iterator ptemp = ttemp->begin(); ptemp < ttemp->begin() + nline; ptemp++)
{
if (ptemp != ttemp->begin() + nline)
{
Text_Editor::Line::iterator ltemp = ptemp->begin();
string stemp(ltemp->begin(), ltemp->end());
len += stemp.size();
}
else
len += nstr;
}
}
}
return te.content.begin() + len;
}
string::iterator &Text_Editor::insert(string::iterator& pos, string::iterator& str_begin, string::iterator& str_end)
{
pos = content.insert(pos, str_begin, str_end);
content.begin();
content.end();
sort();
return pos;
}
string::iterator &Text_Editor::erase(string::iterator& begin, string::iterator& end)
{
end = content.erase(begin, end);
content.end();
sort();
return end;
}
string::iterator &Text_Editor::erase(string::iterator& str)
{
str = content.erase(str);
content.end();
sort();
return str;
}
string &Text_Editor::copys(string::iterator& begin, string::iterator& end)
{
return string(begin, end);
}
string::iterator &Text_Editor::paste(string::iterator& str_begin, string::iterator& str_end, string::iterator& begin, string::iterator& end)
{
begin = content.insert(content.erase(begin, end), str_begin, str_end);
content.begin();
content.end();
sort();
return begin;
}
std::ostream &operator<<(std::ostream &os, Text_Editor &te)
{
int page_count = 0;
int line_count = 0;
for (auto ptemp = te.text.begin(); ptemp != te.text.end(); ptemp++)
{
for (auto ltemp = ptemp->begin(); ltemp != ptemp->end(); ltemp++)
{
string temp(ltemp->begin(), ltemp->end());
os << temp;
line_count++;
}
page_count++;
os << page_count << "page " << line_count << "line" << endl;
}
return os;
}