C++实现简易文本编辑器

 更新时间:2020年03月13日 07:44:29   作者:hmy_  
这篇文章主要为大家详细介绍了C++实现简易文本编辑器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C++实现文本编辑器的具体代码,供大家参考,具体内容如下

1.简易文本编辑器

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

#include<iostream>
#include<string>
#include<cstdlib>
#include<ctype.h>
#include<cstdio>
#include<fstream>
using namespace std;
int NumberCount=0;//数字个数
int CharCount=0;//字母个数
int PunctuationCount=0;//标点符号个数
int BlankCount=0;//空白符个数
 
class Node
{
public:
  string character;
  int cursor;
  int offset;
  Node* next;
  Node(){
    cursor=0;//每行的光标初始位置
    offset=0;//每行的初始偏移位置
    next=NULL;
  }
};
 
class TextEditor
{
private:
  Node* head;
  string name;
  int line;//可更改的行数
  int length;//行数
public:
  TextEditor();
  ~TextEditor();
  string GetName();
  void SetName(string name);
  int GetCursor();
  int MoveCursor(int offset);
  int SetCursor(int line,int offset);
  void AddText(const string s);
  void InsertText(int seat,string s);
  int FindText(string s);
  void DeleteText(string s);
  int GetLine();
  void Count();
  friend ostream& operator<<(ostream& out,TextEditor &text);
  Node* Gethead(){
    return head;
  }
  //int GetLength()
  //{
  //   return length;
  // }
 // int FindText(string s);
 // void DeleteText(int seat,string s);
};
 
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;
    delete p;
    p=q;
  }
 
}
 
int TextEditor::GetLine()
{
  return line;
}
 
string TextEditor::GetName()
{
  return name;
}
 
void TextEditor::SetName(string name)
{
  this->name=name;
}
 
int TextEditor::GetCursor()
{
  Node *p=head;
  while(p->next!=NULL)
    p=p->next;
  return p->cursor;
}
 
int TextEditor::MoveCursor(int offset)
{
  Node *p=head;
  int i=1;
  if(length+1<line){
    cout<<"输入错误!"<<endl;
    exit(0);
  }
  else{
    while(p->next!=NULL&&i<line){
      p=p->next;
      i++;
    }
  }
  if(offset>p->character.length()){
    cout<<"移动位置太大!"<<endl;
    exit(0);
  }
  else
    p->cursor+=offset;
  //cout<<"p ->cursor="<<p->cursor<<endl;
  return p->cursor;
}
 
int TextEditor::SetCursor(int line,int offset)
{
  this->line=line;
  //cout<<"line="<<this->line<<endl;
  return MoveCursor(offset);
}
 
void TextEditor::AddText(const string s)
{
  line=length+1;
  Node* p=new Node;
  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++;
}
 
void TextEditor::InsertText(int seat,string s)
{
  Node *p=head;
  int i=1;
  if(length+1<line){
    cout<<"输入错误!"<<endl;
    exit(0);
  }
  else{
    while(p->next!=NULL&&i<line){
      p=p->next;
      i++;
    }
  }
  //MoveCursor(seat);
  //cout<<"p->cursor="<<p->cursor<<endl;
  string substr;
  for(int i=seat;i<s.length()+seat&&i<=p->character.length();i++)
  substr+=p->character[i];
 
  p->character.insert(p->cursor,s);
 
 
  cout<<"substr="<<substr<<endl;
  DeleteText(substr);//覆盖子串
  p->cursor=0;//光标清零
}
 
ostream& operator<<(ostream& out,TextEditor &text)
{
  int i=1;
  Node* p=text.Gethead();
  while(p!=NULL){
    out<<p->character<<endl;
    p=p->next;
  }
  // cout<<"length="<<text.GetLength()<<endl;
  return out;
}
 
int TextEditor::FindText(string P)
{
  Node* q=head;
  //int templine=1;
  line=1;
  int p=0;
  int t=0;
  int plen=P.length()-1;
  //cout<<"P="<<P<<endl;
  //cout<<"plen="<<plen<<endl;
  int tlen=q->character.length();
  while(q!=NULL){
      p=0;
      t=0;
    tlen=q->character.length();
    if(tlen<plen){
      line++;
       q=q->next;
    }
 
    while(p<plen&&t<tlen){
      if(q->character[t]==P[p]){
        t++;
        p++;
      }
      else{
        t=t-p+1;
        p=0;
      }
    }
   // cout<<"P="<<P<<endl;
   // cout<<"p="<<p<<endl;
   // cout<<"plen="<<plen<<endl;
    if(p>=plen){
 
      return t-plen+1;
    }
 
 
    else{
      line++;
      q=q->next;
    }
 
  }
  return -1;
}
 
void TextEditor::DeleteText(string s)
{
  Node *p=head;
  int i=1;
  int k=FindText(s);
  if(k==-1)
    cout<<"未出现该字符串!"<<endl;
  else{
    while(p!=NULL&&i<line){
      p=p->next;
      // cout<<p->character<<endl;
      i++;
    }
 
    p->character.erase(k-1,s.length());
    cout<<"删除成功!"<<endl;
  }
}
 
void TextEditor::Count()
{
  Node *p=head;
  NumberCount=0;
  CharCount=0;
  PunctuationCount=0;
  BlankCount=0;
  while(p!=NULL){
      for(int i=0;i<p->character.length();i++){
        if(p->character[i]>='0'&&p->character[i]<='9')
          NumberCount++;
        else if(p->character[i]>'a'&&p->character[i]<'z'||p->character[i]>'A'&&p->character[i]<'Z')
          CharCount++;
        else if(ispunct(p->character[i]))
          PunctuationCount++;
        else if(p->character[i]==' ')
          BlankCount++;
      }
      p=p->next;
  }
}
 
int main()
{
  int i,j,k,n=2;
  string s,t,name;
  TextEditor text;
  cout<<"---------------------------------------"<<endl;
  cout<<"1.添加字符"<<endl;
  cout<<"2.设置文档名字"<<endl;
  cout<<"3.获取文档名字"<<endl;
  cout<<"4.显示光标位置"<<endl;
  cout<<"5.设置光标位置,在光标位置处插入文本"<<endl;
  cout<<"6.在文档中查找文本"<<endl;
  cout<<"7.在文档中删除文本"<<endl;
  cout<<"8.统计字母、数字、标点符号、空白符号及总字符个数"<<endl;
  cout<<"9.输入文本"<<endl;
  cout<<"0.退出"<<endl;
  while(n){
    // cout<<endl;
    cout<<endl;
    cout<<"---------------------------------------"<<endl;
    cout<<"请输入:";
    cin>>n;
    getchar();
    switch(n){
      case 1: cout<<"请输入字符:"; getline(cin,s,'\n'); text.AddText(s); break;
      case 2: cout<<"请输入文档名字:"; cin>>name; text.SetName(name); break;
      case 3: cout<<text.GetName()<<endl; break;
      case 4: cout<<"光标在第"<<text.GetLine()<<"行,第"<<text.GetCursor()<<"个字符前!"<<endl; break;
      case 5:{
        cout<<"输入行数:";
        cin>>i;
        cout<<"光标在第"<<text.GetCursor()<<"个字符前!"<<endl;
        cout<<"输入移动位数:";
        cin>>j;
        cout<<"输入插入字符:";
        getchar();
        getline(cin,s);
        text.InsertText(text.SetCursor(i,j),s); break;
      }
      case 6: {
        cout<<"输入查找的字符串:";
        getline(cin,s);
        int k=text.FindText(s);
        if(k==-1)
          cout<<"查找失败!"<<endl;
        else
          cout<<"所查找文本首次出现在:"<<text.GetLine()<<"行,第"<<k<<"个字符处!"<<endl;
          break;
      }
      case 7: cout<<"输入要删除的字符串:"; getline(cin,s); text.DeleteText(s); break;
      case 8: {
        text.Count();
        cout<<"文档中共有:"<<endl;
        cout<<NumberCount<<"个数字"<<endl;
        cout<<CharCount<<"个字母"<<endl;
        cout<<PunctuationCount<<"个标点符号"<<endl;
        cout<<BlankCount<<"个空白字符"<<endl;
        cout<<"共有"<<NumberCount+CharCount+PunctuationCount+BlankCount<<"个字符!"<<endl;
        break;
      }
      case 9: cout<<text; break;
      case 0:{
        string ss=text.GetName();
        ss+=".txt";
        cout<<ss<<endl;
        ofstream outFile(ss.c_str());
        Node* p=text.Gethead();
        while(p!=NULL){
          outFile<<p->character<<endl;
          p=p->next;
        }
        exit(0);
        break;
      }
      default: cout<<"输入错误,请重新输入!"<<endl; break;
    }
 
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • c++重载运算符时返回值为类的对象或者返回对象的引用问题

    c++重载运算符时返回值为类的对象或者返回对象的引用问题

    这篇文章主要介绍了c++重载运算符时返回值为类的对象或者返回对象的引用问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-11-11
  • c++实现md5加密的代码

    c++实现md5加密的代码

    这篇文章主要介绍了c++实现md5加密的实例代码,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • C/C++ 监控磁盘与目录操作的示例

    C/C++ 监控磁盘与目录操作的示例

    这篇文章主要介绍了C/C++ 监控磁盘与目录操作的示例,帮助大家更好的理解和学习C/C++编程,感兴趣的朋友可以了解下
    2020-10-10
  • C++代码实现学生信息管理系统

    C++代码实现学生信息管理系统

    这篇文章主要为大家详细介绍了C++代码实现学生信息管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06
  • C语言中`||`的短路机制详解

    C语言中`||`的短路机制详解

    在C语言中,逻辑或运算符(||)是一种常用的逻辑运算符,用于组合多个条件表达式,C语言中的逻辑或运算符具有短路机制,这是一种非常重要的概念,本文将深入解释C语言中的||短路机制以及其在编程中的应用,感兴趣的朋友跟随小编一起看看吧
    2024-01-01
  • C语言设计三子棋小游戏

    C语言设计三子棋小游戏

    这篇文章主要为大家详细介绍了C语言设计三子棋小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-04-04
  • C语言 超详细梳理总结动态内存管理

    C语言 超详细梳理总结动态内存管理

    动态内存是相对静态内存而言的。所谓动态和静态就是指内存的分配方式。动态内存是指在堆上分配的内存,而静态内存是指在栈上分配的内存,本文带你深入探究C语言中动态内存的管理
    2022-03-03
  • C++使用yaml-cpp库操作YAML的示例代码

    C++使用yaml-cpp库操作YAML的示例代码

    配置文件有利于我们灵活配置工程,解决大量重复劳动,也方便调试,YAML 是一种人类可读的数据序列化格式,它使用缩进和特定的符号来表示数据结构,在本文中,我们将详细介绍如何在 C++ 中使用 yaml-cpp 库来解析和生成 YAML 格式的数据,需要的朋友可以参考下
    2024-10-10
  • 老程序员教你一天时间完成C语言扫雷游戏

    老程序员教你一天时间完成C语言扫雷游戏

    这篇文章主要为大家详细介绍了C语言实现扫雷游戏初级版,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • C++实现字符串转整数(atoi)的代码详解

    C++实现字符串转整数(atoi)的代码详解

    在编程中,经常会遇到将字符串转换为整数的需求,就像标准库中的 atoi 函数一样,本文给大家介绍了C++中字符串转整数(atoi)的实现与解析,并有详细的代码示例供大家参考,需要的朋友可以参考下
    2025-04-04

最新评论