C++英文词典的简单实现.doc_第1页
C++英文词典的简单实现.doc_第2页
C++英文词典的简单实现.doc_第3页
C++英文词典的简单实现.doc_第4页
C++英文词典的简单实现.doc_第5页
免费预览已结束,剩余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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论