数据结构课程设计建文本编辑器Word下载.docx
- 文档编号:21561179
- 上传时间:2023-01-31
- 格式:DOCX
- 页数:30
- 大小:445.27KB
数据结构课程设计建文本编辑器Word下载.docx
《数据结构课程设计建文本编辑器Word下载.docx》由会员分享,可在线阅读,更多相关《数据结构课程设计建文本编辑器Word下载.docx(30页珍藏版)》请在冰豆网上搜索。
3.关闭文件
(图1)
(四)插入字符串
1.提示用户输入要插入字符串的位置(行号row,列号col)以及要插入的字符串。
2.先在行单链表中找到该行(第row行),若超出现有行数,则添加空行补齐;
3.将字符串插入该行(第row行)节点指向的列链表中的col-1位置,若超出现有节点数,则添加保存字符为空格的节点补齐。
(五)删除字符串
(1)提示用户输入要删除字符串的开始位置(行号row,列号col)以及要插入的字符串的长度len。
(2)在链表中定位到该行节点,若不存在,则提示无法删除并返回;
否则继续执行。
(3)在该行中定位到该col字符节点node,若不存在,则提示无法删除并返回;
(4)删除从node字符节点开始的len个节点。
若不满len个,则全部删除。
(六)查找替换
1.提示用户输入要查找的字符串。
2.遍历链表,找到每一个出现此字符串的位置并输出。
3.询问用户是否要进行替换。
若选是,则提示用户输入要替换后的字符串,然后先在链表中删除原字符串,再在该位置插入要替换为的字符串。
(七)行移动
1.提示用户输入要移动的行row和移动后的位置pos。
2.将行链表中的row-1节点移动到pos-1位置。
(八)列移动
1.提示用户输入要移动的列col和移动后的位置pos。
2.遍历每一行(列链表),将每一行的col-1节点移动到pos位置处;
若col-1节点不存在则不处理。
(九)显示文本
(1)遍历行、列链表,并将数据输出到控制台;
(图2)
●函数分析源程序代码
#include<
iostream>
fstream>
string>
usingnamespacestd;
//#include"
Struct.h"
//1、具有图形菜单界面;
//2、查找,替换(等长,不等长),插入(插串,文本块的插入)、块移动(行块,列块移动),删除
//3、可正确存盘、取盘;
//4、正确显示总行数。
//字符节点
structNode
{
charch;
Node*next;
};
//行节点
structRow
Node*line;
Row*next;
//创建一个Node对象
Node*createNode(charch)
Node*p=newNode;
p->
ch=ch;
next=NULL;
returnp;
}
//创建一个Row对象
Row*createRow(Node*line)
Row*p=newRow;
line=line;
//定位到index处
Node*locate(Node*line,intindex)
Node*p=line;
inti=-1;
while(p!
=NULL&
&
i<
index)
{
p=p->
next;
i++;
}
Row*locate(Row*list,intindex)
Row*p=list;
//插入节点
boolinsert(Node*list,intindex,charc)
Node*p=locate(list,index-1);
if(p)
Node*node=createNode(c);
node->
next=p->
p->
next=node;
returntrue;
returnfalse;
boolinsert(Row*list,intindex,Node*line)
Row*p=locate(list,index-1);
Row*row=createRow(line);
row->
next=row;
//删除节点
boolremove(Node*list,intindex)
Node*p=locate(list,index-1);
Node*q=NULL;
if(p&
p->
next)
q=p->
next=q->
deleteq;
//清空链表
voidclear(Node*line)
Node*p=line->
Node*q;
while(p)
q=p->
deletep;
p=q;
boolremove(Row*list,intindex)
Row*p=locate(list,index-1);
Row*q=NULL;
clear(q->
line);
//清空字符链表
deleteq->
line;
//删除字符链表表头
voidclear(Row*text)
Row*p=text->
Row*q;
clear(p->
deletep->
voidLine(inti)
if(i==1)
cout<
<
"
-----------------------------------------------------------------------"
endl;
elseif(i==2)
======================================================================="
intYesOrNo()
charc;
cin>
>
c;
if(c=='
Y'
||c=='
y'
)
return1;
elseif(c=='
N'
n'
return0;
return-1;
//主菜单
voidMenu()
cout<
**********************************************************************"
*简易文本编辑器*"
\t1.打开文件"
\t2.新建文件"
\t3.保存文件"
\t4.插入字符串"
\t5.删除字符串"
\t6.查找替换"
\t7.行移动"
\t8.列移动"
\t9.显示文本"
\t0.退出"
Line
(1);
//输出当前文本
voidPrintText(Row*text)
Line
(2);
当前文本为:
inti=0;
=NULL)
行"
"
;
line->
while(q!
{
cout<
q->
ch;
q=q->
}
总共"
行"
//打开文件
boolOpenFile(char*fileName,Row*text)
ifstreamfile(fileName,ios:
:
in);
if(!
file)
文件读取失败!
returnfalse;
Row*rp;
rp=text;
while(!
file.eof())
Node*line=newNode;
line->
Node*p=line;
file.get(c);
while(c!
=10&
!
Node*q=createNode(c);
q->
p->
next=q;
p=p->
file.get(c);
/*if(c==10)
row.add('
\n'
);
}*/
if(!
Row*rq=createRow(line);
rp->
next=rq;
rp=rp->
file.close();
文件读取完成!
returntrue;
//创建文件
boolCreateFile(constchar*fileName,Row*text)
ofstreamfile(fileName);
文件创建失败!
文件创建成功!
//保存
boolSaveFile(constchar*fileName,Row*text)
文件保存失败!
//if(q->
data=='
file<
file<
文件保存成功!
//插入字符串
voidInsertStr(Row*text,introw,intcol)
inti=-1,j=-1;
Row*rp,*rq;
Node*np,*nq;
while(rp->
next&
row)
rp=rp->
while(i<
nq=createNode('
'
//定义一个新行链表
rq=createRow(nq);
rp->
np=rp->
while(np->
j<
col-1)
np=np->
j++;
while(j<
np->
next=nq;
cin.get();
strings;
getline(cin,s);
for(intk=0;
k<
s.length();
k++)
nq=createNode(s[k]);
nq->
next=np->
//行移动
boolMoveRow(Row*text,introw,intpoz)
if(row==poz)
Row*p=locate(text,row-1);
Row*t=locate(text,poz-1);
t)
q->
next=t->
t->
//列移动
voidMoveCol(Row*text,intcol,intpoz)
if(col==poz)
return;
Row*rp=text->
while(rp)
Node*np=locate(rp->
line,col-1);
Node*nt=locate(rp->
line,poz-1);
Node*nq;
if(np&
np->
nt)
nq=np->
np->
next=nq->
nq->
next=nt->
nt->
//查找替换
voidFindStr(Row*text)
stringstr,rep;
intpos,k=0,row=1,col=0;
请输入你要查找的字符串:
str;
stringline;
Node*nq=p->
while(nq)
line.append(1,nq->
ch);
nq=nq->
pos=line.find(str.c_str(),0);
while(pos!
=string:
npos)
col=pos;
k++;
str<
第"
次出现的位置是:
第"
row<
行,第"
col+1<
列"
是否替换?
(Y/N)"
intyon=YesOrNo();
while(yon==-1)
{
cout<
yon=YesOrNo();
}
if(yon==1)
将"
替换为:
cin>
rep;
intj;
for(j=0;
str.length();
j++)
{
remove(p->
line,col);
}
rep.length();
insert(p->
line,col+j,rep[j]);
替换成功!
pos=line.find(str.c_str(),col+str.length());
row++;
if(k==0)
当前文本中找不到该字符串!
voidDelStr(Row*text,introw,intcol,intlen)
Row*rp=locate(text,row);
if(rp==NULL)
无法删除,因为该位置没有字符串!
Node*np=locate(rp->
if(np==NULL)
Node*nq=np->
intk=0;
while(nq&
len)
s.append(1,nq->
deletenq;
nq=np->
k++;
成功删除字符串"
s<
intmain()
boolflag=false;
intsel,row,col,poz;
Row*text=newRow;
text->
stringfileName;
Menu();
请选择操作:
sel;
while(sel!
=0)
switch(sel)
case1:
//打开文件
请输入要打开的文件路径(例如E:
\\1.txt):
cin>
fileName;
flag=OpenFile((char*)fileName.c_str(),text);
if(flag)
PrintText(text);
break;
case2:
//新建文件
要保存前一个文件吗?
intyon=YesOrNo();
while(yon==-1)
cout<
yon=YesOrNo();
if(yon==1)
SaveFile((char*)fileName.c_str(),text);
}
flag=false;
clear(text);
请输入要新建的文件路径(例如E:
flag=CreateFile((char*)fileName.c_str(),text);
case3:
//
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 课程设计 文本 编辑器