#include #define HASH_TABLE_SIZE 49999
typedef struct word{
char wrd[80];
int num;
}word;
static int diff_words=0;
static int total_words=0;
word *hashtable[HASH_TABLE_SIZE];
int hash(int number){
return number%HASH_TABLE_SIZE;
}
void insert(int count,char *w)
{
int pos=hash(count);
word *newWord;
total_words++;
while (hashtable[pos])
{
if(!strcmp(hashtable[pos]->wrd,w))
{
hashtable[pos]->num++;
return;
}
else
{
pos++;
}
}
newWord=(word*)malloc(sizeof(word));
newWord->num=1;
strcpy(newWord->wrd,w);
hashtable[pos]=newWord;
diff_words++;
}
int main()
{
FILE *fp;
char wrd_temp[80],ch;
word *head,*curr;
int i,arr[100],count=0,j;
word *temp=(word*)malloc(sizeof(word));
memset(hashtable,0,HASH_TABLE_SIZE);
fp=fopen("TheGun.txt","r"); //只读方式打开
i=0;
while (1)
{
ch=fgetc(fp);
if(isalpha(ch)||ch=='\'')
{
if(islower(ch))
{
wrd_temp[i++]=ch-32; //小写字母转大写
count=count+ch-32; //count用于计算单词的哈希值
}
else
{
wrd_temp[i++]=ch;
count=count+ch;
}
}
else
{
wrd_temp[i++]='\0';
if(strlen(wrd_temp)>=1)
{
insert(count,wrd_temp);
}
memset(wrd_temp,0,80);
i=0;
count=0;
}
if(feof(fp))
break;
}
fclose(fp);
printf("Number of total_words = %d\n",total_words);
printf("Number of different words = %d\n",diff_words);
printf("The 100 most common words:\n");
printf("WORD NUMBER OF OCCURRENCES\n");
for (i = 0; i < 10; i++)
{
count=0;
for (j = 0; j < HASH_TABLE_SIZE; j++)
{
if(hashtable[j]!=NULL && hashtable[j]->num>count)
{
count=hashtable[j]->num;
arr[i]=j;
}
}
printf("%s %d\n",hashtable[arr[i]]->wrd,hashtable[arr[i]]->num);
hashtable[arr[i]]->num=0;
}
return 0;
}