简单文本编辑器

上传人:飞*** 文档编号:30716214 上传时间:2018-01-31 格式:DOCX 页数:17 大小:42.01KB
返回 下载 相关 举报
简单文本编辑器_第1页
第1页 / 共17页
简单文本编辑器_第2页
第2页 / 共17页
简单文本编辑器_第3页
第3页 / 共17页
简单文本编辑器_第4页
第4页 / 共17页
简单文本编辑器_第5页
第5页 / 共17页
点击查看更多>>
资源描述

《简单文本编辑器》由会员分享,可在线阅读,更多相关《简单文本编辑器(17页珍藏版)》请在金锄头文库上搜索。

1、简单文本编辑器()一、功能要求1、 具有图形菜单界面;2、 查找,替换(等长,不等长) ,插入(插串,文本块的插入) 、块移动(行块,列块移动) ,删除3、 可正确存盘、取盘;4、正确显示总行数。二、主程序流程图三、数据结构程序中用链表来保存文本,每一行为一个单向链表,每一行的表头保存在一个行链表中,形成了一个 2 维链表,结构如下图所示:行 链 表 表 头Node* lineRow* nextNode* lineRow* nextNode* lineRow* nextNode* lineRow* nextNode* lineRow* nextchar chNode* nextchar chN

2、ode* nextchar chNode* nextchar chNode* nextchar chNode* nextchar chNode* nextchar chNode* nextchar chNode* nextchar chNode* next行 2行 1行 3行 4列 链 表 表 头 列 1 列 2 列 3四、功能实现1. 打开文件(1 ) 提示用户输入文件路径(2 ) 判断文件路径是否有效,若无效,则提示错误信息并返回,否则继续执行。(3 ) 若文件成功打开并且文件指针没有到文件尾,则从文件中一次读取一个字符,并将该字符添加到一列单链表节点中,直至遇到换行符(ASCII 码 1

3、0) 。当列单链表形成后,它的首地址将被保存至行单链表的相应节点的数据域中,如此动作,直至文件指针指向文件尾部而结束。2. 新建文件(1 ) 若行链表中有数据存在,则提示用户保存文件。(2 ) 提示用户输入新建文件的保存路径。(3 ) 测试新文件路径是否有效。3. 保存文件(1 ) 打开文件。(2 ) 遍历行单链表,遍历行单链表节点指向的列单链表,并将数据输出到文件,每个列单链表遍历完后,输出换行符到文件。(3 ) 关闭文件4. 插入字符串(1 ) 提示用户输入要插入字符串的位置(行号 row,列号 col)以及要插入的字符串。(2 ) 先在行单链表中找到该行(第 row 行),若超出现有行数

4、,则添加空行补齐;(3 ) 将字符串插入该行(第 row 行)节点指向的列链表中的 col-1 位置,若超出现有节点数,则添加保存字符为空格的节点补齐。5. 删除字符串(1 ) 提示用户输入要删除字符串的开始位置(行号 row,列号 col)以及要插入的字符串的长度 len。(2 ) 在链表中定位到该行节点,若不存在,则提示无法删除并返回;否则继续执行。(3 ) 在该行中定位到该 col 字符节点 node,若不存在,则提示无法删除并返回;否则继续执行。(4 ) 删除从 node 字符节点开始的 len 个节点。若不满 len 个,则全部删除。6. 查找替换(1 ) 提示用户输入要查找的字符串

5、。(2 ) 遍历链表,找到每一个出现此字符串的位置并输出。(3 ) 询问用户是否要进行替换。若选是,则提示用户输入要替换后的字符串,然后先在链表中删除原字符串,再在该位置插入要替换为的字符串。7. 行移动(1 ) 提示用户输入要移动的行 row 和移动后的位置 pos。(2 ) 将行链表中的 row-1 节点移动到 pos-1 位置。8. 列移动(1 ) 提示用户输入要移动的列 col 和移动后的位置 pos。(2 ) 遍历每一行(列链表) ,将每一行的 col-1 节点移动到 pos 位置处;若 col-1 节点不存在则不处理。9. 显示文本(1 ) 遍历行、列链表,并将数据输出到控制台;五

6、、代码实现#include #include #include using namespace std;/author: yyc /1、 具有图形菜单界面;/2、 查找,替换(等长,不等长) ,插入(插串,文本块的插入) 、块移动(行块,列块移动) ,删除/3、 可正确存盘、取盘;/4、正确显示总行数。/字符节点struct Nodechar ch;Node* next;/行节点struct RowNode* line;Row* next;/创建一个 Node 对象Node* createNode(char ch)Node* p = new Node;p-ch=ch;p-next=NULL;r

7、eturn p;/创建一个 Row 对象Row* createRow(Node* line)Row* p=new Row;p-line=line;p-next=NULL;return p;/定位到 index 处Node* locate(Node* line,int index)Node* p=line;int i=-1;while(p!=NULL&inext;i+;return p;Row* locate(Row* list,int index)Row* p=list;int i=-1;while(p!=NULL&inext;i+;return p;/插入节点bool insert(Node

8、* list,int index,char c)Node* p= locate(list,index-1);if(p)Node* node=createNode(c);node-next=p-next;p-next=node;return true;return false;bool insert(Row* list,int index,Node* line)Row* p= locate(list,index-1);if(p)Row* row=createRow(line);row-next=p-next;p-next=row;return true;return false;/删除节点boo

9、l remove(Node* list,int index)Node* p=locate(list,index-1);Node* q=NULL;if(p&p-next)q = p-next;p-next=q-next;delete q;return true;return false;/清空链表void clear(Node* line)Node* p=line-next;Node* q;while(p)q=p-next;delete p;p=q;bool remove(Row* list,int index)Row* p=locate(list,index-1);Row* q=NULL;if

10、(p&p-next)q = p-next;p-next=q-next;clear(q-line); /清空字符链表delete q-line; /删除字符链表表头delete q;return true;return false;void clear(Row* text)Row* p=text-next;Row* q;while(p)q=p-next;clear(p-line);delete p-line; /删除字符链表表头delete p;p=q;void Line(int i)if(i=1)coutc;if(c=Y|c=y)return 1;else if(c=N|c=n)return

11、0;return -1;/主菜单void Menu()coutnext;Node* q;int i=0;while(p!=NULL)i+;coutline-next;while(q!=NULL)coutch;q=q-next;coutnext;Line(1);coutnext=NULL;Node* p=line;file.get(c);while(c!=10&!file.eof()Node* q=createNode(c);q-next=NULL;p-next=q;p=p-next;file.get(c);if(!file.eof()Row* rq = createRow(line);rp-n

12、ext=rq;rp=rp-next;file.close();coutnext;Node* q=NULL;while(p!=NULL)q = p-line-next;while(q!=NULL)/if(q-data=n)filech;q=q-next;filenext;file.close();coutnext&inext;i+;while(inext=rq;rp=rp-next;i+;np=rp-line;while(np-next&jnext;j+;while(jnext=nq;np=np-next;j+;cin.get();string s;getline(cin,s);for(int

13、k=0;knext=np-next;np-next=nq;np=np-next;/行移动bool MoveRow(Row* text,int row,int poz)if(row=poz)return true;Row* p=locate(text,row-1);Row* t=locate(text,poz-1);Row* q;if(p&p-next&t)q=p-next;p-next=q-next;q-next=t-next;t-next=q;return true;return false;/列移动void MoveCol(Row* text,int col,int poz)if(col=

14、poz)return;Row* rp=text-next;while(rp)Node* np=locate(rp-line,col-1);Node* nt=locate(rp-line,poz-1);Node* nq;if(np&np-next&nt)nq=np-next;np-next=nq-next;nq-next=nt-next;nt-next=nq;rp=rp-next;/查找替换void FindStr(Row* text)string str,rep;int pos,k=0,row=1,col=0;coutstr;Row* p=text-next;while(p)string li

15、ne;Node* nq=p-line-next;while(nq)line.append(1,nq-ch);nq=nq-next;pos=line.find(str.c_str(),0);while(pos!=string:npos)col=pos;k+;coutrep;int j;for(j=0;jline,col);for(j=0;jline,col+j,repj);coutnext;if(k=0)coutline,col-1);if(np=NULL)coutnext;string s;int k=0;while(nq&kch);np-next=nq-next;delete nq;nq=np-next;k+;coutnext=NULL;string fileName;Menu();coutsel;while(sel!=0)switch(sel)case 1: /打开文件coutfileName;flag = OpenFile(char *)fileName.c_str(),text);if(flag)PrintText(text);break;case 2: /

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 其它办公文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号