如何用C++实现文本编辑器?

2022-04-01 教育 96阅读

1.简易文本编辑器

2.用链表实现,保存到文件中

  • #include

  • #include

  • #include

  • #include

  • #include

  • #include

  • usingnamespacestd;

  • intNumberCount=0;//数字个数

  • intCharCount=0;//字母个数

  • intPunctuationCount=0;//标点符号个数

  • intBlankCount=0;//空白符个数

  • classNode

  • {

  • public:

  • stringcharacter;

  • intcursor;

  • intoffset;

  • Node*next;

  • Node(){

  • cursor=0;//每行的光标初始位置

  • offset=0;//每行的初始偏移位置

  • next=NULL;

  • }

  • };

  • classTextEditor

  • {

  • private:

  • Node*head;

  • stringname;

  • intline;//可更改的行数

  • intlength;//行数

  • public:

  • TextEditor();

  • ~TextEditor();

  • stringGetName();

  • voidSetName(stringname);

  • intGetCursor();

  • intMoveCursor(intoffset);

  • intSetCursor(intline,intoffset);

  • voidAddText(conststrings);

  • voidInsertText(intseat,strings);

  • intFindText(strings);

  • voidDeleteText(strings);

  • intGetLine();

  • voidCount();

  • friendostream&operator

  • Node*Gethead(){

  • returnhead;

  • }

  • //intGetLength()

  • //{

  • //  returnlength;

  • //}

  • // intFindText(strings);

  • // voidDeleteText(intseat,strings);

  • };

  • TextEditor::TextEditor()

  • {

  • head=NULL;

  • name="test";//文件初始名

  • //tail=NULL;

  • line=1;

  • length=0;

  • }

  • TextEditor::~TextEditor()

  • {

  • Node*p=head;

  • Node*q;

  • while(p!=NULL){

  • q=p->next;

  • deletep;

  • p=q;

  • }

  • }

  • intTextEditor::GetLine()

  • {

  • returnline;

  • }

  • stringTextEditor::GetName()

  • {

  • returnname;

  • }

  • voidTextEditor::SetName(stringname)

  • {

  • this->name=name;

  • }

  • intTextEditor::GetCursor()

  • {

  • Node*p=head;

  • while(p->next!=NULL)

  • p=p->next;

  • returnp->cursor;

  • }

  • intTextEditor::MoveCursor(intoffset)

  • {

  • Node*p=head;

  • inti=1;

  • if(length+1

  • cout

  • exit(0);

  • }

  • else{

  • while(p->next!=NULL&&i

  • p=p->next;

  • i++;

  • }

  • }

  • if(offset>p->character.length()){

  • cout

  • exit(0);

  • }

  • else

  • p->cursor+=offset;

  • //coutcursor="cursor

  • returnp->cursor;

  • }

  • intTextEditor::SetCursor(intline,intoffset)

  • {

  • this->line=line;

  • //coutline

  • returnMoveCursor(offset);

  • }

  • voidTextEditor::AddText(conststrings)

  • {

  • line=length+1;

  • Node*p=newNode;

  • Node*q=head;

  • p->character=s;

  • p->next=NULL;

  • if(head==NULL)

  • head=p;

  • else{

  • while(q->next!=NULL)

  • q=q->next;

  • q->next=p;

  • }

  • length++;

  • //line++;

  • }

  • voidTextEditor::InsertText(intseat,strings)

  • {

  • Node*p=head;

  • inti=1;

  • if(length+1

  • cout

  • exit(0);

  • }

  • else{

  • while(p->next!=NULL&&i

  • p=p->next;

  • i++;

  • }

  • }

  • //MoveCursor(seat);

  • //coutcursor="cursor

  • stringsubstr;

  • for(inti=seat;icharacter.length();i++)

  • substr+=p->character[i];

  • p->character.insert(p->cursor,s);

  • cout

  • DeleteText(substr);//覆盖子串

  • p->cursor=0;//光标清零

  • }

  • ostream&operator

  • {

  • inti=1;

  • Node*p=text.Gethead();

  • while(p!=NULL){

  • outcharacter

  • p=p->next;

  • }

  • //cout

  • returnout;

  • }

  • intTextEditor::FindText(stringP)

  • {

  • Node*q=head;

  • //inttempline=1;

  • line=1;

  • intp=0;

  • intt=0;

  • intplen=P.length()-1;

  • //cout

  • //cout

  • inttlen=q->character.length();

  • while(q!=NULL){

  • p=0;

  • t=0;

  • tlen=q->character.length();

  • if(tlen

  • line++;

  • q=q->next;

  • }

  • while(p

  • if(q->character[t]==P[p]){

  • t++;

  • p++;

  • }

  • else{

  • t=t-p+1;

  • p=0;

  • }

  • }

  • // cout

  • // cout

  • // cout

  • if(p>=plen){

  • returnt-plen+1;

  • }

  • else{

  • line++;

  • q=q->next;

  • }

  • }

  • return-1;

  • }

  • voidTextEditor::DeleteText(strings)

  • {

  • Node*p=head;

  • inti=1;

  • intk=FindText(s);

  • if(k==-1)

  • cout

  • else{

  • while(p!=NULL&&i

  • p=p->next;

  • //coutcharacter

  • i++;

  • }

  • p->character.erase(k-1,s.length());

  • cout

  • }

  • }

  • voidTextEditor::Count()

  • {

  • Node*p=head;

  • NumberCount=0;

  • CharCount=0;

  • PunctuationCount=0;

  • BlankCount=0;

  • while(p!=NULL){

  • for(inti=0;icharacter.length();i++){

  • if(p->character[i]>='0'&&p->character[i]

  • NumberCount++;

  • elseif(p->character[i]>'a'&&p->character[i]<&>character[i]>'A'&&p->character[i]<&>

  • CharCount++;

  • elseif(ispunct(p->character[i]))

  • PunctuationCount++;

  • elseif(p->character[i]=='')

  • BlankCount++;

  • }

  • p=p->next;

  • }

  • }

  • intmain()

  • {

  • inti,j,k,n=2;

  • strings,t,name;

  • TextEditortext;

  • cout

  • cout

  • cout

  • cout

  • cout

  • cout

  • cout

  • cout

  • cout

  • cout

  • cout

  • while(n){

  • //cout

  • cout

  • cout

  • cout

  • cin>>n;

  • getchar();

  • switch(n){

  • case1:cout

  • case2:cout>name;text.SetName(name);break;

  • case3:cout

  • case4:cout

  • case5:{

  • cout

  • cin>>i;

  • cout

  • cout

  • cin>>j;

  • cout

  • getchar();

  • getline(cin,s);

  • text.InsertText(text.SetCursor(i,j),s);break;

  • }

  • case6:{

  • cout

  • getline(cin,s);

  • intk=text.FindText(s);

  • if(k==-1)

  • cout

  • else

  • cout

  • break;

  • }

  • case7:cout

  • case8:{

  • text.Count();

  • cout

  • cout

  • cout

  • cout

  • cout

  • cout

  • break;

  • }

  • case9:cout

  • case0:{

  • stringss=text.GetName();

  • ss+=".txt";

  • cout

  • ofstreamoutFile(ss.c_str());

  • Node*p=text.Gethead();

  • while(p!=NULL){

  • outFilecharacter

  • p=p->next;

  • }

  • exit(0);

  • break;

  • }

  • default:cout

  • }

  • }

  • }

声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com