数据结构 C++ 简单英汉字典 实验报告_第1页
数据结构 C++ 简单英汉字典 实验报告_第2页
数据结构 C++ 简单英汉字典 实验报告_第3页
数据结构 C++ 简单英汉字典 实验报告_第4页
数据结构 C++ 简单英汉字典 实验报告_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实验报告:简单英汉字典2011-12-22实现目标及要求:1)利用散列表实现英汉字典;2) 实现散列表类,要求实现一种散列表:散列函数选取建议:将单词转成整数,再用除留余数法获得散列地址。冲突解决方法可采用开散列法。3)实现字典类,字典数据存放在内存,字典类中有两个重要数据成员:字典数组:存放字典数据的数组(或线性表)。散列表对象:作为字典数据索引的散列表。散列表元素结构包含单词和对应字典数据在字典数组中的下标,可通过散列表直接获得单词在字典数组的下标。4) 利用上面两个类实现英汉字典。5)界面要求:键盘输入单词,屏幕输出解释:界面菜单项:1.添加新词2.删除单词3.查字典查字典界面:请输入英

2、文单词:China中文翻译:中国实验环境与工具:使用Microsoft Visual Studio 2010 在Windows7 64位环境下进行实验。实验思路:分如下N步:1. 先实现链结点类的结构。2. 实现字典类的结构。3. 实现词条类的结构。4. 实现词条的散列表分类方法。5. 整合功能。主要的数据结构如下:/Hash_head.h#include#include#includeusing namespace std;const int defaultDicSize=10;const int defaultTableSize=10;class ChainNodepublic:Entry

3、 EntryLink;ChainNode *link;templateclass Dictionarypublic:Dictionary(E e);Dictionary()delete ht;bool search(string,string &);bool search(string,string &,ChainNode *&);bool insert(E);bool insert(string,string);bool remove(string);void dicPos(E e);void outputDic();private:ChainNode *ht;int dicSize;/总词

4、条数int tableSize;/每个索引容量;templateDictionary:Dictionary(E e)/计算词条数,作为字典最大长度dicSize=0;while(edicSize.chinese!=)dicSize+;/初始化字典ht=new ChainNodedicSize;for(int i=0;idicSize;i+)hti.link=NULL;hti.EntryLink=Entry();templatevoid Dictionary:dicPos(E e)int i;ChainNode *p;for(i=0;idicSize;i+)insert(ei);template

5、bool Dictionary:insert(E ent)int k;ChainNode *p1,*p2;tableSize=5;k=ent.firstLetter()%tableSize;p1=&htk;if(p1-link!=NULL)p1=p1-link;while(p1-link!=NULL)p1=p1-link;p2=new ChainNode;p2-EntryLink=ent;p2-link=NULL;p1-link=p2;return 0;templatebool Dictionary:insert(string str1,string str2)Entry *e=new Ent

6、ry (str1,str2);insert(*e);return 1;templatevoid Dictionary:outputDic()int i;ChainNode *p1;for(i=0;itableSize;i+)/cout*endl;/coutiendl;/cout*link!=NULL)p1=p1-link;p1-EntryLink.output();/cout*endl;templatebool Dictionary:remove(string str)string result;ChainNode *ptr;if(search(str,result,ptr)=1)ptr-li

7、nk=ptr-link-link;return 1;elsereturn 0; templatebool Dictionary:search(string str,string &result)ChainNode *ptr;return search(str,result,ptr);templatebool Dictionary:search(string str,string &result,ChainNode *&ptr)int k;ChainNode *p1,*p2;Entry strEnt(str);k=strEnt.firstLetter()%dicSize;p1=&htk;if(p

8、1-link!=NULL)doptr=p1;p1=p1-link;if(p1-EntryLink.word=strEnt.word)result=p1-EntryLink.chinese;return 1;while(p1-link!=NULL);return 0;elsereturn 0;/Entry_head.h#include#include#includeusing namespace std;templateclass Entrypublic:K word;K chinese;public:Entry(K,K);Entry();Entry(K);void output();int f

9、irstLetter();templateEntry:Entry(K w,K c)word=w;chinese=c;templateEntry:Entry()word=;chinese=;templateEntry:Entry(K w)word=w;chinese=;templatevoid Entry:output()cout英文:wordendl;cout中文:chineseendlendl;templateint Entry:firstLetter()char *strv = strdup (word.c_str();/stringcharreturn int(strv0);主要代码结构

10、:/Hash_main.cpp#include#include#includeEntry_head.h#includeHash_head.husing namespace std;const int deafaultSize=10+1;int main()cout*endl;cout* *endl;cout* 大猫哥 *endl;cout* Make By LJDe.de *endl;cout* E-mail:LJDede *endl;cout* *endl;cout*endl;Entry edeafaultSize=Entry(eliminate,消除),Entry(accommodatio

11、n ,招待设备),Entry(convince ,使确信),Entry(conscience ,良心),Entry(lantern ,灯笼),Entry(procession ,队伍),Entry(quit,离开),Entry(pudding ,布丁),Entry(reaction ,反应),Entry(shrink ,收缩),Entry();Dictionary Entry dic(e);int deed;string str1,str2;Entry *eLit;dic.dicPos(e);docoutendl*endl;cout1.查看已有的字典词条。endl;cout2.添加新的词条到字

12、典。endl;cout3.删除已有词条。endl;cout4.查字典。endl;cout5.清屏。endl;cout6.退出程序。endl;cout*endl;coutdeed;cout你选择的操作是:deedendl;switch(deed)case 1:coutendl*endl;dic.outputDic();break;case 2:coutendl*endl;coutstr1;coutendl;coutstr2;coutendl;dic.insert(str1,str2);cout你输入的词条为:endlendl;eLit=new Entry (str1,str2);eLit-output();break;case 3:coutendl*endl;coutstr1;if(dic.remove(str1)cout删除成功!endl;elsecout删除失败,目标词条不存在endl;break;case 4:coutendl*endl;coutstr1;dic.search(str1,str2);cout你要查询的词条为:endl;eLit=new Entry (str1,str2);eLit-output();break;case 5:coutendl*endl;system(cls);break;default:break;while(de

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论