C++实现的哈希搜索

程序内容

Complete a text searching engine using hash table.

完成一个文本搜索引擎,使用哈希表


程序设计

程序流程图

程序代码

程序变量
const int HASH_SIZE=100;
vector<string> container;
struct hash_node{
int number;
string key;
hash_node* next;
hash_node(string a="",int b=0,hash_node* c=NULL);
};
hash_node* hash_chain=NULL;
string word;
初始化

方法是从文件流单个读取字符,遇到句号,问号,感叹号和省略号都视为断句标志,利用了char和string之间的转换,出现bug的情况是句子字符长度超过1024,这个概率太小。程序兼顾了去除前导空格的功能。

bool ReadFile(string source){//读取文件并存储
ifstream fin;
fin.open(source.c_str());
if(!fin){//打开文件失败
cout<<"Unable to open "<<source<<"."<<endl;
return false;
}
//cout<<"open success"<<endl;
container.clear();
char buffer[1024]={0};
int k=0;
string temp;
while(!fin.eof()){
buffer[k++]=fin.get();
//断句符有三个
if(buffer[k-1]=='.'||buffer[k-1]=='?'||buffer[k-1]=='!'){
temp=buffer;
container.push_back(temp);
memset(buffer,0,1024);
k=0;
while(!fin.eof()){
if(isalpha(buffer[k]=fin.get())){
k=1;
break;
}
}
}
}
fin.close();
cout<<"succeed to open "<<source<<"."<<endl;
return true;
}
创建哈希表

核心哈希函数,string转int,来源CSDN博客独孤小剑,自己改进了取模操作

int Hash(string &key){//哈希 string转int
int seed=31;
int hash=0;
int strln=key.length();
for(int i=0;i<strln;i++)
hash=(hash*seed+key[i])%HASH_SIZE;
return hash%HASH_SIZE;
}

生成哈希表函数,从容器中逐个取出单词(消除最后一个单词的标点符号),根据哈希函数存储在哈希表中,利用自定义的结构体。同一个句子中相同的单词只存储一次。

void CreatHashTable(){//生成哈希表
hash_chain=new hash_node [HASH_SIZE];
for(int i=0;i<HASH_SIZE;i++){//初始化
hash_chain[i].key="";
hash_chain[i].number=0;
hash_chain[i].next=NULL;
}
int pos;
string temp;
for(int i=0;i<container.size();i++){
temp=container[i];//去除结尾符号
transform(temp.begin(),temp.end(),temp.begin(),Cut);
//cout<<container[i]<<endl;
istringstream iss(temp);
while(iss>>temp){//读取单词
//cout<<temp<<endl;
pos=Hash(temp);
hash_node* p1=&hash_chain[pos];
if(p1->key==""){
p1->key=temp;
p1->number=i;
}
else{
bool repeat=false;
while(p1->next!=NULL){
if(temp==p1->key&&i==p1->number){
repeat=true;
break;
}
p1=p1->next;
}
if(temp==p1->key&&i==p1->number){
repeat=true;
}
if(repeat)
continue;
hash_node* pnew=new hash_node(temp,i,NULL);
pnew->next=p1->next;
p1->next=pnew;
}
}
}
}
搜索关键词

前提是成功打开文件。利用循环多次搜索,退出的关键词是END(大小写敏感)。instruction()函数中有说明退出方法。

int main()
{
instruction();
if(ReadFile("source.txt")){
//PrintContainer();
CreatHashTable();
cout<<"Please enter you key: ";
while((cin>>word)&&word!="END"){
SearchFor(word);
cout<<"Please enter you key: ";
}
DeleteHashTable();
}
return 0;
}
释放内存

良好习惯(强迫症罢了),据说这才是程序不崩溃的核心=。=

void DeleteHashTable(){//删除哈希表
hash_node *p1,*p2,*p3;
for(int i=0;i<HASH_SIZE;i++){
p1=&hash_chain[i];
p2=p1->next;
while(p2!=NULL){
p3=p2->next;
delete p2;
p2=p3;
}
}
delete [] hash_chain;
container.clear();
}

程序运行情况


完整代码

#include<bits/stdc++.h>
using namespace std; const int HASH_SIZE=100;
vector<string> container;
struct hash_node{
int number;
string key;
hash_node* next;
hash_node(string a="",int b=0,hash_node* c=NULL);
};
hash_node* hash_chain=NULL;
string word; bool ReadFile(string source);
void PrintContainer();
void CreatHashTable();
int Hash(string &key);
char Cut(char c);
void SearchFor(string word);
void instruction();
void DeleteHashTable(); int main()
{
instruction();
if(ReadFile("source.txt")){
//PrintContainer();
CreatHashTable();
cout<<"Please enter you key: ";
while((cin>>word)&&word!="END"){
SearchFor(word);
cout<<"Please enter you key: ";
}
DeleteHashTable();
}
return 0;
} void instruction(){//程序介绍
cout<<"Programme: Search.cpp"<<endl;
cout<<"Author: Wsine"<<endl;
cout<<"Date: 2014-11-29"<<endl;
cout<<"Exit key is 'END'"<<endl<<endl;
} bool ReadFile(string source){//读取文件并存储
ifstream fin;
fin.open(source.c_str());
if(!fin){//打开文件失败
cout<<"Unable to open "<<source<<"."<<endl;
return false;
}
//cout<<"open success"<<endl;
container.clear();
char buffer[1024]={0};
int k=0;
string temp;
while(!fin.eof()){
buffer[k++]=fin.get();
//断句符有三个
if(buffer[k-1]=='.'||buffer[k-1]=='?'||buffer[k-1]=='!'){
temp=buffer;
container.push_back(temp);
memset(buffer,0,1024);
k=0;
while(!fin.eof()){
if(isalpha(buffer[k]=fin.get())){
k=1;
break;
}
}
}
}
fin.close();
cout<<"succeed to open "<<source<<"."<<endl;
return true;
} void PrintContainer(){//打印函数供测试使用
for(int i=0;i<container.size();i++)
cout<<container[i]<<endl;
} int Hash(string &key){//哈希 string转int
int seed=31;
int hash=0;
int strln=key.length();
for(int i=0;i<strln;i++)
hash=(hash*seed+key[i])%HASH_SIZE;
return hash%HASH_SIZE;
} char Cut(char c){//自定义切割函数
if(isalpha(c)||c=='\0')
return c;
else
return ' ';
} void CreatHashTable(){//生成哈希表
hash_chain=new hash_node [HASH_SIZE];
for(int i=0;i<HASH_SIZE;i++){//初始化
hash_chain[i].key="";
hash_chain[i].number=0;
hash_chain[i].next=NULL;
}
int pos;
string temp;
for(int i=0;i<container.size();i++){
temp=container[i];//去除结尾符号
transform(temp.begin(),temp.end(),temp.begin(),Cut);
//cout<<container[i]<<endl;
istringstream iss(temp);
while(iss>>temp){//读取单词
//cout<<temp<<endl;
pos=Hash(temp);
hash_node* p1=&hash_chain[pos];
if(p1->key==""){
p1->key=temp;
p1->number=i;
}
else{
bool repeat=false;
while(p1->next!=NULL){
if(temp==p1->key&&i==p1->number){
repeat=true;
break;
}
p1=p1->next;
}
if(temp==p1->key&&i==p1->number){
repeat=true;
}
if(repeat)
continue;
hash_node* pnew=new hash_node(temp,i,NULL);
pnew->next=p1->next;
p1->next=pnew;
}
}
}
} hash_node::hash_node(string a,int b,hash_node* c){//结构体构造函数
key=a;
number=b;
next=c;
} void SearchFor(string word){//搜索函数
int pos=Hash(word),k=1;
hash_node* p1=&hash_chain[pos];
while(p1!=NULL){//遍历链表
if(p1->key==word){
cout<<endl;
cout<<"Sentence "<<k++<<" :"<<endl;
cout<<container[p1->number]<<endl;
}
p1=p1->next;
}
cout<<endl;
} void DeleteHashTable(){//删除哈希表
hash_node *p1,*p2,*p3;
for(int i=0;i<HASH_SIZE;i++){
p1=&hash_chain[i];
p2=p1->next;
while(p2!=NULL){
p3=p2->next;
delete p2;
p2=p3;
}
}
delete [] hash_chain;
container.clear();
}

C++实现的哈希搜索的更多相关文章

  1. 如何在CentOS 7上安装Percona服务器

    在这篇文章中我们将了解关于 Percona 服务器,一个开源的MySQL,MariaDB的替代品.InnoDB的数据库引擎使得Percona 服务器非常有吸引力,如果你需要的高性能,高可靠性和高性价比 ...

  2. JVM剖析

    JVM剖析 这篇文章详细解释了Java虚拟机的内部架构.以下这幅图展示了Java虚拟机里面的关键组件(是依据Java SE 7版本的Java虚拟机). 这些组件将在下面的两个章节一一展开.第一章节涵盖 ...

  3. 《Python学习手册》读书笔记

    之前为了编写一个svm分词的程序而简单学了下Python,觉得Python很好用,想深入并系统学习一下,了解一些机制,因此开始阅读<Python学习手册(第三版)>.如果只是想快速入门,我 ...

  4. 显示引擎innodb状态详解

    很多人让我来阐述一下  SHOW INNODB STATUS 的输出信息,了解SHOW INNODB STATUS都输出了几个什么信息,并且我们能够这些信息中获取什么资讯,得以提高MySQL性能. 首 ...

  5. 《Python学习手册》读书笔记【转载】

    转载:http://www.cnblogs.com/wuyuegb2312/archive/2013/02/26/2910908.html 之前为了编写一个svm分词的程序而简单学了下Python,觉 ...

  6. Hash Tables

    哈希表 红黑树实现的符号表可以保证对数级别的性能,但我们可以做得更好.哈希表实现的符号表提供了新的数据访问方式,插入和搜索操作可以在常数时间内完成(不支持和顺序有关的操作).所以,在很多情况下的简单符 ...

  7. Windows五种IO模型性能分析和Linux五种IO模型性能分析

    Windows五种IO模型性能分析和Linux五种IO模型性能分析 http://blog.csdn.net/jay900323/article/details/18141217 http://blo ...

  8. 【比赛】NOIP2017 宝藏

    这道题考试的时候就骗了部分分.其实一眼看过去,n范围12,就知道是状压,但是不知道怎么状压,想了5分钟想不出来就枪毙了状压,与AC再见了. 现在写的是状压搜索,其实算是哈希搜索,感觉状压DP理解不了啊 ...

  9. Python学习手冊笔记

    之前为了编写一个svm分词的程序而简单学了下Python.认为Python非常好用.想深入并系统学习一下,了解一些机制,因此開始阅读<Python学习手冊(第三版)>. 假设仅仅是想高速入 ...

随机推荐

  1. Date获取时间段

    /** * */ package com.chinabase.common.util; /** * @author yuanji * @created on:Sep 19, 2008 */ impor ...

  2. redis使用

    1.服务器 下载redis Windows版,命令行启动redis-server.exe即可. 2.客户端使用 jedis 添加DLL: commons-pool2-2.0.jar jedis-2.4 ...

  3. C#基础-ref、out

    1.默认情况下,C#假定所有的方法参数传递都是传值的. 如下面的方法: public static void Main(string[] args) { int val = 5; //调用AddVal ...

  4. 开始记录blog

    将自己的总结.新的记录下来,形成习惯,为以后的温故知新

  5. 最新CSS3常用30种选择器总结(适合初学者)

     1. *:通用元素选择器 * { margin: 0; padding: 0; } *选择器是选择页面上的全部元素,上面的代码作用是把全部元素的margin和padding设为0,最基本的清除默认C ...

  6. iptables用法

    iptables -t nat -A PREROUTING -s 10.10.10.0/24 -i eth1 -p tcp --dport 80 -j REDIRECT --to-ports 3128 ...

  7. 软件工程 speedsnail 第二次冲刺8

    20150525 完成任务:障碍物整体设计,实现一页多次布局: 遇到问题: 问题1 与现有资源冲突 解决1 未解决 明日任务: 蜗牛碰到线后速度方向的调整:(做优化)

  8. SMTP Failed on Process Scheduler

    If you are seeing messages like this in your message log when running a process through the process ...

  9. Silverlight DataGrid数据行背景颜色控制

    sdk:DataGrid数据绑定后,部分特殊的行需要用不同的背景颜色来显示.(注册DataGrid的LoadingRow事件) private void radGridView_LoadingRow( ...

  10. js如何将纯数字字符串转换为long型

    1.js如何将纯数字字符串转换为long型? js 中 int的存储位数?最大十进制数表示是多少? 精度http://www.jb51.net/article/59808.htm 整数(不使用小数点或 ...