C 语言词频统计258
词频统计是指统计一个文本中每个单词出现的次数。在 C 语言中,我们可以使用哈希表来实现词频统计。哈希表是一个数据结构,它将键值对存储在数组中,并使用哈希函数将键映射到数组索引。哈希函数将键转换成一个数字,该数字用于确定键值对在数组中的位置。
要使用哈希表进行词频统计,我们可以将单词作为键,将单词出现的次数作为值。当我们从文本中读取单词时,我们首先检查哈希表中是否已经存在该单词。如果单词不存在,我们就创建一个新的键值对,并将单词的出现次数初始化为 1。如果单词已经存在,我们就将单词的出现次数增加 1。
以下是 C 语言中使用哈希表进行词频统计的一个示例代码:```c
#include
#include
#include
// 哈希表大小
#define HASHTABLE_SIZE 1000
// 哈希表节点
typedef struct node {
char *key;
int value;
struct node *next;
} node_t;
// 哈希表
typedef struct hashtable {
node_t table;
int size;
} hashtable_t;
// 创建哈希表
hashtable_t *create_hashtable(int size) {
hashtable_t *hashtable = malloc(sizeof(hashtable_t));
if (hashtable == NULL) {
return NULL;
}
hashtable->table = malloc(sizeof(node_t *) * size);
if (hashtable->table == NULL) {
free(hashtable);
return NULL;
}
for (int i = 0; i < size; i++) {
hashtable->table[i] = NULL;
}
hashtable->size = size;
return hashtable;
}
// 销毁哈希表
void destroy_hashtable(hashtable_t *hashtable) {
for (int i = 0; i < hashtable->size; i++) {
node_t *node = hashtable->table[i];
while (node != NULL) {
node_t *next = node->next;
free(node->key);
free(node);
node = next;
}
}
free(hashtable->table);
free(hashtable);
}
// 哈希函数
unsigned int hash_function(char *key) {
unsigned int hash = 0;
for (int i = 0; key[i] != '\0'; i++) {
hash = hash * 31 + key[i];
}
return hash;
}
// 插入键值对
void insert(hashtable_t *hashtable, char *key, int value) {
unsigned int hash = hash_function(key) % hashtable->size;
node_t *node = malloc(sizeof(node_t));
if (node == NULL) {
return;
}
node->key = strdup(key);
node->value = value;
node->next = hashtable->table[hash];
hashtable->table[hash] = node;
}
// 查找键
node_t *find(hashtable_t *hashtable, char *key) {
unsigned int hash = hash_function(key) % hashtable->size;
node_t *node = hashtable->table[hash];
while (node != NULL) {
if (strcmp(node->key, key) == 0) {
return node;
}
node = node->next;
}
return NULL;
}
int main() {
// 创建哈希表
hashtable_t *hashtable = create_hashtable(HASHTABLE_SIZE);
// 从文本中读取单词
FILE *fp = fopen("", "r");
if (fp == NULL) {
perror("fopen");
return EXIT_FAILURE;
}
char word[100];
while (fscanf(fp, "%s", word) != EOF) {
// 将单词插入哈希表
node_t *node = find(hashtable, word);
if (node == NULL) {
insert(hashtable, word, 1);
} else {
node->value++;
}
}
fclose(fp);
// 打印词频
for (int i = 0; i < hashtable->size; i++) {
node_t *node = hashtable->table[i];
while (node != NULL) {
printf("%s: %d", node->key, node->value);
node = node->next;
}
}
// 销毁哈希表
destroy_hashtable(hashtable);
return EXIT_SUCCESS;
}
```
在这个示例中,我们使用一个包含 1000 个桶的哈希表。我们使用哈希函数将单词映射到哈希表中的桶中。如果单词已经存在于哈希表中,我们就把它的出现次数增加 1。否则,我们就创建一个新的哈希表节点并将单词插入到哈希表中。
一旦我们从文本中读取了所有单词,我们就可以遍历哈希表并打印出每个单词的出现次数。我们可以通过访问哈希表中的链表来遍历哈希表中的所有单词。
使用哈希表的词频统计算法的时间复杂度为 O(n),其中 n 是文本中单词的数量。这是因为哈希表查找和插入操作的时间复杂度为 O(1),而遍历文本和哈希表的时间复杂度为 O(n)。
2025-02-06
上一篇:C 语言中静态函数的调用限制
下一篇:C 语言中不同文件之间的函数声明
Java数组元素:从基础到高级操作的深度解析
https://www.shuihudhg.cn/134539.html
PHP Web应用的安全基石:全面解析数据库SQL注入防御
https://www.shuihudhg.cn/134538.html
Python函数入门到进阶:用简洁代码构建高效程序
https://www.shuihudhg.cn/134537.html
PHP中解析与提取代码注释:DocBlock、反射与AST深度探索
https://www.shuihudhg.cn/134536.html
Python深度解析与高效处理.dat文件:从文本到二进制的实战指南
https://www.shuihudhg.cn/134535.html
热门文章
C 语言中实现正序输出
https://www.shuihudhg.cn/2788.html
c语言选择排序算法详解
https://www.shuihudhg.cn/45804.html
C 语言函数:定义与声明
https://www.shuihudhg.cn/5703.html
C语言中的开方函数:sqrt()
https://www.shuihudhg.cn/347.html
C 语言中字符串输出的全面指南
https://www.shuihudhg.cn/4366.html