哈希表查找书内单词数
加载书并从书中提取单词,然后在表中构建/更新相应的对象。在对象中,单词是该对象表示的单词,count是该单词在书中出现的次数,而key是用于哈希的唯一ID。
#include<iostream>
#include <string>
#include <fstream>
#include<sstream>
using namespace std;
#define MAX 11003
#define MAX2 1301
//#define MULT 3
typedef struct WordCount {
char word[64];
unsigned short count; // # of times this word occurred in the book
unsigned short page; // last page this word occurred
unsigned short key;
struct WordCount *next;
}WordCount;
WordCount *hashtable[MAX] = {
nullptr };
WordCount *hashtable2[MAX2] = {
nullptr };
int HashIndex(const char *word)
{
int sum = 0;
for (int i = 0; word[i] != '\0'; ++i)
{
sum = sum + word[i];
}
return sum % MAX;
}
int HashIndex2(const char *word)
{
int sum = 0;
for (int i = 0; word[i] != '\0'; ++i)
{
sum = sum + word[i];
}
return sum % MAX2;
}
//chaining
void cinsertword(const char *word,int pagenumber)
{
WordCount *p;
int index = HashIndex(word);
for (p = hashtable2[index]; p != nullptr; p = p->next)
{
if (strcmp(p->word, word) == 0)
{
(p->count)++;
p->page = pagenumber;
p->key = index;
return;
}
}
p = new WordCount [sizeof(WordCount)];
p->count = 1;
p->page = pagenumber;
p->next =