使用哈夫曼压缩函数压缩一个文件并输出保存c++、实现
时间: 2024-01-27 20:06:09 浏览: 29
好的,我可以为您提供一个简单的C++程序,用于使用哈夫曼编码压缩文件并保存输出。以下是程序代码:
```c++
#include <iostream>
#include <fstream>
#include <queue>
#include <unordered_map>
using namespace std;
// 构建哈夫曼树节点
struct HuffmanNode {
char ch; // 字符
int freq; // 出现频率
HuffmanNode *left, *right; // 左右子节点
HuffmanNode(char c, int f) : ch(c), freq(f), left(nullptr), right(nullptr) {}
~HuffmanNode() { delete left; delete right; }
};
// 比较函数,用于优先队列排序
struct cmp {
bool operator() (const HuffmanNode* a, const HuffmanNode* b) {
return a->freq > b->freq;
}
};
// 哈夫曼编码
void huffmanEncoding(HuffmanNode* root, string code, unordered_map<char, string>& umap) {
if (!root) return;
if (root->ch) umap[root->ch] = code;
huffmanEncoding(root->left, code + "0", umap);
huffmanEncoding(root->right, code + "1", umap);
}
// 哈夫曼压缩函数
void huffmanCompress(string inputFile, string outputFile) {
// 统计字符出现频率
unordered_map<char, int> freq;
ifstream fin(inputFile);
char c;
while (fin.get(c)) freq[c]++;
// 构建哈夫曼树
priority_queue<HuffmanNode*, vector<HuffmanNode*>, cmp> pq;
for (auto& p : freq) pq.push(new HuffmanNode(p.first, p.second));
while (pq.size() > 1) {
HuffmanNode *left = pq.top(); pq.pop();
HuffmanNode *right = pq.top(); pq.pop();
pq.push(new HuffmanNode('\0', left->freq + right->freq));
}
HuffmanNode *root = pq.top();
// 构建哈夫曼编码表
unordered_map<char, string> umap;
huffmanEncoding(root, "", umap);
// 将编码写入输出文件
ofstream fout(outputFile);
fout << umap.size() << endl;
for (auto& p : umap) fout << p.first << " " << p.second << endl;
// 将压缩后的数据写入输出文件
fin.clear(); fin.seekg(0); // 重置文件指针
string s = "";
while (fin.get(c)) s += umap[c];
int n = s.size(), i = 0;
while (i < n) {
int len = min(8, n - i);
char byte = 0;
for (int j = 0; j < len; ++j) {
byte <<= 1;
if (s[i + j] == '1') byte |= 1;
}
fout.put(byte);
i += len;
}
// 释放内存
delete root;
}
int main() {
string inputFile = "input.txt";
string outputFile = "output.txt";
huffmanCompress(inputFile, outputFile);
return 0;
}
```
在这个程序中,我们通过哈夫曼编码压缩输入文件,并将编码表和压缩后的数据写入输出文件。您可以将输入文件名和输出文件名更改为自己想要压缩的文件。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)