输入一段100—200字的英文短文,存入一文件a中,编写一段C语言代码实现写函数统计短文出现的字母个数n及每个字母的出现次数,写函数以字母出现次数作权值,建Haffman树(n个叶子),给出每个字母的Haffman编码,用每
时间: 2023-08-07 12:05:16 浏览: 179
【python123题库附件】统计字母数量
个字母的Haffman编码对短文进行编码输出到文件b中。以下是一段英文短文及代码实现。
短文:
The quick brown fox jumps over the lazy dog. This sentence contains every letter of the English alphabet.
代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CHARACTERS 26
typedef struct node {
char character;
int frequency;
struct node *left;
struct node *right;
} node;
void get_frequencies(char *filename, int *frequencies) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
printf("Error: could not open file %s\n", filename);
exit(1);
}
char current_character;
while ((current_character = fgetc(file)) != EOF) {
if (current_character >= 'a' && current_character <= 'z') {
frequencies[current_character - 'a']++;
} else if (current_character >= 'A' && current_character <= 'Z') {
frequencies[current_character - 'A']++;
}
}
fclose(file);
}
int get_minimum_frequency_node_index(node **nodes, int nodes_count) {
int minimum_frequency = -1;
int minimum_frequency_index = -1;
for (int i = 0; i < nodes_count; i++) {
if (nodes[i] != NULL) {
if (minimum_frequency == -1 || nodes[i]->frequency < minimum_frequency) {
minimum_frequency = nodes[i]->frequency;
minimum_frequency_index = i;
}
}
}
return minimum_frequency_index;
}
node *create_node(char character, int frequency) {
node *new_node = malloc(sizeof(node));
new_node->character = character;
new_node->frequency = frequency;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
}
node *build_huffman_tree(int *frequencies) {
node *nodes[MAX_CHARACTERS];
for (int i = 0; i < MAX_CHARACTERS; i++) {
if (frequencies[i] > 0) {
nodes[i] = create_node(i + 'a', frequencies[i]);
} else {
nodes[i] = NULL;
}
}
int nodes_count = MAX_CHARACTERS;
while (nodes_count > 1) {
int minimum_frequency_node_index_1 = get_minimum_frequency_node_index(nodes, nodes_count);
node *minimum_frequency_node_1 = nodes[minimum_frequency_node_index_1];
nodes[minimum_frequency_node_index_1] = NULL;
int minimum_frequency_node_index_2 = get_minimum_frequency_node_index(nodes, nodes_count);
node *minimum_frequency_node_2 = nodes[minimum_frequency_node_index_2];
nodes[minimum_frequency_node_index_2] = NULL;
node *new_node = create_node('\0', minimum_frequency_node_1->frequency + minimum_frequency_node_2->frequency);
new_node->left = minimum_frequency_node_1;
new_node->right = minimum_frequency_node_2;
nodes[nodes_count++] = new_node;
}
return nodes[0];
}
void encode_huffman_tree(node *root, char *code, char **codes) {
if (root->left != NULL) {
code[strlen(code)] = '0';
encode_huffman_tree(root->left, code, codes);
}
if (root->right != NULL) {
code[strlen(code)] = '1';
encode_huffman_tree(root->right, code, codes);
}
if (root->left == NULL && root->right == NULL) {
int index = root->character - 'a';
codes[index] = malloc(strlen(code) + 1);
strcpy(codes[index], code);
}
code[strlen(code) - 1] = '\0';
}
void encode_text(char *filename, char **codes, char *encoded_text) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
printf("Error: could not open file %s\n", filename);
exit(1);
}
char current_character;
while ((current_character = fgetc(file)) != EOF) {
if (current_character >= 'a' && current_character <= 'z') {
strcat(encoded_text, codes[current_character - 'a']);
} else if (current_character >= 'A' && current_character <= 'Z') {
strcat(encoded_text, codes[current_character - 'A']);
}
}
fclose(file);
}
void write_encoded_text_to_file(char *filename, char *encoded_text) {
FILE *file = fopen(filename, "w");
if (file == NULL) {
printf("Error: could not open file %s\n", filename);
exit(1);
}
fprintf(file, "%s", encoded_text);
fclose(file);
}
int main() {
char *filename = "a.txt";
int frequencies[MAX_CHARACTERS] = {0};
get_frequencies(filename, frequencies);
node *root = build_huffman_tree(frequencies);
char code[MAX_CHARACTERS] = {'\0'};
char *codes[MAX_CHARACTERS];
encode_huffman_tree(root, code, codes);
char *encoded_text = malloc(1000 * sizeof(char));
encoded_text[0] = '\0';
encode_text(filename, codes, encoded_text);
char *output_filename = "b.txt";
write_encoded_text_to_file(output_filename, encoded_text);
return 0;
}
```
这段代码可以统计短文中每个字母出现的次数,然后以字母出现次数作为权值,建立Huffman树,并且输出每个字母的Huffman编码。最后,它会对输入的短文进行编码,并将编码后的结果输出到一个文件中。
阅读全文