




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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<assert.h>#include<string>#include<stdlib.h>using namespace std;const int defaultDicSize=10;const int d
3、efaultTableSize=10;class ChainNodepublic:Entry<string> EntryLink;ChainNode *link;template<class E>class Dictionarypublic:Dictionary(E e);Dictionary()delete ht;bool search(string,string &);bool search(string,string &,ChainNode *&);bool insert(E);bool insert(string,string);bool
4、 remove(string);void dicPos(E e);void outputDic();private:ChainNode *ht;int dicSize;/总词条数int tableSize;/每个索引容量;template<class E>Dictionary<E>:Dictionary(E e)/计算词条数,作为字典最大长度dicSize=0;while(edicSize.chinese!="")dicSize+;/初始化字典ht=new ChainNodedicSize;for(int i=0;i<dicSize;i+)ht
5、i.link=NULL;hti.EntryLink=Entry<string>();template<class E>void Dictionary<E>:dicPos(E e)int i;ChainNode *p;for(i=0;i<dicSize;i+)insert(ei);template<class E>bool Dictionary<E>:insert(E ent)int k;ChainNode *p1,*p2;tableSize=5;k=ent.firstLetter()%tableSize;p1=&htk;
6、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;template<class E>bool Dictionary<E>:insert(string str1,string str2)Entry<string> *e=new Entry<string> (str1,str2);insert(*
7、e);return 1;template<class E>void Dictionary<E>:outputDic()int i;ChainNode *p1;for(i=0;i<tableSize;i+)/cout<<"*"<<endl;/cout<<i<<endl;/cout<<"*"<<endl;if(hti.link!=NULL)p1=&hti;while(p1->link!=NULL)p1=p1->link;p1->En
8、tryLink.output();/cout<<"*"<<endl;template<class E>bool Dictionary<E>:remove(string str)string result;ChainNode *ptr;if(search(str,result,ptr)=1)ptr->link=ptr->link->link;return 1;elsereturn 0; template<class E>bool Dictionary<E>:search(string s
9、tr,string &result)ChainNode *ptr;return search(str,result,ptr);template<class E>bool Dictionary<E>:search(string str,string &result,ChainNode *&ptr)int k;ChainNode *p1,*p2;Entry<string> strEnt(str);k=strEnt.firstLetter()%dicSize;p1=&htk;if(p1->link!=NULL)doptr=p1
10、;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<assert.h>#include<string>#include<stdlib.h>using namespace std;template<class K>class Entrypublic:K word;K chines
11、e;public:Entry(K,K);Entry();Entry(K);void output();int firstLetter();template<class K>Entry<K>:Entry(K w,K c)word=w;chinese=c;template<class K>Entry<K>:Entry()word=""chinese=""template<class K>Entry<K>:Entry(K w)word=w;chinese=""templ
12、ate<class K>void Entry<K>:output()cout<<"英文:"<<word<<endl;cout<<"中文:"<<chinese<<endl<<endl;template<class K>int Entry<K>:firstLetter()char *strv = strdup (word.c_str();/stringcharreturn int(strv0);主要代码结构:/Hash_main
13、.cpp#include<iostream>#include<string>#include"Entry_head.h"#include"Hash_head.h"using namespace std;const int deafaultSize=10+1;int main()cout<<"*"<<endl;cout<<"* *"<<endl;cout<<"* 大猫哥 *"<<endl;cout<
14、;<"* Make By LJDe.de *"<<endl;cout<<"* E-mail:LJDede *"<<endl;cout<<"* *"<<endl;cout<<"*"<<endl;Entry<string> edeafaultSize=Entry<string>("eliminate","消除"),Entry<string>("a
15、ccommodation ","招待设备"),Entry<string>("convince ","使确信"),Entry<string>("conscience ","良心"),Entry<string>("lantern ","灯笼"),Entry<string>("procession ","队伍"),Entry<string>("
16、quit","离开"),Entry<string>("pudding ","布丁"),Entry<string>("reaction ","反应"),Entry<string>("shrink ","收缩"),Entry<string>();Dictionary< Entry<string> > dic(e);int deed;string str1,str2;Entry&l
17、t;string> *eLit;dic.dicPos(e);docout<<endl<<"*"<<endl;cout<<"1.查看已有的字典词条。"<<endl;cout<<"2.添加新的词条到字典。"<<endl;cout<<"3.删除已有词条。"<<endl;cout<<"4.查字典。"<<endl;cout<<"5.清屏。"
18、;<<endl;cout<<"6.退出程序。"<<endl;cout<<"*"<<endl;cout<<"请选择你要进行的操作:"cin>>deed;cout<<"你选择的操作是:"<<deed<<endl;switch(deed)case 1:cout<<endl<<"*"<<endl;dic.outputDic();break;case
19、2:cout<<endl<<"*"<<endl;cout<<"请输入新词条的key,为英文:"cin>>str1;cout<<endl;cout<<"请输入该词条的value,为中文:"cin>>str2;cout<<endl;dic.insert(str1,str2);cout<<"你输入的词条为:"<<endl<<endl;eLit=new Entry<string> (str1,str2);eLit->output();break;case 3:cout<<endl<<"*"<<endl;cout<<"请输入要删去的词条的key,为英文:"cin>>str1;if(dic.remove(str1)cout<<"删除成功!"<<endl;elsecout<<"删除失败,目标词条不存在"<<endl;brea
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 电声器件在智能安防报警系统中的应用考核试卷
- 纤维表面的功能化处理考核试卷
- 肉制品加工企业的品牌推广与消费者体验提升考核试卷
- 绢纺与丝织品企业品牌塑造与传播考核试卷
- 个人物品清理协议
- 室内设计工装就业指南
- 稀有金属在磁性材料领域的应用考核试卷
- 电机组件的电磁兼容性设计考核试卷
- 粮食仓储企业绿色经济国际合作考核试卷
- 玻璃制造流程及应用考核试卷
- 连云港2025年连云港市赣榆区事业单位招聘31人笔试历年参考题库附带答案详解
- 8.1薪火相传的传统美德 课件-2024-2025学年统编版道德与法治七年级下册
- 湖北省武汉市2025届高中毕业生四月调研考试语文试卷及答案(武汉四调)
- 食堂负面清单管理制度
- 2025年安徽省示范高中皖北协作区第27届联考 生物学(含解析)
- 新中考考试平台-考生端V2.0使用手册
- 《诗词五首渔家傲(李清照)》优秀课件
- 初中数学北师大七年级下册(2023年新编) 三角形《认识三角形》教学设计
- 现浇箱梁施工危险源辨识及分析
- 抗高血压药物研究进展页PPT课件
- 环境土壤学PPT课件
评论
0/150
提交评论