c语言用链表实现,统计一个英文文本文件中每个单词的出现次数(词频统计),结果按单词词典序输出到屏幕

c语言用链表实现,统计一个英文文本文件中每个单词的出现次数(词频统计),结果按单词词典序输出到屏幕#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXWORD 32
struct node {
char word[MAXWORD];
int count;
struct node *link;
} ;
struct node *Wordlist = NULL;
int getWord(FILE *bfp,char *w);
int searchWord(char *w);
int insertWord( struct node *p, char *w);

int main()
{
char filename[32], word[MAXWORD];
FILE *bfp;
struct node *p;

scanf("%s", filename);
if((bfp = fopen(filename, "r")) == NULL){
fprintf(stderr, "%s can’t open!\n",filename);
return -1;
}
while( getWord(bfp,word) != EOF)
if(searchWord(word) == -1) {
fprintf(stderr, "Memory is full!\n");
return -1;
}
for(p=Wordlist; p != NULL; p=p->link)
printf("%s %d\n", p->word, p->count);
return 0;
}

int searchWord(char *w)
{
struct node *p, *q=NULL;
for(p=Wordlist; p != NULL; q=p,p=p->link){
if(strcmp(w, p->word) < 0)
break;
else if(strcmp(w, p->word) == 0){
p->count++;
return 0 ;
}
}
return insertWord(q, w);
}

//帮忙补充一下,谢谢(英文文本文件名为“article.txt”)

#include <stdio.h>
#include <string.h>
int main(void)
{
int a = 0, b = 0, c = 0;
char buf[128];
FILE *fp;
/* 打开文件,文件名必须大写 */
fp= fopen("DATA5610.TXT", "r");
if (!fp) {
printf("No 'DATA5610.TXT' found.\n");
return -1;
}
/* 逐次读取单词,空格或回车分割 */
while (fscanf(fp, "%s", buf) > 0) {
/* 如读取到的单词是 if,则a自增 1 */
if (strcmp(buf, "if") == 0)
a++;
else if (strcmp(buf, "while") == 0)
b++;
else if (strcmp(buf, "for") == 0)
c++;
}
printf("if: %d, while: %d, for: %d\n", a, b, c);
fclose(fp);
return 0;
}追问

你这只是这三个单词的词频统计

温馨提示:答案为网友推荐,仅供参考