C语言课设英文词频计算程序

C语言课设英文词频计算程序,第1张

思路示意:

char word[1000][20] ={0}//统计1000个单词,每个单词20字节长

int wordcount=0

int wordfreq[1000]= {0}//每个单词的词频

char line[1000]//每行最多1000个字母

int wordstart,wordend,ii

FILE *fp = fopen(文本文件)

while(!feof(fp))

{

scanf(fp,"%s",line)//读取一行

wordstart=0

for(wordend=wordstartwordstart<strlen(line) &&wordend<strlen(line)wordend++) //查找空格,切分单词

{

if (line[wordend]==' ')

{

line[wordend]=0x00

for(ii=0ii<wordcountii++) //查找单词是备亩否在word队列

{

if (!strcmp(word[ii], line[wordstart]) //单词已经存在

{

wordfreq[ii] ++

break

}

}

if (ii>=wordcount) //单词不存在

{

strcpy(word[wordcount], line[wordstart], strlen(line[wordstart])

wordcount++

}

wordstart = wordend + 1

}

}

if (wordstart<strlen(line)) //此行最后一个单词

{

line[wordend]=0x00

for(ii=0ii<wordcountii++) //查找单词是否在word队列中

{

if (!strcmp(word[ii], line[wordstart]) /仿滚森/单词已经存在

{

wordfreq[ii] ++

break

}

}

if (ii>=wordcount) //单词备前不存在

{

strcpy(word[wordcount], line[wordstart], strlen(line[wordstart])

wordcount++

}

wordstart = wordend + 1

}

}

}

fclose(fp)

//此时,word队列中保存了读取的所有单词,wordfreq队列中保存了相应的词频。

//使用排序算法进行排序(代码可以在网上搜)

for(ii=0ii<wordcountii++)

printf(" %s -> %d\n", word[ii], wordfreq[ii])

return 0

用我的程序有以下好处:

一、避免第一个单词出搜信现前的空格计算为单词。

二、避免字符窜中间的多个连续空格重复计算单词个世梁轮渣闭数。

#include<stdio.h>

int strwordcount(char p[20])

{

int i=0

int count=0

if(p[0]==' ')

i++

for(i=1i<20i++)

{ if(p[i]==' '&&p[i-1]!=' ')

count++

}

return count+1

}

void main()

{

int c

char p[20]

gets(p)

c=strwordcount(p)

printf("%d\n",c)

}

我把你的代码重写了一下,你写得太繁琐了。茄洞

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#define MAX 1000

int main(void)

{

char word[MAX]={0}

int wordcount[26]={0}

int i,a,z,sum=0

a='a'z='z'-'a'+1

srand(time(NULL))

for(i=0i<MAXi++)

{

word[i]=rand()%z+a

wordcount[word[i]-'a']++

}

for(i=0i<26i++)

{

printf("出现字母败纳颤%c的次察败数为: %d\n",i+'a',wordcount[i])

sum+=wordcount[i]

}

printf("字母总数验证:%d\n",sum)

return 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12434632.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存