資源簡介
哈夫曼編解碼器
問題描述:使用哈夫曼編碼,實現文本文件的編碼和解碼,具體要求如下: ① 文本文件 data.txt 中僅包含 ASCII 字符,總字符數不少于 1000。 ② 統計 data.txt 中各種字符的出現次數,并計算各自概率(出現次數/總字符數)。 ③ 以概率作為葉結點權值,構造哈夫曼樹,并求出每個葉結點對應的哈夫曼編碼。 ④ 對 data.txt 進行編碼,結果存于 encode.txt。 ⑤ 對 encode.txt 進行解碼,結果存于 decode.txt。 ⑥ 編寫代碼比較 data.txt 與 decode.txt 的文件內容是否一致。 涉及算法及知識:哈夫曼樹、哈弗曼
代碼片段和文件信息
#include
#include
#include
#include?
using?namespace?std;
typedef?struct{
int?weight;
int?lchild?rchild?parent;
}HTNode?*HuffmanTree;
typedef?char**?HuffmanCode;
struct?letter{
char?data;
int?count;
char*?code;
}let[300];
string?decode;
//在HT[1...end]中選擇2個根結點權值最小的樹,s1、s2為這2棵樹在HT數組中的下標
void?Select(HuffmanTree?HT?int?end?int?&s1?int?&s2){
unsigned?int?min?=?UINT_MAX;?//?安全起見,循環前將min設為很大的值
for?(int?i?=?1;?i?<=?end;?i++){
if?(HT[i].parent?==?0?&&?(HT[i].weight s1?=?i;
min?=?HT[i].weight;
}
}
min?=?UINT_MAX;
for?(?i?=?1;?i?<=?end;?i++){
if?(HT[i].parent?==?0?&&?(HT[i].weight? s2?=?i;
min?=?HT[i].weight;
}
}
}
//編碼
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????2000??2018-01-09?14:48??哈夫曼課程設計\data.txt
?????文件??????99328??2018-01-11?15:16??哈夫曼課程設計\Debug\vc60.idb
?????文件?????126976??2018-01-10?16:58??哈夫曼課程設計\Debug\vc60.pdb
?????文件?????569422??2018-01-11?15:16??哈夫曼課程設計\Debug\哈夫曼設計.exe
?????文件?????817912??2018-01-11?15:16??哈夫曼課程設計\Debug\哈夫曼設計.ilk
?????文件?????334855??2018-01-11?15:15??哈夫曼課程設計\Debug\哈夫曼設計.obj
?????文件????2253648??2018-01-09?14:05??哈夫曼課程設計\Debug\哈夫曼設計.pch
?????文件????1147904??2018-01-10?16:58??哈夫曼課程設計\Debug\哈夫曼設計.pdb
?????文件???????2000??2018-01-11?15:16??哈夫曼課程設計\decode.txt
?????文件??????13169??2018-01-11?15:16??哈夫曼課程設計\encode.txt
?????文件???????5420??2018-01-10?16:58??哈夫曼課程設計\哈夫曼設計.cpp
?????文件???????3451??2018-01-11?14:36??哈夫曼課程設計\哈夫曼設計.dsp
?????文件????????528??2018-01-11?17:02??哈夫曼課程設計\哈夫曼設計.dsw
?????文件??????41984??2018-01-11?17:02??哈夫曼課程設計\哈夫曼設計.ncb
?????文件??????48640??2018-01-11?17:02??哈夫曼課程設計\哈夫曼設計.opt
?????文件????????768??2018-01-11?15:16??哈夫曼課程設計\哈夫曼設計.plg
?????目錄??????????0??2018-01-10?16:58??哈夫曼課程設計\Debug
?????目錄??????????0??2018-01-11?17:02??哈夫曼課程設計
-----------?---------??----------?-----??----
??????????????5468005????????????????????18
- 上一篇:數據結構課程設計城市鏈表
- 下一篇:pmp_章節習題
評論
共有 條評論