




免费预览已结束,剩余2页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
用散列表实现简单的英文词典2011年4月4日星期一一头文件:/文件名:Word.h/单词类的定义#include #include class Wordfriend ostream& operator(ostream &os, const Word & obj)/重载输出函数int n=strlen(obj.word);for(int i=0; in; +i) os obj.wordi;return os;public:char word25; /用于存放单词Word() for(int i=0; i25; +i) wordi=0;bool operator=(Word &r)/重载判断相等符号 if(strcmp(word,r.word)=0) return true;else return false;void operator=(Word &r) strcpy(word,r.word); /重载赋值运算符;/文件名:openHashTable.h/开散列表#include #include template class openHashTableprivate:struct node/私有结点Type data;struct node *next;node() next = NULL; node(Type &d) data = d; next = NULL;node *array;int(*key)(const Type &x);/关键字static int defaultKey(const int &k) return k; /缺省值int size;public:openHashTable(int length =101 , int(* f)(const Type &x) = defaultKey);openHashTable();int find(Type &x); /查找函数bool insert(Type &x); /插入函数bool remove( Type &x); /删除函数void output(); /输出词典函数;/=开散列表函数的实现=/构造函数的实现template openHashTable:openHashTable(int length, int(*f)(const Type &x)size = length;array = new node *size;key = f;for(int i=0; isize; +i) arrayi = new node;/析构函数的实现template openHashTable:openHashTable()node *p, *q;for(int i=0; inext;delete p;p = q;while(p!=NULL);delete array;/插入函数的实现template bool openHashTable:insert(Type &x)int pos;node *p;pos = key(x)%size; /计算相对应的关键字p = arraypos-next;while(p!=NULL & !(p-data=x) p = p-next;if(p=NULL)p = new node(x);p-next = arraypos-next;arraypos-next = p;return true;return false;/删除函数的实现template bool openHashTable:remove(Type &x)int pos; node *p, *q;pos = key(x)%size; /计算相对应的关键字 q = arraypos;p = q-next;while(p!=NULL& !(p-data=x) q = p;p = p-next;if(p=NULL) return false; /没有找到else q-next = p-next; /找到后删除delete p;return true;/查找函数的实现templateint openHashTable:find(Type &x)int pos;node *p;pos = key(x)%size; /计算相对应的关键字p = arraypos;while(p-next!=NULL & !(p-next-data=x) ) p = p-next;if(p-next!=NULL) return pos; /找到返回相应的桶位else return 0; /没到找到就返回0/打印词典函数的实现templatevoid openHashTable:output()node *p;for(int i=0; inext!=NULL)p=arrayi-next;if(i10) cout 00 i 10&i100) cout 0 i ;while(p!=NULL) /打印同一桶位,即有冲突的单词cout data;if(p-next!=NULL) cout ;if(p-next=NULL) cout next;二Main函数的实现:/文件名:openHashTableServesAs-A-DictionaryTest.cpp/用散列表实现英文词典#include openHashTable.h#include Word.h#include #include int myHash(const Word &a); /求权重函数int power(int n); /求2的n次方函数void menu(); /打印菜单函数void main() Word w; char chioce;int n;bool flag=false;openHashTabledictionary(101,myHash); /定义一个名为dictionary的开散列表,即作为词典while(!flag)menu();cin chioce;switch(chioce)case i: cout 请输入单词:; cin.ignore(1,n); cin.getline(w.word,25); if(dictionary.insert(w) cout 插入成功! endl; else cout 这个单词已存在! endl; break;case d: cout 请输入单词:; cin.ignore(1,n); cin.getline(w.word,25); if(dictionary.remove(w) cout 删除成功! endl; else cout 这个单词不存在! endl; break;case s: cout 请输入单词:; cin.ignore(1,n); cin.getline(w.word,25); n = dictionary.find(w); if(n!=0) cout 已找到,单词在第 n 桶中 endl; else cout 这个单词不存在! endl; break;case v: cout 词典如下所示: endl; dictionary.output(); break;case q: flag = true; break;default: cout 输入错误! =a&a.wordi=z) num += (a.wordi - 0- 17 -32)*power(i);else num += (a.wordi - 0-17)*power(i);return num;/
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年杭州市上城区小营街道办事处编外招聘1人考前自测高频考点模拟试题及答案详解(名师系列)
- 新型催化剂开发-洞察及研究
- 连锁超市库存管理系统需求说明
- 小学一年级解决实际问题数学练习
- 2025内蒙古赤峰穆香源肉类食品有限公司招聘模拟试卷及一套参考答案详解
- 2025广东韶关市始兴县事业单位招聘工作人员暨“青年人才”和“急需紧缺人才”招聘89人考前自测高频考点模拟试题附答案详解(典型题)
- 小升初数学计算专项训练与解析
- 新高考英语作文写作指导及范文解析
- 极地冰雪建筑的遥感监测技术-洞察及研究
- 名师讲座听课笔记及教学方法总结
- 纪念九·一八:致敬那场永不妥协的抗争-主题班会课件
- 2025年周年热点大事件复习课件-【知识精讲精研】高三历史统编版(2019)二轮复习
- 【道法】做自强不息的中国人课件+-2024-2025学年统编版道德与法治七年级下册
- 老年人高血压健康知识
- 水泥电杆行业分析报告
- 煤矿安全监控系统培训课件
- T∕CEC 208-2019 电动汽车充电设施信息安全技术规范
- 全案托管设计合同范例
- 中医拔罐技术试题及答案
- 浙江水利专业高级工程师任职资格考试题及答案
- DB65-T 4783-2024 冰川资源遥感调查技术规范
评论
0/150
提交评论