当前位置: 首页 > news >正文

利用huffman树实现对文件A先编码后解码

利用huffman树实现对文件A先编码后解码,范围为ASCII码0-255的值,如何解决特殊符号问题是一个难点,注意应使用unsigned char存储数据,否则ASCII码128-255的值可能会出问题:

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include<map>
#include<vector>
const int N = 1e4;
using namespace std;
struct HuffmanNode
{int data;double weigh;int parent, lchild, rchild;
};
class HuffTree
{
private:vector<HuffmanNode>hufftree;map<int, vector<int>>eachcode;int n;//字符结点数
public:HuffTree() { hufftree.resize(0), n = 0; }void createHuffTree(vector<HuffmanNode>& leafs);~HuffTree();void GetCode(int c);//第i个符号的编码void SelectSmall(int& least, int& less, int i);void Decode(ifstream& is, ofstream& os);void geteachcode();string getcode(int ne);
};
void HuffTree::SelectSmall(int& least, int& less, int i)
{while ((hufftree[least].parent != -1 || hufftree[less].parent != -1)){if ((hufftree[least].parent != -1) || least == less)least++;if ((hufftree[less].parent != -1) || least == less) less++;}if (hufftree[least].weigh > hufftree[less].weigh)swap(least, less);for (int j = min(least, less); j < i; j++){if (j == least || j == less)continue;if (hufftree[j].parent == -1 && hufftree[j].weigh < hufftree[less].weigh){if (hufftree[j].weigh < hufftree[least].weigh){less = least;least = j;}else{less = j;}}}
}
void HuffTree::createHuffTree(vector<HuffmanNode>& leafs)
{n = leafs.size();hufftree.resize(2 * n - 1);for (int i = 0; i < n; i++){hufftree[i].data = leafs[i].data;hufftree[i].weigh = leafs[i].weigh;hufftree[i].lchild = hufftree[i].rchild = hufftree[i].parent = -1;}for (int i = n; i < 2 * n - 1; i++){int least = 0, less = 1;SelectSmall(least, less, i);hufftree[least].parent = hufftree[less].parent = i;hufftree[i].parent = -1;hufftree[i].lchild = least;hufftree[i].rchild = less;hufftree[i].weigh = hufftree[least].weigh + hufftree[less].weigh;}
}
void HuffTree::GetCode(int c)
{int i = 0;for (auto it = hufftree.begin(); it != hufftree.end(); it++){if (hufftree[i].data == c)break;i++;}if (i >= hufftree.size())return;int p = i;int parent = hufftree[i].parent;while (parent != -1){if (hufftree[parent].lchild == p)eachcode[c].insert(eachcode[c].begin(), 0);else eachcode[c].insert(eachcode[c].begin(), 1);p = parent;parent = hufftree[parent].parent;}
}
void HuffTree::geteachcode()
{for (auto it = eachcode.begin(); it != eachcode.end(); it++){cout << it->first << ":";for (int i = 0; i < it->second.size(); i++){cout << it->second[i];}cout << endl;}
}
string HuffTree::getcode(int ne)
{string res;for (int i = 0; i < eachcode[ne].size(); i++){res += to_string(eachcode[ne][i]);}return res;
}
void HuffTree::Decode(ifstream& is, ofstream& os)
{string target = "";int root = hufftree.size() - 1;int p = root;char c;while (is.get(c)){if (c == '0')p = hufftree[p].lchild;else p = hufftree[p].rchild;if (hufftree[p].lchild == -1 && hufftree[p].rchild == -1){unsigned char rchar=hufftree[p].data;os << rchar;p = root;}}
}
HuffTree::~HuffTree()
{}
int main()
{/*srand(time(0));ofstream out("random.txt");if (!out){cerr << "无法打开文件!" << endl;return 1;}for (int i = 0; i < N; i++){unsigned char rchar;rchar = rand() % 256;int data = rchar;out << rchar;}out.close();*/map<int, int>m;ifstream ifs_2("random.txt", ios::binary);char ch_1;while (ifs_2.get(reinterpret_cast<char&>(ch_1))){int data = static_cast<unsigned char>(ch_1);m[data]++;}ifs_2.close();map<int, double>m2;for (auto it = m.begin(); it != m.end(); it++){//m2[it->first] = static_cast<double>(it->second) / N;m2[it->first] = static_cast<double>(it->second);cout << it->first << "频率:" << m2[it->first] << endl;}HuffTree t;vector<HuffmanNode>leafs;leafs.resize(N);int i = 0;for (auto it = m2.begin(); it != m2.end(); it++){leafs[i].data = it->first;leafs[i].weigh = it->second;i++;}t.createHuffTree(leafs);for (int k = 0; k <= 255; k++){t.GetCode(k);}t.geteachcode();ifstream file("random.txt", ios::binary);string buf;char ch;ofstream os("B.txt", ios::binary);while (file.get(ch)){unsigned char uch = static_cast<unsigned char>(ch);int ne = uch;string is = t.getcode(ne);for (int i = 0; i < is.size(); i++){os << is[i];}}os.close();file.close();ofstream ofs("C.txt", ios::binary);ifstream ifs("B.txt", ios::binary);t.Decode(ifs, ofs);ofs.close();ifs.close();cout << "文件A与文件C的比较结果为: ";ifstream fileA("random.txt", ios::binary);ifstream fileC("C.txt", ios::binary);char bufA, bufC;while (fileA.get(bufA) && fileC.get(bufC)){if (bufA != bufC){cout << "不一致" << endl;return 0;}}cout << "一致" << endl;fileA.close();fileC.close();return 0;
}

http://www.lryc.cn/news/482602.html

相关文章:

  • 第三十九章 基于VueCli自定义创建项目
  • 网页web无插件播放器EasyPlayer.js点播播放器遇到视频地址播放不了的现象及措施
  • LLaMA-Factory学习笔记(1)——采用LORA对大模型进行SFT并采用vLLM部署的全流程
  • PHP和Python脚本的性能监测方案
  • C语言实现数据结构之堆
  • 战略共赢 软硬兼备|云途半导体与知从科技达成战略合作
  • python:用 sklearn 构建 K-Means 聚类模型
  • elementUI中2个日期组件实现开始时间、结束时间(禁用日期面板、控制开始时间不能超过结束时间的时分秒)实现方案
  • Oracle 聚集因子factor clustering
  • 【大数据学习 | kafka高级部分】kafka的快速读写
  • 云技术基础
  • 字节序(Byte Order)
  • 融云:社交泛娱乐出海机会尚存,跨境电商异军突起
  • django博客项目实现站内搜索功能
  • 蓝桥杯c++算法学习【1】之枚举与模拟(卡片、回文日期、赢球票、既约分数:::非常典型的比刷例题!!!)
  • Android 延时操作的常用方法
  • AI驱动的轻量级笔记应用Blinko
  • 一文搞懂 UML 类图
  • Zabbix 7 最新版本安装 Rocky Linux 8
  • 使用HTML、CSS和JavaScript创建动态雪人和雪花效果
  • redis bind 127.0.0.1和bind 10.34.56.78的区别
  • 基于点云的 3D 目标检测模型 PointPillars 部署 tensorRT
  • centos查看硬盘资源使用情况命令大全
  • Solon MVC 的 @Mapping 用法说明
  • uni-app表单⑪
  • PyQt5 加载UI界面与资源文件
  • 【MySQL】数据库知识突破:数据类型全解析与详解
  • 使用Golang实现开发中常用的【实例设计模式】
  • 【Java学习】电脑基础操作和编程环境配置
  • AVL树解析