激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - C/C++ - C++實現簡易文本編輯器

C++實現簡易文本編輯器

2021-08-24 15:01hmy_ C/C++

這篇文章主要為大家詳細介紹了C++實現簡易文本編輯器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了C++實現文本編輯器的具體代碼,供大家參考,具體內容如下

1.簡易文本編輯器

2.用鏈表實現,保存到文件中

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#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;
    }
 
  }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/qq_36099645/article/details/53011927

延伸 · 閱讀

精彩推薦
  • C/C++C/C++經典實例之模擬計算器示例代碼

    C/C++經典實例之模擬計算器示例代碼

    最近在看到的一個需求,本以為比較簡單,但花了不少時間,所以下面這篇文章主要給大家介紹了關于C/C++經典實例之模擬計算器的相關資料,文中通過示...

    jia150610152021-06-07
  • C/C++C語言中炫酷的文件操作實例詳解

    C語言中炫酷的文件操作實例詳解

    內存中的數據都是暫時的,當程序結束時,它們都將丟失,為了永久性的保存大量的數據,C語言提供了對文件的操作,這篇文章主要給大家介紹了關于C語言中文件...

    針眼_6702022-01-24
  • C/C++C語言實現電腦關機程序

    C語言實現電腦關機程序

    這篇文章主要為大家詳細介紹了C語言實現電腦關機程序,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    xiaocaidayong8482021-08-20
  • C/C++C++之重載 重定義與重寫用法詳解

    C++之重載 重定義與重寫用法詳解

    這篇文章主要介紹了C++之重載 重定義與重寫用法詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下...

    青山的青6062022-01-04
  • C/C++詳解c語言中的 strcpy和strncpy字符串函數使用

    詳解c語言中的 strcpy和strncpy字符串函數使用

    strcpy 和strcnpy函數是字符串復制函數。接下來通過本文給大家介紹c語言中的strcpy和strncpy字符串函數使用,感興趣的朋友跟隨小編要求看看吧...

    spring-go5642021-07-02
  • C/C++c++ 單線程實現同時監聽多個端口

    c++ 單線程實現同時監聽多個端口

    這篇文章主要介紹了c++ 單線程實現同時監聽多個端口的方法,幫助大家更好的理解和學習使用c++,感興趣的朋友可以了解下...

    源之緣11542021-10-27
  • C/C++深入理解goto語句的替代實現方式分析

    深入理解goto語句的替代實現方式分析

    本篇文章是對goto語句的替代實現方式進行了詳細的分析介紹,需要的朋友參考下...

    C語言教程網7342020-12-03
  • C/C++學習C++編程的必備軟件

    學習C++編程的必備軟件

    本文給大家分享的是作者在學習使用C++進行編程的時候所用到的一些常用的軟件,這里推薦給大家...

    謝恩銘10102021-05-08
主站蜘蛛池模板: 99精品国产小情侣高潮露脸在线 | 成人午夜在线免费观看 | 狠狠撸电影 | 色域tv| 免费观看亚洲视频 | 国内精品久久久久久久久久久久 | 中文日韩欧美 | 久久久在线免费观看 | 成人黄色免费视频 | 羞羞的动漫在线观看 | 一级一级一级毛片 | 三级国产三级在线 | 综合在线视频 | 欧美成人免费看 | 国产深夜福利视频在线播放 | 免费观看一区二区三区视频 | 国产亚洲精品视频中文字幕 | 日韩黄色片网站 | 国产一级二级毛片 | 奇米影视亚洲春色 | 中文有码一区二区 | 久久久久久久久久久国产精品 | 欧美第1页 | av噜噜噜噜 | 国产91精品久久久久久久 | avav在线播放 | 日韩视频一区在线 | 欧美激情第一区 | 九九精品久久 | 人禽l交免费视频观看 视频 | 国产在线免费 | 国产精品成人免费一区久久羞羞 | 日韩精品中文字幕一区二区 | 毛毛片在线看 | 成人国产精品久久 | 欧美顶级毛片在线播放小说 | 欧美极品免费 | 999久久久国产999久久久 | 77成人影院 | 亚洲第一成网站 | 欧美国产一区二区三区激情无套 |