用C语言简单易懂地实现统计出现次数最多的单词,统计一个英文的文本文件中出现次数最多的前10个单词规定,单词的含义为连续的字母大写或小写构成的字符串字母以外的其他符号和空白符号都是为单词之间的分隔符输出出现次数最多
时间: 2023-11-28 22:46:19 浏览: 436
以下是用C语言实现统计出现次数最多的单词的代码,具体实现思路在代码注释中有详细说明:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_WORD_LEN 100 // 定义单词最大长度
// 定义结构体存放单词和单词数量
typedef struct WordCount {
char word[MAX_WORD_LEN];
int count;
struct WordCount *next;
} WordCount;
// 将单词插入链表中
void insertWord(WordCount **head, char *word) {
WordCount *p = *head;
while (p != NULL) {
// 如果单词已经存在,单词数量+1
if (strcmp(p->word, word) == 0) {
p->count++;
return;
}
p = p->next;
}
// 如果单词不存在,新建一个节点
WordCount *newNode = (WordCount *)malloc(sizeof(WordCount));
strcpy(newNode->word, word);
newNode->count = 1;
newNode->next = *head;
*head = newNode;
}
// 读取文件并统计单词数量
void countWords(WordCount **head, char *filename) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
printf("Failed to open file %s\n", filename);
return;
}
char word[MAX_WORD_LEN];
int c;
while ((c = fgetc(fp)) != EOF) {
// 如果是字母,将其加入单词中
if (isalpha(c)) {
int i = 0;
while (isalpha(c)) {
word[i++] = tolower(c);
c = fgetc(fp);
}
word[i] = '\0';
insertWord(head, word);
}
}
fclose(fp);
}
// 比较函数,用于排序
int cmp(const void *a, const void *b) {
WordCount **pa = (WordCount **)a;
WordCount **pb = (WordCount **)b;
return (*pb)->count - (*pa)->count;
}
// 输出出现次数最多的前10个单词
void printTop10(WordCount **head) {
WordCount *p = *head;
WordCount *arr[1000];
int i = 0;
while (p != NULL) {
arr[i++] = p;
p = p->next;
}
qsort(arr, i, sizeof(WordCount *), cmp);
for (int j = 0; j < 10 && j < i; j++) {
printf("次数:%d\t%s\n", arr[j]->count, arr[j]->word);
}
}
int main() {
WordCount *head = NULL;
countWords(&head, "test.txt");
printTop10(&head);
return 0;
}
```
阅读全文