#include <iostream>
#include<string>
#include<cstdio>
#include<list>
#include<fstream>
using namespace std;
class TextEditor
{
public:
list<string> article;
int cursor;
int line;
int offset;
int total;
ofstream out_file;
string name;
int *n;
void getname();
void setname();
const int getCursor(int *pline=NULL,int *poffset=NULL);
void moveCursor(int origin);
void setCursor(int line,int offset);
void AddText(const string& text);
void InsertText(string text);
void FindText( const string& text );
void DeleteText(int cursor,int length);
void DeleteText(string &p);
void WordStat();
friend ostream& operator<<(ostream& out,TextEditor &edit);
friend istream& operator>>(istream& in,TextEditor &edit);
// void TextEditor();
void edit();
void textshow();
void print();
};
void TextEditor::getname()//获取文件名
{
cout<<name<<endl;
}
void TextEditor::setname()
{
cout<<"Please input the file name"<<endl;
cin>>name;
}
void TextEditor::AddText(const string &text)
{
article.push_back(text);
}
void TextEditor::InsertText(string text)
{
int i=1;
list<string>::iterator a;
for(a=article.begin();i!=line;a++,i++);/*走到该行*/
a->insert(offset,text);
offset+=a->size();
}
void TextEditor::moveCursor(int origin)/*origin 是初始位置*/
{
line=1;
cursor=origin;
list<string>::iterator a;
for(a=article.begin();a!=article.end();a++)
{
int c=a->size();//该行长度
if((cursor-c)>0)
{
cursor-=a->size();
line++;
}
else
{
offset=cursor-1;
break;
}
}
cout<<"当前光标位置"<<line<<" "<<offset<<endl;
}
void TextEditor::setCursor(int line,int offset)
{
this->line=line;
this->offset=offset;
cursor=0;
int i=0;
list<string>::iterator a;
for(a=article.begin();i!=line;i++,a++)
{
cursor+=a->size();
}
cursor+=offset;
}
void TextEditor::FindText(const string&p)
{
line=1;
offset=0;
list<string>::iterator t;
for(t=article.begin();t!=article.end();t++)
{
int j=t->find(p,0);
if(j!=-1)
{
offset=t->find(p,0);
cout<<"查找字符串"<<line<<" "<<offset<<endl;
break;
}
line++;
}
if(t==article.end())
{
cout<<"未查找到"<<endl;
}
}
void TextEditor::DeleteText(int cursor,int length)
{
moveCursor(cursor);
list<string>::iterator t;
int i=1;
for(t=article.begin();i!=line;i++,t++);
t->erase(offset,length);
}
void TextEditor::DeleteText(string &p)
{
list<string>::iterator t;
string s;
int u;
int i=0;
for(t=article.begin();t!=article.end();t++)
{
s=s+*t;
}
i=s.find(p,0);
if(i==-1)
{
cout<<"未找到匹配字符串"<<endl;
}
else
{
int len=p.size();
moveCursor(i+1);
int j=1;
for(t=article.begin();j!=line;j++,t++);
if(t->size()-offset>len)
{
t->erase(offset,len);
}
else
{
u=t->size();
t->erase(offset,t->size()-offset);
t++;
t->erase(0,len+offset-u);
}
}
}
void TextEditor::WordStat()
{
list<string>::iterator a;
int i=0;
int j=0;
int h=0;
int g=0;
int f=0;
cout<<"文档---"<<"<<"<<name<<">>--内容:"<<endl;
for(a=article.begin();a!=article.end();a++)
{
cout<<*a<<endl;
}
for(a=article.begin();a!=article.end();a++)
{
i=0;
while((*a)[i]!='\0')
{
if((*a)[i]>=65&&(*a)[i]<=91||(*a)[i]>=97&&(*a)[i]<123)/*字母*/
j++;
else if((*a)[i]>=48&&(*a)[i]<=57)/*数字*/
h++;
else if((*a)[i]==' ')/*空格*/
g++;
else
f++;/*其他*/
i++;
}
}
cout<<"字母个数"<<j<<"数字个数"<<h<<"空格个数"<<g
<<"其他个数"<<f
<<"字符总个数"<<j+h+g+f<<endl;
}
void TextEditor::textshow()
{
list<string>::iterator a;
ofstream outf("data.text");
outf<<"<<"<<name<<">>"<<endl;
for(a=article.begin();a!=article.end();a++)
{
outf<<*a<<endl;
}
}
void TextEditor::print()
{
cout<<"---------文本编辑器------"<<endl;
cout<<"-------1.读取文件------"<<endl;
cout<<"-------2.插入文本------"<<endl;
cout<<"-------3.查找文本------"<<endl;
cout<<"-------4.删除文本------"<<endl;
cout<<"-------5.移动光标------"<<endl;
cout<<"-------6.输出文本------"<<endl;
cout<<"-------7.写入文件并推出------"<<endl;
}
void TextEditor::edit()
{
string text;
int i,j,h;
setname();
print();
cin>>i;
ifstream inf("data.text");
if(!inf)
{
cout<<"can not open"<<endl;
}
while(i!=7)
{
switch(i)
{
case 1:
while(!inf.eof())
{
inf>>text;
AddText(text);
}
cout<<"读取成功"<<endl;
textshow();
print();
cin>>i;
break;
case 2:
{
cout<<"输入插入文本";
cin>>text;
cout<<"输入插入位置";
cin>>j>>h;
setCursor(j,h);
InsertText(text);
textshow();
print();
cin>>i;
break;
}
case 3:
{
cout<<"输入查找字符串";
cin>>text;
FindText(text);
print();
cin>>i;
break;
}
case 4:
{
cout<<"输入删除字符串";
cin>>text;
DeleteText(text);
textshow();
print();
cin>>i;
break;
}
case 5:
{
cout<<"输入光标移动位置";
cin>>j;
moveCursor(j);
textshow();
print();
cin>>i;
break;
}
case 6:
{
WordStat();
textshow();
print();
cin>>i;
break;
}
}
}
}
int main()
{
TextEditor a;
a.edit();
return 0;
}
文本编辑器(链表+string+光标移动)
最新推荐文章于 2022-06-05 13:25:37 发布