欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++實(shí)現(xiàn)簡易文本編輯器

 更新時(shí)間:2020年03月13日 07:44:29   作者:hmy_  
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)簡易文本編輯器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C++實(shí)現(xiàn)文本編輯器的具體代碼,供大家參考,具體內(nèi)容如下

1.簡易文本編輯器

2.用鏈表實(shí)現(xiàn),保存到文件中

#include<iostream>
#include<string>
#include<cstdlib>
#include<ctype.h>
#include<cstdio>
#include<fstream>
using namespace std;
int NumberCount=0;//數(shù)字個(gè)數(shù)
int CharCount=0;//字母個(gè)數(shù)
int PunctuationCount=0;//標(biāo)點(diǎn)符號(hào)個(gè)數(shù)
int BlankCount=0;//空白符個(gè)數(shù)
 
class Node
{
public:
  string character;
  int cursor;
  int offset;
  Node* next;
  Node(){
    cursor=0;//每行的光標(biāo)初始位置
    offset=0;//每行的初始偏移位置
    next=NULL;
  }
};
 
class TextEditor
{
private:
  Node* head;
  string name;
  int line;//可更改的行數(shù)
  int length;//行數(shù)
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<<"輸入錯(cuò)誤!"<<endl;
    exit(0);
  }
  else{
    while(p->next!=NULL&&i<line){
      p=p->next;
      i++;
    }
  }
  if(offset>p->character.length()){
    cout<<"移動(dòng)位置太大!"<<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<<"輸入錯(cuò)誤!"<<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;//光標(biāo)清零
}
 
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<<"未出現(xiàn)該字符串!"<<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.設(shè)置文檔名字"<<endl;
  cout<<"3.獲取文檔名字"<<endl;
  cout<<"4.顯示光標(biāo)位置"<<endl;
  cout<<"5.設(shè)置光標(biāo)位置,在光標(biāo)位置處插入文本"<<endl;
  cout<<"6.在文檔中查找文本"<<endl;
  cout<<"7.在文檔中刪除文本"<<endl;
  cout<<"8.統(tǒng)計(jì)字母、數(shù)字、標(biāo)點(diǎn)符號(hào)、空白符號(hào)及總字符個(gè)數(shù)"<<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<<"光標(biāo)在第"<<text.GetLine()<<"行,第"<<text.GetCursor()<<"個(gè)字符前!"<<endl; break;
      case 5:{
        cout<<"輸入行數(shù):";
        cin>>i;
        cout<<"光標(biāo)在第"<<text.GetCursor()<<"個(gè)字符前!"<<endl;
        cout<<"輸入移動(dòng)位數(shù):";
        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<<"所查找文本首次出現(xiàn)在:"<<text.GetLine()<<"行,第"<<k<<"個(gè)字符處!"<<endl;
          break;
      }
      case 7: cout<<"輸入要?jiǎng)h除的字符串:"; getline(cin,s); text.DeleteText(s); break;
      case 8: {
        text.Count();
        cout<<"文檔中共有:"<<endl;
        cout<<NumberCount<<"個(gè)數(shù)字"<<endl;
        cout<<CharCount<<"個(gè)字母"<<endl;
        cout<<PunctuationCount<<"個(gè)標(biāo)點(diǎn)符號(hào)"<<endl;
        cout<<BlankCount<<"個(gè)空白字符"<<endl;
        cout<<"共有"<<NumberCount+CharCount+PunctuationCount+BlankCount<<"個(gè)字符!"<<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<<"輸入錯(cuò)誤,請重新輸入!"<<endl; break;
    }
 
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C語言中求字符串長度的函數(shù)的幾種實(shí)現(xiàn)方法

    C語言中求字符串長度的函數(shù)的幾種實(shí)現(xiàn)方法

    這篇文章主要介紹了C語言中求字符串長度的函數(shù)的幾種實(shí)現(xiàn)方法,需要的朋友可以參考下
    2018-08-08
  • C++?Qt開發(fā)之關(guān)聯(lián)容器類使用方法詳解

    C++?Qt開發(fā)之關(guān)聯(lián)容器類使用方法詳解

    當(dāng)我們談?wù)摼幊讨械臄?shù)據(jù)結(jié)構(gòu)時(shí),順序容器是不可忽視的一個(gè)重要概念,Qt?中提供了豐富的容器類,用于方便地管理和操作數(shù)據(jù),本章我們將主要學(xué)習(xí)關(guān)聯(lián)容器,主要包括?QMap?,QSet和?QHash,感興趣的朋友跟著小編一起來學(xué)習(xí)吧
    2023-12-12
  • C++ vector的介紹及常見功能實(shí)現(xiàn)

    C++ vector的介紹及常見功能實(shí)現(xiàn)

    這篇文章主要介紹了C++ vector的介紹及模擬實(shí)現(xiàn),vector在實(shí)際中非常的重要,但在實(shí)際中我們只要熟悉常見的接口就可以了,最重要的是理解他的底層原理,要能夠自己模擬實(shí)現(xiàn)出一個(gè)簡單的vector,本文結(jié)合示例代碼給大家詳細(xì)介紹,需要的朋友可以參考下
    2023-05-05
  • C語言函數(shù)棧幀解析

    C語言函數(shù)棧幀解析

    下面小編就為大家?guī)硪黄獪\談C語言函數(shù)調(diào)用參數(shù)壓棧的相關(guān)問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2021-09-09
  • c++中將二維數(shù)組元素變換為逆向存放的實(shí)現(xiàn)代碼

    c++中將二維數(shù)組元素變換為逆向存放的實(shí)現(xiàn)代碼

    編程將一個(gè)二維數(shù)組元素變換為逆向存放,即按元素在內(nèi)存中的物理排列位置,第一個(gè)元素變成倒數(shù)第一個(gè)元素,第二個(gè)元素變成倒數(shù)第二個(gè)元素,依此類推
    2020-11-11
  • C語言函數(shù)棧幀的創(chuàng)建與銷毀詳解

    C語言函數(shù)棧幀的創(chuàng)建與銷毀詳解

    這篇文章主要為大家詳細(xì)介紹了C語言函數(shù)棧幀的創(chuàng)建與銷毀,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • C語言基礎(chǔ)之malloc和free函數(shù)詳解

    C語言基礎(chǔ)之malloc和free函數(shù)詳解

    這篇文章主要介紹了C語言基礎(chǔ)之malloc和free函數(shù)詳解的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • c++實(shí)現(xiàn)二路歸并排序的示例代碼

    c++實(shí)現(xiàn)二路歸并排序的示例代碼

    這篇文章主要介紹了c++實(shí)現(xiàn)二路歸并排序的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • C++中設(shè)計(jì)一個(gè)類時(shí)的注意事項(xiàng)分享

    C++中設(shè)計(jì)一個(gè)類時(shí)的注意事項(xiàng)分享

    這篇文章主要來和大家分享一下C++中,設(shè)計(jì)一個(gè)類要注意哪些東西,這往往也是C++面試時(shí)會(huì)考到的問題,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-06-06
  • C++ Boost PointerContainer智能指針詳解

    C++ Boost PointerContainer智能指針詳解

    智能指針是一種像指針的C++對象,但它能夠在對象不使用的時(shí)候自己銷毀掉。雖然STL提供了auto_ptr,但是由于不能同容器一起使用(不支持拷貝和賦值操作),因此很少有人使用。它是Boost各組件中,應(yīng)用最為廣泛的一個(gè)
    2022-11-11

最新評(píng)論