当前位置:文档之家› 数据结构第二次实验报告

数据结构第二次实验报告

数据结构与算法分析课程设计报告课题名称: A Text Editor Imlementation提交文档学生姓名:苟丁提交文档学生学号: 0843042229 同组成员名单:无指导教师姓名:孙界平指导教师评阅成绩:指导教师评阅意见:..提交报告时间:2010 年 5 月 7 日1. 实验题目:带括号的算术表达式求值2. 实验的目的和要求:1.采用C++的ASCII码文件和串函数实现;2.熟练掌握串运算的应用;3.熟练掌握计算机系统的基本操作方法,了解如何编辑、编译、链接和运行一个C++程序;4.上机调试程序,掌握查错、排错使程序能正确运行3.实验的环境:1、硬件环境:联想笔记本电脑,Intel(R) Pentium(R) Dual T3400 ,2GB内存2、软件环境:Windows XP 下的Microsoft Visual Studio 20084.算法描述:具体操作与函数描述(1)编辑一个文本文件,命名为text.txt.(2)函数run()提供给用户选择符的输入:w,r,I,d,f,c,q,h,n,p,b,e,g,v.用户可以选择H选择符寻求帮助,得知操作符分别代表的动作。

(3) R代表函数Read()将文本读入缓冲区,缓冲区以前的任何内容都将将消失。

(4) W代表函数Write()将缓冲区的内容写入文本文件。

(5) I代表函数Insert()插入新行,用户可以在适当的提示下键入新行并提供新行。

(6) D代表delete()行数所执行的删除操作,可以删除当前行,并进入下一行。

(7) F代表函数findChar(),用于查找目标行。

(8) C代表函数changLine(),将用户请求的字符串修改成用户请求的替换文本,可选择的是仅在当前行中有效。

(9) Q代表函数quit(),用户执行此命令可以退出编辑。

(10)N代表函数next(),用户可以从当前行移到下一行。

(11)P代表函数pre(),用户可以从当前行移到下一行。

(12)E代表end(),可以移到最后一行。

(13)G代表go(),用户可以指定到选择的行。

(14)V查看缓冲区的全部内测试程序说明:测试main()如下,主要定义了一行的长度,主要是执行run()函数,实现用户输入字符选择执行操作的目的。

#include"Utility.h"#include"MyList.h"int main(){const int LG=50;void run(char);char inputCh=NULL;Temp:cout<<"输入目标文本名字:";cin>>inputname;cout<<"输出文本名字:";cin>>outputname;ifstream ipf;ipf.open(inputname,ios::in);if(ipf.fail()){cout<<"不能打开文件!!\n";goto Temp;return 0;}char ch[LG];while (!ipf.eof()){ipf.getline(ch,LG-1);List.append(ch);}ipf.close();countL=List.length();display();cout<<"你要进行什么操作(输入h 获得帮助):";char chr=getchar();do{chr=getchar();if(chr=='\n') continue;run();if(toupper(chr)!='E') cout<<"你要进行什么操作(输入h 获得帮助):";}while((toupper(chr)!='E'));}流程图如下:5.源程序清单:MyLink.h#ifndef MYLINK_H#define MYLINK_H#include<iostream>#include<string>using namespace std;template<class Elem>class MyLink{public:MyLink(string LString,MyLink<Elem>*prv,MyLink<Elem>*);MyLink();void display();int findChar(char*);void repSubChar(char*,char*);string LiString;MyLink* prv;MyLink* next;};template<class Elem>MyLink<Elem>::MyLink(string LString,MyLink<Elem>*pr,MyLink<Elem>*ne){ this->LiString=LString;this->prv=pr;this->next=ne;}template<class Elem>MyLink<Elem>::MyLink(){this->prv=NULL;this->next=NULL;}template<class Elem>int MyLink<Elem>::findChar(char* fin){return LiString.find(fin);}template<class Elem>void MyLink<Elem>::repSubChar(char* old,char* rep){int temp=LiString.find(old);LiString.replace(temp,sizeof(old)-1,rep);}#endifMyList.h#ifndef LinkList_H#define LinkList_H#include"Link.h"#include<iostream>using namespace std;template<class Elem> class MyList{private:MyLink<Elem>*head;MyLink<Elem>*tail;int count;void init(){fence=tail=head=new MyLink<Elem>;count=0;}void removeall(){while (head!=NULL){fence=head;head= head->next;delete fence;}}public:MyList(){fence=tail=head=new MyLink<Elem>;count=0;};~MyList(){while (head!=NULL){fence=head;head= head->next;delete fence;};void clear(){removeall();init();};bool insert(const Elem&);bool append(const Elem&);bool replace(const Elem&);bool remove();void setStart(){fence=head;}void prev();void next(){if (fence!=tail) fence=fence->next;}int length() const{return count;}MyLink<Elem>*setPos(int &pos);void print();void output(char*);MyLink<Elem>*fence;};template<class Elem>bool MyList<Elem>::insert(const Elem&item){fence->next=new MyLink<Elem>(item,fence,fence->next);if (fence->next->next->prv!=NULL){fence->next->next->prv=fence->next;}if (tail==fence){tail=fence->next;}return true;}template<class Elem>bool MyList<Elem>::append(const Elem&item){tail=tail->next=new MyLink<Elem>(item,tail,NULL); count++;return true;}template<class Elem>bool MyList<Elem>::replace(const Elem&item){fence->LiString=item;return true;}template<class Elem>bool MyList<Elem>::remove(){fence=fence->prv;MyLink<Elem>*ltemp=fence->next;if (ltemp->next!=NULL){ltemp->next->prv=fence;}else tail=fence;fence->next=ltemp->next;delete ltemp;count--;return true;}template<class Elem>MyLink<Elem>* MyList<Elem>::setPos(int &pos){ if(count==0) cout<<"缓冲区为空!!\n";if (pos<=0||pos>count){cout<<"输入行号错误!!\n";cout<<"你想到哪一行-"<<countL<<":";int tempin=0;char ch=getchar();ch=getchar();while(!isdigit(ch)){while (ch!='\n'){ch=getchar();}cout<<"请输入数字!!\n";cout<<"你想到哪一行-"<<countL<<":";ch=getchar();}cin.putback(ch);cin>>pos;}MyList::setStart();for (int i=0;i<pos;i++){fence=fence->next;}return fence;}template<class Elem>void MyList<Elem>::print(){if (cout==0){cout<<"The buffer is empty;\n";}MyList::setStart();do{fence=fence->next;cout<<fence->LiString<<endl;}while(fence!=tail);}template<class Elem>void MyList<Elem>::output(char* outputname){ofstream opf;opf.open(outputname,ios::out);MyList::setStart();do{fence=fence->next;opf<<fence->LiString<<endl;//fence=fence->next;}while(fence->next!=tail);fence=fence->next;opf<<fence->LiString;}#endifUtility.h#include<string.h>//standard string operations#include<iostream>//standard iostream operations#include<limits.h>//numeric limits#include<math.h>//mathematical functions#include<fstream>//file input and output#include<ctype.h>//character classification#include<time.h>//date and time function#include<conio.h>//con input and outputenum Error_code{success,fail,underflow,overflow};//enum bool{false,true}; Editor.cpp#include"Utility.h"#include"MyList.h"using namespace std;MyList<string> List;void goWhichLine(int&);void display();void delLine();void insertLine();void repLine();void findChar(char*);void repChar(char*,char*);void count();void output(char*);int tempLine=0;int countL=0;char inputname[20],outputname[20];int main(){const int LG=50;void whatToDo(char);char inputCh=NULL;Temp:cout<<"输入目标文本名字:";cin>>inputname;cout<<"输出文本名字:";cin>>outputname;ifstream ipf;ipf.open(inputname,ios::in);if(ipf.fail()){cout<<"不能打开文件!!\n";goto Temp;return 0;}char ch[LG];while (!ipf.eof()){ipf.getline(ch,LG-1);List.append(ch);}ipf.close();countL=List.length();display();cout<<"你要进行什么操作(输入h 获得帮助):";char chr=getchar();do{chr=getchar();if(chr=='\n') continue;whatToDo(toupper(chr));if(toupper(chr)!='E') cout<<"你要进行什么操作(输入h 获得帮助):";}while((toupper(chr)!='E'));}void whatToDo(char ch){switch(ch){case'H':cout<<endl<<"功能符号表:"<<endl;cout<<"输入G or g-----选择行数"<<endl;cout<<"输入B or b-----显示缓冲区中文件"<<endl;cout<<"输入D or d-----删除某一行"<<endl;cout<<"输入I or i-----插入某一行"<<endl;cout<<"输入R or r-----替换某一行"<<endl;cout<<"输入C or c-----显示缓冲区中文件行数和字符数"<<endl;cout<<"输入F or f-----查找字符串"<<endl;cout<<"输入P or p-----替换某个字符串"<<endl;cout<<"输入S or s-----将缓冲区中文件保存到源文件"<<endl;cout<<"输入O or o------输出文件"<<endl;cout<<"输入E or e-----推出程序。

相关主题