最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

1.簡(jiǎn)易文本編輯器

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;//字母?jìng)€(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<<"請(qǐng)輸入:";
    cin>>n;
    getchar();
    switch(n){
      case 1: cout<<"請(qǐng)輸入字符:"; getline(cin,s,'\n'); text.AddText(s); break;
      case 2: cout<<"請(qǐng)輸入文檔名字:"; 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ò)誤,請(qǐng)重新輸入!"<<endl; break;
    }
 
  }
}

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

相關(guān)文章

  • c++重載運(yùn)算符時(shí)返回值為類的對(duì)象或者返回對(duì)象的引用問(wèn)題

    c++重載運(yùn)算符時(shí)返回值為類的對(duì)象或者返回對(duì)象的引用問(wèn)題

    這篇文章主要介紹了c++重載運(yùn)算符時(shí)返回值為類的對(duì)象或者返回對(duì)象的引用問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • c++實(shí)現(xiàn)md5加密的代碼

    c++實(shí)現(xiàn)md5加密的代碼

    這篇文章主要介紹了c++實(shí)現(xiàn)md5加密的實(shí)例代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • C/C++ 監(jiān)控磁盤(pán)與目錄操作的示例

    C/C++ 監(jiān)控磁盤(pán)與目錄操作的示例

    這篇文章主要介紹了C/C++ 監(jiān)控磁盤(pán)與目錄操作的示例,幫助大家更好的理解和學(xué)習(xí)C/C++編程,感興趣的朋友可以了解下
    2020-10-10
  • C++代碼實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)

    C++代碼實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C++代碼實(shí)現(xiàn)學(xué)生信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • C語(yǔ)言中`||`的短路機(jī)制詳解

    C語(yǔ)言中`||`的短路機(jī)制詳解

    在C語(yǔ)言中,邏輯或運(yùn)算符(||)是一種常用的邏輯運(yùn)算符,用于組合多個(gè)條件表達(dá)式,C語(yǔ)言中的邏輯或運(yùn)算符具有短路機(jī)制,這是一種非常重要的概念,本文將深入解釋C語(yǔ)言中的||短路機(jī)制以及其在編程中的應(yīng)用,感興趣的朋友跟隨小編一起看看吧
    2024-01-01
  • C語(yǔ)言設(shè)計(jì)三子棋小游戲

    C語(yǔ)言設(shè)計(jì)三子棋小游戲

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言設(shè)計(jì)三子棋小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • C語(yǔ)言?超詳細(xì)梳理總結(jié)動(dòng)態(tài)內(nèi)存管理

    C語(yǔ)言?超詳細(xì)梳理總結(jié)動(dòng)態(tài)內(nèi)存管理

    動(dòng)態(tài)內(nèi)存是相對(duì)靜態(tài)內(nèi)存而言的。所謂動(dòng)態(tài)和靜態(tài)就是指內(nèi)存的分配方式。動(dòng)態(tài)內(nèi)存是指在堆上分配的內(nèi)存,而靜態(tài)內(nèi)存是指在棧上分配的內(nèi)存,本文帶你深入探究C語(yǔ)言中動(dòng)態(tài)內(nèi)存的管理
    2022-03-03
  • C++使用yaml-cpp庫(kù)操作YAML的示例代碼

    C++使用yaml-cpp庫(kù)操作YAML的示例代碼

    配置文件有利于我們靈活配置工程,解決大量重復(fù)勞動(dòng),也方便調(diào)試,YAML?是一種人類可讀的數(shù)據(jù)序列化格式,它使用縮進(jìn)和特定的符號(hào)來(lái)表示數(shù)據(jù)結(jié)構(gòu),在本文中,我們將詳細(xì)介紹如何在?C++?中使用?yaml-cpp?庫(kù)來(lái)解析和生成?YAML?格式的數(shù)據(jù),需要的朋友可以參考下
    2024-10-10
  • 老程序員教你一天時(shí)間完成C語(yǔ)言掃雷游戲

    老程序員教你一天時(shí)間完成C語(yǔ)言掃雷游戲

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)掃雷游戲初級(jí)版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • C++實(shí)現(xiàn)字符串轉(zhuǎn)整數(shù)(atoi)的代碼詳解

    C++實(shí)現(xiàn)字符串轉(zhuǎn)整數(shù)(atoi)的代碼詳解

    在編程中,經(jīng)常會(huì)遇到將字符串轉(zhuǎn)換為整數(shù)的需求,就像標(biāo)準(zhǔn)庫(kù)中的 atoi 函數(shù)一樣,本文給大家介紹了C++中字符串轉(zhuǎn)整數(shù)(atoi)的實(shí)現(xiàn)與解析,并有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下
    2025-04-04

最新評(píng)論

辛集市| 青海省| 菏泽市| 新密市| 南宁市| 清水县| 齐齐哈尔市| 静宁县| 六盘水市| 磴口县| 英德市| 盐津县| 稻城县| 青铜峡市| 丰台区| 嘉荫县| 临城县| 刚察县| 左贡县| 梧州市| 邵阳市| 南丹县| 应城市| 安达市| 苗栗县| 巴青县| 区。| 慈溪市| 三都| 西宁市| 云和县| 和龙市| 银川市| 滨海县| 旺苍县| 太谷县| 嘉祥县| 舞钢市| 滦平县| 隆化县| 富锦市|