C语言写一个基于哈夫曼编码的压缩软件
时间: 2023-07-19 09:05:35 浏览: 138
哈夫曼编码是一种无损压缩算法,可以将任意一种数据转换为只含有0和1的编码,使得编码后的数据长度更短,从而达到压缩的目的。下面是一个用C语言实现基于哈夫曼编码的压缩软件的简单示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TREE_HT 100
#define MAX_CHAR 256
struct MinHeapNode {
char data;
unsigned freq;
struct MinHeapNode *left, *right;
};
struct MinHeap {
unsigned size;
unsigned capacity;
struct MinHeapNode **array;
};
struct MinHeapNode *newNode(char data, unsigned freq) {
struct MinHeapNode *temp = (struct MinHeapNode *)malloc(sizeof(struct MinHeapNode));
temp->left = temp->right = NULL;
temp->data = data;
temp->freq = freq;
return temp;
}
struct MinHeap *createMinHeap(unsigned capacity) {
struct MinHeap *minHeap = (struct MinHeap *)malloc(sizeof(struct MinHeap));
minHeap->size = 0;
minHeap->capacity = capacity;
minHeap->array = (struct MinHeapNode **)malloc(minHeap->capacity * sizeof(struct MinHeapNode *));
return minHeap;
}
void swapMinHeapNode(struct MinHeapNode **a, struct MinHeapNode **b) {
struct MinHeapNode *t = *a;
*a = *b;
*b = t;
}
void minHeapify(struct MinHeap *minHeap, int idx) {
int smallest = idx;
int left = 2 * idx + 1;
int right = 2 * idx + 2;
if (left < minHeap->size && minHeap->array[left]->freq < minHeap->array[smallest]->freq)
smallest = left;
if (right < minHeap->size && minHeap->array[right]->freq < minHeap->array[smallest]->freq)
smallest = right;
if (smallest != idx) {
swapMinHeapNode(&minHeap->array[smallest], &minHeap->array[idx]);
minHeapify(minHeap, smallest);
}
}
int isSizeOne(struct MinHeap *minHeap) {
return (minHeap->size == 1);
}
struct MinHeapNode *extractMin(struct MinHeap *minHeap) {
struct MinHeapNode *temp = minHeap->array[0];
minHeap->array[0] = minHeap->array[minHeap->size - 1];
--minHeap->size;
minHeapify(minHeap, 0);
return temp;
}
void insertMinHeap(struct MinHeap *minHeap, struct MinHeapNode *minHeapNode) {
++minHeap->size;
int i = minHeap->size - 1;
while (i && minHeapNode->freq < minHeap->array[(i - 1) / 2]->freq) {
minHeap->array[i] = minHeap->array[(i - 1) / 2];
i = (i - 1) / 2;
}
minHeap->array[i] = minHeapNode;
}
void buildMinHeap(struct MinHeap *minHeap) {
int n = minHeap->size - 1;
int i;
for (i = (n - 1) / 2; i >= 0; --i)
minHeapify(minHeap, i);
}
void printArr(int arr[], int n) {
int i;
for (i = 0; i < n; ++i)
printf("%d", arr[i]);
printf("\n");
}
int isLeaf(struct MinHeapNode *root) {
return !(root->left) && !(root->right);
}
struct MinHeap *createAndBuildMinHeap(char data[], int freq[], int size) {
struct MinHeap *minHeap = createMinHeap(size);
for (int i = 0; i < size; ++i)
minHeap->array[i] = newNode(data[i], freq[i]);
minHeap->size = size;
buildMinHeap(minHeap);
return minHeap;
}
struct MinHeapNode *buildHuffmanTree(char data[], int freq[], int size) {
struct MinHeapNode *left, *right, *top;
struct MinHeap *minHeap = createAndBuildMinHeap(data, freq, size);
while (!isSizeOne(minHeap)) {
left = extractMin(minHeap);
right = extractMin(minHeap);
top = newNode('$', left->freq + right->freq);
top->left = left;
top->right = right;
insertMinHeap(minHeap, top);
}
return extractMin(minHeap);
}
void printCodes(struct MinHeapNode *root, int arr[], int top) {
if (root->left) {
arr[top] = 0;
printCodes(root->left, arr, top + 1);
}
if (root->right) {
arr[top] = 1;
printCodes(root->right, arr, top + 1);
}
if (isLeaf(root)) {
printf("%c: ", root->data);
printArr(arr, top);
}
}
void HuffmanCodes(char data[], int freq[], int size) {
struct MinHeapNode *root = buildHuffmanTree(data, freq, size);
int arr[MAX_TREE_HT], top = 0;
printCodes(root, arr, top);
}
void compressFile(char *inputFile, char *outputFile) {
FILE *fpIn = fopen(inputFile, "rb");
FILE *fpOut = fopen(outputFile, "wb");
if (fpIn == NULL || fpOut == NULL) {
printf("Error opening files!\n");
return;
}
// 统计每个字符出现的频率
int charFreq[MAX_CHAR] = {0};
char c;
while ((c = fgetc(fpIn)) != EOF)
++charFreq[c];
// 构建哈夫曼树并打印编码
HuffmanCodes((char *)charFreq, charFreq, MAX_CHAR);
rewind(fpIn);
// 写入编码后的文件
int bitCount = 0;
unsigned char buffer = 0;
struct MinHeapNode *root = buildHuffmanTree((char *)charFreq, charFreq, MAX_CHAR);
while ((c = fgetc(fpIn)) != EOF) {
int arr[MAX_TREE_HT] = {0};
int top = 0;
printCodes(root, arr, top);
for (int i = 0; i < top; ++i) {
if (arr[i] == 0)
buffer = buffer << 1;
else
buffer = (buffer << 1) | 1;
++bitCount;
if (bitCount == 8) {
fwrite(&buffer, sizeof(buffer), 1, fpOut);
bitCount = 0;
buffer = 0;
}
}
}
if (bitCount > 0) {
buffer = buffer << (8 - bitCount);
fwrite(&buffer, sizeof(buffer), 1, fpOut);
}
fclose(fpIn);
fclose(fpOut);
}
void decompressFile(char *inputFile, char *outputFile) {
FILE *fpIn = fopen(inputFile, "rb");
FILE *fpOut = fopen(outputFile, "wb");
if (fpIn == NULL || fpOut == NULL) {
printf("Error opening files!\n");
return;
}
// 读取哈夫曼编码
int charFreq[MAX_CHAR] = {0};
char c;
while ((c = fgetc(fpIn)) != EOF) {
++charFreq[c];
}
struct MinHeapNode *root = buildHuffmanTree((char *)charFreq, charFreq, MAX_CHAR);
// 解压文件
struct MinHeapNode *p = root;
while ((c = fgetc(fpIn)) != EOF) {
for (int i = 7; i >= 0; --i) {
int bit = (c >> i) & 1;
if (bit == 0)
p = p->left;
else
p = p->right;
if (isLeaf(p)) {
fwrite(&p->data, sizeof(p->data), 1, fpOut);
p = root;
}
}
}
fclose(fpIn);
fclose(fpOut);
}
int main() {
compressFile("input.txt", "output.bin");
decompressFile("output.bin", "output.txt");
return 0;
}
```
上述代码中,`compressFile`函数实现了文件压缩,`decompressFile`函数实现了文件解压缩。在压缩文件过程中,首先统计每个字符出现的频率,然后构建哈夫曼树并打印每个字符的编码。接着读取输入文件中的每个字符,根据字符的编码将其转换为0和1,并将其写入输出文件。在解压缩文件过程中,首先读取输入文件中的哈夫曼编码,然后根据编码重新构建哈夫曼树。接着读取输入文件中的每个字节,并将每个字节中的每个位按照哈夫曼编码逐个转换为字符写入输出文件。
阅读全文