c语言统计英文字母频率,c语言 英文单词频率统计 哈希存储

#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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值