Скачиваний:
33
Добавлен:
10.05.2014
Размер:
1.92 Кб
Скачать
#include <iostream>
#include <string.h>
#define N 20 //количество строк
#define MaxStrLen 10 //максимальная длина строки

using namespace std;

char st[N][MaxStrLen];
int ht[N];

void init_str(void)
{
	strcpy(st[0],"zero");
    strcpy(st[1],"one");
    strcpy(st[2],"two");
    strcpy(st[3],"three");
    strcpy(st[4],"four"); 
    strcpy(st[5],"five");
    strcpy(st[6],"six");
    strcpy(st[7],"seven");
    strcpy(st[8],"eight");
    strcpy(st[9],"nine");
    strcpy(st[10],"ten");
    strcpy(st[11],"eleven");
    strcpy(st[12],"twelve");
    strcpy(st[13],"thirteen");
    strcpy(st[14],"fourteen");
    strcpy(st[15],"fifteen");
    strcpy(st[16],"sixteen");
    strcpy(st[17],"seventeen");
    strcpy(st[18],"eighteen");
    strcpy(st[19],"nineteen");
    
    strcpy(st[13],"");
}

int hash_calc_sv2(char x[],const int size)
{
	int StringLength,HashKey,i,tmp;
	StringLength=strlen(x);
	HashKey=0;
	if (StringLength%2==0) //Чётный случай
	{
		for (i=0; i<StringLength; i=i+2)
		{
			tmp=x[i];
			tmp=tmp*1000;
			tmp=tmp+x[i+1];
			HashKey=HashKey+tmp;
		}
	}
	if (StringLength%2!=0) //Нечётный случай
	{
		for (i=1; i<StringLength; i=i+2)
		{
			tmp=x[i];
			tmp=tmp*1000;
			tmp=tmp+x[i+1];
			HashKey=HashKey+tmp;
		}
		HashKey=HashKey+x[0];
	}
	return (HashKey);
}

int hash_calc(char x[],const int size)
{
	int StringLength,HashKey,i;
	StringLength=strlen(x);
	HashKey=0;
	for (i=0; i<=StringLength; i++)
	{
		HashKey=HashKey+x[i];
	}
	return (HashKey);
}

void init_hash_table(void)
{
	int i, key;
	for (i=0; i<N; i++)
	{
		ht[i]=-1;
	}
	for (i=0; i<N; i++)
	{
		if (strcmp(st[i],"")!=0)
		{
			key=hash_calc_sv2(st[i],MaxStrLen)%N;//Свёртка 2
			//cout<<st[i]<<" "<<key<<endl;
			if (ht[key]==-1)
			{
				ht[key]=i;
				cout<<key<<" "<<st[ht[key]]<<endl;
			}
		}
	}
}

int main(void)
{
	init_str();
	init_hash_table();
	return 0;
}
Соседние файлы в папке old1