最近写的c程序,但想了几天都想不出怎样写统计词频的函数word_f()
望哪个高手指点指点
#include<Stdio.h>
#include<Conio.h>
#include<String.h>
#include<Ctype.h>
#define linewide 3000 /*linewide为每行的宽度*/
unsigned char xx[1][linewide];
int paragraph=0; /*段数*/
int num=0; /*num为单词的共个数*/
int ReadDat(void); /*从文本读取文章的函数*/
void WriteDat(void); /*把结果写进文本的函数*/
int main(void)
{
ReadDat();
count();
word_f();
WriteDat();
getch();
return 0;
}
int word_f(void) /*统计词频*/
{
}
int count(void) /*统计单词个数*/
{
int i,j,k,strl,word=0;
for(i=0;i<paragraph;i++)
{
strl=strlen(xx[i]); /*求各段的长度*/
for(j=0;j<strl-1;j++)
{
if(xx[i][j]==' ') /*计算单词数的语句*/
word=0; /*若xx[i][j]是空格word为0,否则为1*/
else if(word==0) /*word从0变为1时,num加1*/
{
word=1;
num++;
}
}
}
}
int ReadDat(void)
{
FILE *fp;
int i=0;
unsigned char *p;
if((fp=fopen("IN.TXT","r"))==NULL)
{
printf("Open fail!\n"); /*IN.TXT文本不存在,显示失败*/
return 1;
}
while(fgets(xx[i],linewide,fp)!=NULL)
{
p=strchr(xx[i],'\n');
if(p) *p=0;
i++;
}
paragraph=i;
fclose(fp);
return 0;
}
void WriteDat()
{
FILE *fp;
int i;
fp=fopen("OUT.TXT","w");
fprintf(fp,"The number of word=%d\n",num);
fprintf(fp,"The number of paragraph= %d",paragraph);
printf("The number of word= %d\n",num);
printf("The number of paragraph= %d",paragraph);
/*for(i=0;i<paragraph;i++)
{
printf("%s\n",xx[i]);
fprintf(fp,"%s\n",xx[i]);
}*/
fclose(fp);
}
[此贴子已经被作者于2006-6-26 9:42:00编辑过]