数据结构 C++ 文件操作 简易文本编辑器

//类的声明
#pragma once
#include
#include
using namespace std;
#include
#include
class texteditor
{
private:
list article;
int cursor;
int line;
int offset;
int total;
ofstream outf;
string name;
int* n;
public:
void getname() const;
void setname();

void getcursor(int *pline = NULL, int *poffset = NULL) const;
void movecursor(int origin);
void setcursor(int line, int offset);

void addtext(const string& text);
void inserttext(string text);

void findtext(const string& p);

void deletetext(const string& text);
void deletetext(int cursor, int length);

void wordstat();
void next(string p);
int kmp(list::iterator t, string p);
void print();
void textshow();
void editor();
};


//类的定义
#include "texteditor.h"
#include
#include
using namespace std;

void texteditor::getname()const
{
cout << name;
}
void texteditor::setname()
{
cout << "请输入文档名:";
cin >> name;
}
void texteditor::addtext(const string& text)
{
article.push_back(text);
}
void texteditor::inserttext(string text)
{
int i = 1;
list::iterator a;
for (a = article.begin(); i != line; a++, i++);
if (a->size() - 1 >= offset)
{
a->insert(offset, text);
}
else
{
a++;
article.insert(a, text);
}
}
void texteditor::movecursor(int origin)
{
line = 1;
cursor = origin;

list::iterator a;
for (a = article.begin(); a != article.end(); a++)
{
int c = a->size();
if ((cursor - c) > 0)
{
cursor = cursor - a->size();
line++;
}
else
{
offset = cursor - 1;
break;
}
}
cout << "当前光标位置: " << line << " " << offset << endl;
}
void texteditor::setcursor(int line, int offset)
{
this->line = line;
this->offset = offset;
cursor = 0;
int i = 1;
list::iterator a;
for (a = article.begin(); i != line; i++, a++)
{
cursor += a->size();
}
cursor += offset;
// cout << cursor;
}
void texteditor::findtext(const string& p)
{
line = 1;
offset = 0;
list::iterator t;

for (t = article.begin(); t != article.end(); t++)
{
int j = t->find(p, 0);
if (j != -1)
{
offset = t->find(p, 0);
cout << "查找字符串位置是: " << line << " " << offset << endl;
break;
}
line++;
}
if (t == article.end())
cout << "未查找到!!" << endl;
}
void texteditor::deletetext(int cursor, int lengt)
{
movecursor(cursor);
list::iterator t;
int i = 1;
for (t = article.begin(); i != line; i++, t++);
t->erase(offset, lengt);
}
void texteditor::deletetext(const string& text)
{
list::iterator t;
string s;
int i, u;
for (t = article.begin(); t != article.end(); t++)
{
s = s + *t;
}

i = s.find(text, 0);
if (i == -1)
{
cout << "未查找到匹配字符串,无法删除!!" << endl;
}
else
{
int lengt = text.size(

);
movecursor(i + 1);
int j = 1;
for (t = article.begin(); j != line; j++, t++);
if (t->size() - offset > lengt)
t->erase(offset, lengt);
else
{
u = t->size();
t->erase(offset, t->size() - offset);
t++;
t->erase(0, lengt + offset - u);
}
}
}
void texteditor::wordstat()
{
list::iterator a;
int i, j = 0, h = 0, g = 0, f = 0;
cout << "文档------" << "《" << name << "》" << "------内容:\n";
for (a = article.begin(); a != article.end(); a++)
{
cout << *a << "\n";
}
cout << endl;
for (a = article.begin(); a != article.end(); a++)
{
i = 0;
while ((*a)[i] != '\0')
{
if ((*a)[i] >= 65 && (*a)[i]<91 || (*a)[i] >= 97 && (*a)[i] < 123)
j++;
else if ((*a)[i]>48 && (*a)[i]<57)
h++;
else if ((*a)[i] == ' ')
g++;
else
f++;
i++;
}
}

cout << "字母个数为: " << j << " 数字个数为: " << h << " 空格个数为: " << g << "\n 其他符号个数为: " << f << " 总字符个数: " << j + h + g + f << endl;
}
void texteditor::textshow()
{
list::iterator a;
ofstream outf("data.txt");
outf << "《" << name<< "》" << endl;
for (a = article.begin(); a != article.end(); a++)
{
outf << *a << "\n";
}
}
void texteditor::print()
{
cout << " %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
cout << " %%%%欢迎使用简易文本编辑器%%%%\n";
cout << " %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
cout << " %% 1、读取文件 %%\n";
cout << " %% 2、插入文本 %%\n";
cout << " %% 3、查找文本 %%\n";
cout << " %% 4、删除文本 %%\n";
cout << " %% 5、移动光标 %%\n";
cout << " %% 6、输出文本 %%\n";
//cout << " %% 7、写入文件 %%\n";
cout << " %% 7、退出 %%\n";
cout << " %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
cout << " 请选择。。。";
}
void texteditor::editor()
{
string text;
int i, j, h;
setname();
print();
cin >> i;
ifstream inf("data.txt");

system("cls");
if (!inf)
{
cout << "can not open";
exit(0);
}
while (i != 7)
{
switch (i)
{
case 1:

//cout << "请输入文本行数:"
/*cout << "请输入文本:";

cin >> text;*/
;
while (!inf.eof())
{
inf >> text;
addtext(text);
}
cout << "读取成功!!" << endl;
textshow();
print();
cin >> i;

system("cls");
break;
case 2:
cout << " 输入插入文本:";
cin >> text;
cout << "输入要插入的位置:";
cin >> j >> h;
setcursor(j, h);
//a.movecursor(j);
//cout << j;
inserttext(text);
textshow();
print();
cin >> i;
system("cls");
break;
case 3:
cout << "请输入要查找的字符串:";
cin >> text;
findtext(text);
print();
cin >> i;
system("cls");
break;

case 4:
cout << "输入要删除的字符串:";
cin >> text;
deletetext(text);
textshow();
print();
cin >> i;
system("cls");
break;
case 5:
cout << "输入光标要移动的位置:";
cin >> j;
movecursor(j);
print();
cin >> i;
system("cls");
break;
case 6:
wordstat();
print();
cin >> i;
system("cls");
break;
/*case 7:
a.textshow();
a.print();
cin >> i;
system("cls");
break;*/
}
}
}


//主函数
#include "texteditor.h"
#include
#include
#include
using namespace std;
//#include "stdafx.h"

int main()
{
texteditor a;
a.editor();

cout << "程序结束!!";
return 0;
}

相关文档
最新文档