1、数据结构课程设计建文本编辑器重庆交通大学信息科学与工程学院数据结构课程设计报告书专 业: 计算机科学与技术 课程设计名称:数据结构课程设计题 目:简易文本编辑器班 级:姓 名: 学 号:指 导 教 师:完 成 时 间:2013年6月27日目录一. 设计的目的和要求3课程设计的目的3基本要求3二. 课程设计任务要求3需求分析3三. 具体设计3主程序流程图4详细设计功能分析4函数分析 源程序代码6四. 调试分析和测试结果20代码测试截图结果分析20五. 总结心得与体会25一课程设计目的和要求 设计目的数据结构课程设计是一门实践性非常强的课程,不但结合了C语言的设计基本知识,更加注重技能的培养,是学
2、生能够允许渐进的把握C语言C+的程序设计的技能。通过此课程设计可以提高学生的思维能力,促进学生的综合应用能力和专业素质的提高,同时提以下几个方面的能力。1. 了解并掌握数据结构与算法的设计方法,具备初步的独立分析和设计能力;2. 初步掌握软件开发过程的问题分析、系统设计、程序编码、测试等基本方法和技能;3. 提高综合运用所学的理论知识和方法独立分析和解决问题的能力;4. 训练用系统的观点和软件开发一般规范进行软件开发,培养软件工作者所应具备的科学的工作方法和作风。二课程设计任务要求 需求分析1. 具有图形菜单界面;2. 查找,替换(等长,不等长),插入(插串,文本块的插入)、块移动(行块,列块
3、移动),删除3. 可正确存盘、取盘;4. 正确显示总行数。三具体设计 主程序流程图 如(图1)所示 程序中用链表来保存文本,每一行为一个单向链表,每一行的表头保存在一个行链表中,形成了一个2维链表,结构如下(图2)所示: 详细设计功能实现(一) 打开文件1. 提示用户输入文件路径2. 判断文件路径是否有效,若无效,则提示错误信息并返回,否则继续执行。3. 若文件成功打开并且文件指针没有到文件尾,则从文件中一次读取一个字符,并将该字符添加到一列单链表节点中,直至遇到换行符(ASCII码10)。当列单链表形成后,它的首地址将被保存至行单链表的相应节点的数据域中,如此动作,直至文件指针指向文件尾部而
4、结束。(二) 新建文件1. 若行链表中有数据存在,则提示用户保存文件。2. 提示用户输入新建文件的保存路径。3. 测试新文件路径是否有效。(三) 保存文件1. 打开文件。2. 遍历行单链表,遍历行单链表节点指向的列单链表,并将数据输出到文件,每个列单链表遍历完后,输出换行符到文件。3. 关闭文件 (图1)(四) 插入字符串1. 提示用户输入要插入字符串的位置(行号row,列号col)以及要插入的字符串。2. 先在行单链表中找到该行(第row行),若超出现有行数,则添加空行补齐;3. 将字符串插入该行(第row行)节点指向的列链表中的col-1位置,若超出现有节点数,则添加保存字符为空格的节点补
5、齐。(五) 删除字符串(1) 提示用户输入要删除字符串的开始位置(行号row,列号col)以及要插入的字符串的长度len。(2) 在链表中定位到该行节点,若不存在,则提示无法删除并返回;否则继续执行。(3) 在该行中定位到该col字符节点node,若不存在,则提示无法删除并返回;否则继续执行。(4) 删除从node字符节点开始的len个节点。若不满len个,则全部删除。(六) 查找替换1. 提示用户输入要查找的字符串。2. 遍历链表,找到每一个出现此字符串的位置并输出。3. 询问用户是否要进行替换。若选是,则提示用户输入要替换后的字符串,然后先在链表中删除原字符串,再在该位置插入要替换为的字符
6、串。(七) 行移动1. 提示用户输入要移动的行row和移动后的位置pos。2. 将行链表中的row-1节点移动到pos-1位置。(八) 列移动1. 提示用户输入要移动的列col和移动后的位置pos。2. 遍历每一行(列链表),将每一行的col-1节点移动到pos位置处;若col-1节点不存在则不处理。(九) 显示文本(1)遍历行、列链表,并将数据输出到控制台; (图2) 函数分析 源程序代码#include #include #include using namespace std;/#include Struct.h/1、 具有图形菜单界面;/2、 查找,替换(等长,不等长),插入(插串,文
7、本块的插入)、块移动(行块,列块移动),删除/3、 可正确存盘、取盘;/4、正确显示总行数。/字符节点struct Node char ch; Node* next;/行节点struct Row Node* line; Row* next;/创建一个Node对象Node* createNode(char ch) Node* p = new Node; p-ch=ch; p-next=NULL; return p;/创建一个Row对象Row* createRow(Node* line) Row* p=new Row; p-line=line; p-next=NULL; return p;/定位到
8、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* list,int index,char c) Node* p= locate(list,index-1); if(p) Node* node=createNode(c); no
9、de-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;/删除节点bool remove(Node* list,int index) Node* p=locate(list,index-1); Node* q=NULL; i
10、f(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(p&p-next) q = p-next; p-next=q-next; clear(q-line); /清空字符
11、链表 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) cout-endl; else if(i=2) cout=c; if(c=Y|c=y) return 1; else if(c=N|c=n) return 0; return -
12、1;/主菜单void Menu() cout*endl; cout* 简易文本编辑器 *endl; cout*endl; coutt1.打开文件endl; coutt2.新建文件endl; coutt3.保存文件endl; coutt4.插入字符串endl; coutt5.删除字符串endl; coutt6.查找替换endl; coutt7.行移动endl; coutt8.列移动endl; coutt9.显示文本endl; coutt0.退出endl; Line(1);/输出当前文本void PrintText(Row* text) coutendl; Line(2); cout当前文本为:n
13、ext; Node* q; int i=0; while(p!=NULL) i+; cout行iline-next; while(q!=NULL) coutch; q=q-next; coutnext; Line(1); cout总共 i 行endl; Line(2);/打开文件bool OpenFile(char * fileName,Row* text) ifstream file(fileName,ios:in); if(!file) cout文件读取失败!next=NULL; Node* p=line; file.get(c); while(c!=10&!file.eof() Node
14、* q=createNode(c); q-next=NULL; p-next=q; p=p-next; file.get(c); /*if(c=10) row.add(n); */ if(!file.eof() Row* rq = createRow(line); rp-next=rq; rp=rp-next; file.close(); cout文件读取完成!endl; return true;/创建文件bool CreateFile(const char * fileName,Row* text) ofstream file(fileName); if(!file) cout文件创建失败!
15、endl; return false; file.close(); cout文件创建成功!endl; return true;/保存bool SaveFile(const char * fileName,Row* text) ofstream file(fileName); if(!file) cout文件保存失败!next; Node* q=NULL; while(p!=NULL) q = p-line-next; while(q!=NULL) /if(q-data=n) filech; q=q-next; filenext; file.close(); cout文件保存成功!next&in
16、ext; 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 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
17、=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=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
18、&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; cout请输入你要查找的字符串:str; Row* p=text-next; while(p) string line; Node* nq=p-line-next; while(nq) line.append(1,nq-ch); nq=nq-next; pos=line.find(str.c_str(),0)
19、; while(pos!=string:npos) col=pos; k+; coutstr 第k次出现的位置是:第row行,第col+1列endl; cout是否替换?(Y/N) ; int yon=YesOrNo(); while(yon=-1) cout是否替换?(Y/N) ; yon=YesOrNo(); if(yon=1) cout将 strrep; int j; for(j=0;jline,col); for(j=0;jline,col+j,repj); cout替换成功!next; if(k=0) cout当前文本中找不到该字符串!endl; void DelStr(Row* t
20、ext,int row,int col,int len) Row* rp = locate(text,row); if(rp=NULL) cout无法删除,因为该位置没有字符串!line,col-1); if(np=NULL) cout无法删除,因为该位置没有字符串!next; string s; int k=0; while(nq&kch); np-next=nq-next; delete nq; nq=np-next; k+; cout成功删除字符串 snext=NULL; string fileName; Menu(); coutsel; while(sel!=0) switch(sel
21、) case 1: /打开文件 coutfileName; flag = OpenFile(char *)fileName.c_str(),text); if(flag) PrintText(text); break; case 2: /新建文件 if(flag) cout要保存前一个文件吗?(Y/N) ; int yon=YesOrNo(); while(yon=-1) cout要保存前一个文件吗?(Y/N) ; yon=YesOrNo(); if(yon=1) SaveFile(char *)fileName.c_str(),text); flag = false; clear(text); coutfile