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

1、上机作业呗(老师最近越来越没人性了)

2、代码

头文件texteditor.h

#ifndef TEXTEDITOR_H_INCLUDED
#define TEXTEDITOR_H_INCLUDED
#include"string.h"
#include
#include
#include
#include
using namespace std;
class TextEditor{
private:
     int cursor;
     int line;
     int offset;
     int total;
     int letters;
     int quots;
     int nums;
     int spaces;
     string textName;
     list article;
public:
    TextEditor();
    ~TextEditor();
    const string  & getName() const;
    void setName(const string &name);
    int  getCursor(int *,int *) const;
    int moveCursor(int);
    int setCursor(int ,int);
    int addText(const string &);
    int insertText(string);
    int findTextInAll(const string &,int *,int *);
    int deleteText(const string &);
    int deleteText(int ,int,int);
    int saveFile();
    void wordState(int *,int *,int *,int *,int *) const;
    list::iterator getIndex(int x);
    friend ostream & operator<<(ostream &,TextEditor &);
    friend istream & operator>>(istream &,TextEditor &);
};
  TextEditor:: TextEditor(){
             textName = "Untitled";
             cursor=1;
             line=1;
             offset=0;
             total =0;
             spaces =0;
             quots =0;
             letters = 0;
             nums = 0;
  }
  TextEditor::~TextEditor(){

  }
list::iterator TextEditor::getIndex(int x){
      if(x<0)x=0;
      if(x>line)x=line;
      list::iterator it;
      int l = 0;
      for(it=article.begin();it!=article.end();it++){
        l++;
        if(l==x) break;
      }
      return  it;
  }
const string & TextEditor::getName() const{
     return textName;
}
void TextEditor::setName(const string &name){
    if(name.length()!=0){
        textName=name;
    }
}
int  TextEditor::getCursor(int * pLine=NULL,int *pOffset=NULL) const{
          *pLine = cursor;
           *pOffset=offset;
           return 1;
}
int TextEditor::moveCursor(int offset){
         list::iterator it =getIndex(cursor);
         this->offset+=offset;
         while(it->length()offset&&it!=article.end()){
            this->offset-=it->length();
            cursor++;
            it++;
         }
         return 1;
}
int TextEditor::setCursor(int line ,int offset){
          if(line>article.size()) line = article.size();
          this->cursor=line;
          if(offset<0) offset=0;
          if(offset>getIndex(line)->length()) offset = getIndex(line)->length();
          this->offset=offset;
          return 1;
}
int TextEditor::addText(const string & text){
     article.push_back(text);
     total+=text.length();
     for(int i=0;i='0'&&text[i]<='9') nums++;else
        if(text[i]>='a'&&text[i]<='z') letters++;else
        if(text[i]>='A'&&text[i]<='Z') letters++;else
        quots++;
     }
     line++;
     return 1;
}
int min(int x,int y)
{
    return x='0'&&text[i]<='9') nums++;else
        if(text[i]>='a'&&text[i]<='z') letters++;else
        if(text[i]>='A'&&text[i]<='Z') letters++;else
        quots++;
     }
     if(article.empty()) {
        article.push_back(text);
        offset = text.length();
        return 1;
     }
     list::iterator it = getIndex(cursor);

     string temp=it->substr(0,offset);
     temp+=text;
     temp +=it->substr(offset);
     *it = temp;
     return 1;
}
int TextEditor::findTextInAll(const string & text ,int *line,int *offset){
       list::iterator it;
       int l=0;
       int li = *line;
       int pp = *offset;
       for(it=article.begin();it!=article.end();it++){
            l++;
         if(*line>l) continue;
         if(*linefind(text,(*offset)+1)!=string::npos){
            *line = l;
            *offset = it->find(text,(*offset)+1);
            return 1;
         }
       }
       if(li==*line&&pp==*offset) *offset=-1;
       return 1;
}

int TextEditor::deleteText(const string & text){
      int line=1,offset=-1;
      findTextInAll(text,&line,&offset);
      while(1){
        if(offset==-1)break;
        cout<<"是否删除第"<>c;
        if(c[0]!='Y'&&c[0]!='y') continue;
        deleteText(line,offset,text.length());
        findTextInAll(text,&line,&offset);
      }
}
int TextEditor::deleteText(int cursor ,int pos,int length){
         list::iterator it = getIndex(cursor);
         total-=length;
         string text = *it;
         for(int i=pos;ilength(),pos+length);i++){
            if(text[i]==' ') spaces--;else
            if(text[i]>='0'&&text[i]<='9') nums--;else
            if(text[i]>='a'&&text[i]<='z') letters--;else
            if(text[i]>='A'&&text[i]<='Z') letters--;else
               quots--;
         }
         *it = it->erase(pos,length);
}
void TextEditor::wordState(int *pTotal,int *pLetter,int *pDigit,int *pSpace,int *pQuot) const{
    *pTotal = total;
    *pLetter =letters;
    *pDigit = nums;
    *pSpace =spaces;
    *pQuot = quots;
}
ostream & operator<<(ostream & out,TextEditor & editor){
      int line = 1;
      list::iterator it;
      out<<"***********"<>(istream & in,TextEditor &editor){
    char s[100];
    getchar();
    gets(s);
    string text(s);
    editor.insertText(text);
    while(1){
        cout<<"继续输入(Y 或 N):";
        string c;
        in>>c;
        if(c[0]!='Y'&&c[0]!='y') break;
        getchar();
        gets(s);
        string ss(s);
        editor.insertText(ss);
    }
    return in;
}
int TextEditor::saveFile(){
    string name = textName+".txt";
    ofstream out(name.c_str(),ios::out);
    list::iterator it;
    for(it=article.begin();it!=article.end();it++){
        out<<*it<

源文件texteditor.cpp

#include"texteditor.h"
void showInstructions(){
  cout<<"*************简易文本编辑器***************\n";
  cout<<"选择以下数字进行文本编辑,请使用英文输入法"<>n;
            if(n==11) break;
            switch(n)
            {
                case 0:{
                    showInstructions();
                   break;
                }
                case 1:{
                    cout<<"文档名称为:"<>s;
                    editor.setName(s);
                    cout<<"设置成功"<>line;
                    cout<<"光标在这一行的位置:";
                    cin>>pos;
                    editor.setCursor(line,pos);
                    break;
                }
                case 5:{
                    cout<>editor;
                    break;
                }
                case 6:{
                    cout<<"请输入要添加的文字:"<>c;
                        if(c[0]!='Y'&&c[0]!='y') break;
                        getchar();
                        gets(s);
                        text = s;
                        editor.addText(text);
                    }
                    break;
                }
                case 7:{
                    int total,space,num,quot,letter;
                    editor.wordState(&total,&letter,&num,&space,");
                    cout<<"全文总数为:"<>as;
                    if(as==1){
                      cout<<"请输入要删除的文字:"<>line;
                       cout<<"\n输入位置:";
                       cin>>pos;
                       cout<<"\n输入长度:";
                       cin>>length;
                       editor.deleteText(line,pos,length);
                       cout<<"删除成功"<


你可能感兴趣的:(C++实现简易文本编辑器)