//字符串哈希函数
#define MOD 100
//(著名的ELFhash算法)
#include<stdio.h>
#include<string.h>
int ELFhash(char *key)
{
unsigned long h=0;
while(*key)
{
h=(h<<4)+*key++;
unsigned long g=h&0Xf0000000L;
if(g)
h^=g>>24;
h&=~g;
}
return h%MOD;
}
int main()
{
int istr;
char str[]="abcdef";
istr=ELFhash(str);
printf("%s->%d\n",str,istr);
printf("===========\n");
//str[]="abc";
strcpy(str,"a");
istr=ELFhash(str);
printf("%s->%d\n",str,istr);
printf("===========\n");
strcpy(str,"A");
istr=ELFhash(str);
printf("%s->%d\n",str,istr);
printf("===========\n");
str[0]='\0';
istr=ELFhash(str);
printf("%s->%d\n",str,istr);
printf("===========\n");
strcpy(str,"AB");
istr=ELFhash(str);
printf("%s->%d\n",str,istr);
printf("===========\n");
strcpy(str,"Ab");
istr=ELFhash(str);
printf("%s->%d\n",str,istr);
printf("===========\n");
return 0;
}
/*
abcdef->22
===========
a->97
===========
A->65
===========
->0
===========
AB->6
===========
Ab->38
===========
Press any key to continue
*/
著名的ELFhash算法
最新推荐文章于 2021-07-05 13:56:32 发布