单词文件名:Englishwords.txt
文件内容格式:
abandon vt.丢弃;放弃,抛弃
运行:
冲突率:21.866667%
平均查找长度:1.274000
请输入要查找的单词:abandon
查找了1次
意思是:vt.丢弃;放弃,抛弃
请输入要查找的单词:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HASHMAPLENGTH 10000
int conflictWordNum;
int wordsSum;
float conflictRate;
int searchLength;
float averageSearchLength;
typedef struct node{
char *word;
char *annotation;
struct node *next;
}NODE;
typedef struct hash{
struct node *head;
}HASH;
HASH *createHashMap(){
HASH *hashMap = (HASH *)malloc(sizeof(HASH)*HASHMAPLENGTH);
int i;
for (i = 0; i < HASHMAPLENGTH; i++){
hashMap[i].head = NULL;
}
return hashMap;
}
unsigned int BKDRHash(char *str){
unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
unsigned int hash = 0;
while (*str){
hash = hash * seed + (*str++);
}
return (hash & 0x7FFFFFFF);
}
void insertHashMap(HASH *hashMap, char *word, char *annotation){
//开辟节点空间,接受数据
NODE *pnew = (NODE *)malloc(sizeof(NODE));
if (pnew == NULL){
exit(0);
}
pnew->word = (char *)malloc(strlen(word) + 1);
strcpy(pnew->word, word);
pnew->annotation = (char *)malloc(strlen(annotation) + 1);
strcpy(pnew->annotation, annotation);
//根据Key得到hash地址
unsigned int hashAddress = BKDRHash(word) % HASHMAPLENGTH;
//链接
pnew->next = NULL;
NODE *p = hashMap[hashAddress].head;
//找到尾部
if (p == NULL){
hashMap[hashAddress].head = pnew;
}
else {
while (p->next != NULL){
p = p->next;
searchLength++;
}
p->next = pnew;
conflictWordNum++;
searchLength++;
}
searchLength++;
wordsSum++;
}
char *findHashMap(HASH *hashMap, char *word){
//根据Key得到hash地址
unsigned int hashAddress = BKDRHash(word) % HASHMAPLENGTH;
NODE *p = hashMap[hashAddress].head;
int searchTimes = 0;
if (p != NULL){
if (!strcmp(word, p->word)){
searchTimes++;
printf("查找了%d次\n", searchTimes);
return p->annotation;
}
while (p->next != NULL){
p = p->next;
searchTimes++;
if (!strcmp(word, p->word)){
printf("查找了%d次\n", searchTimes);
return p->annotation;
}
}
}
return NULL;
}
void hashEvaluation(){
conflictRate = conflictWordNum / (wordsSum * 1.0);
averageSearchLength = searchLength / (wordsSum * 1.0);
printf("冲突率:%f%%\n", conflictRate*100);
printf("平均查找长度:%f\n", averageSearchLength);
}
int checkString(char *str){
int i;
for (i = 0; i < strlen(str); i++){
if(str[i] == '.'){
return 1;
}
}
return 0;
}
void Read_WordFile(HASH *hashMap, FILE *fp){
char buf[100] = {0};
char word[50] = {0};
char annotation[50] = {0};
while (fgets(buf, sizeof(buf), fp) != NULL){
buf[strlen(buf) - 1] = '\0';
if (checkString(buf)){
if (buf[0] == ' '){
sscanf(buf, " %[a-z] %s", word, annotation);
}
else {
sscanf(buf, "%[a-z] %s", word, annotation);
}
insertHashMap(hashMap, word, annotation);
}
}
}
void DisplayHashMap(HASH *hashMap){
int i;
NODE *p;
int count = 0;
for (i = 0; i < HASHMAPLENGTH; i++){
p = hashMap[i].head;
while (p != NULL){
printf("%d ", ++count);
printf("%s ", p->word);
printf("%s \n", p->annotation);
p = p->next;
}
}
}
void ClearHashMap(HASH *hashMap){
int i;
NODE *p;
for (i = 0; i < HASHMAPLENGTH; i++){
p = hashMap[i].head;
while (p != NULL){
hashMap[i].head = p->next;
free(p);
if (hashMap[i].head == NULL){
break;
}
p = hashMap[i].head->next;
}
}
}
void DestoryHashMap(HASH **hashMap){
ClearHashMap(*hashMap);
free(*hashMap);
*hashMap = NULL;
}
int main(){
HASH *hashMap = createHashMap();
FILE *fp = fopen("Englishwords.txt","r");
if (fp == NULL){
printf("打开文件发生错误!\n");
return 0;
}
Read_WordFile(hashMap, fp);
hashEvaluation();
// DisplayHashMap(hashMap);
char word[20];
char *annotation;
while (1){
printf("请输入要查找的单词:");
scanf("%s", word);
annotation = findHashMap(hashMap, word);
printf("\t意思是:%s\n", annotation);
}
DestoryHashMap(&hashMap);
fclose(fp);
return 0;
}