




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、哈夫曼樹的壓縮與解壓1. 算法簡要描述1.哈夫曼算法1. 哈弗曼算法是根據(jù)給定的n個(gè)權(quán)值w1,w2,w3.wn,構(gòu)造由n棵二叉樹構(gòu)成的深林F=T1,T2,。Tn,其中每個(gè)二叉樹Ti分別都是只含有一個(gè)權(quán)值wi的根結(jié)點(diǎn),其左右子樹為空(i=1,,2)。2. 在深林F中選取其根結(jié)點(diǎn)的權(quán)值最小的兩棵二叉樹,分別作其左右子樹構(gòu)造一顆新的二叉樹,并置這棵新的二叉樹根結(jié)點(diǎn)的權(quán)值為其左右子樹的根結(jié)點(diǎn)之和。3. 從F中刪去這兩棵二叉樹,同時(shí)剛新生成的二叉樹加入到深林F中。4. 重復(fù)2,3,步驟,直至深林F中只含有一顆二叉樹為止。2.哈夫曼樹的實(shí)現(xiàn)函數(shù)String EnCode(Char Type ch):表示哈
2、夫曼樹已存在,返回字符ch的編碼。函數(shù)LinkList<CharType>UnCode(String strCode):表示對(duì)哈夫曼樹進(jìn)行譯碼,返回編碼前的字符序列。根據(jù)算法可以看出,在具有n個(gè)結(jié)點(diǎn)權(quán)值的哈夫曼樹的構(gòu)造過程中 ,每次都是從F中刪去兩棵樹,增加一棵樹,即每次結(jié)束后減少一棵樹,經(jīng)過n-1次處理后,F(xiàn)中就只剩下一棵樹了。另外,每次合并都要產(chǎn)生一個(gè)新的結(jié)點(diǎn),合并n-1次后共產(chǎn)生了n-1個(gè)新結(jié)點(diǎn),并且這n-1個(gè)新節(jié)點(diǎn)都是具有左右子樹的分支結(jié)點(diǎn)。則最終得到的哈夫曼樹中共有2n-1個(gè)結(jié)點(diǎn),并且其中沒有度為1的分支結(jié)點(diǎn),最后一次產(chǎn)生的新結(jié)點(diǎn)就是哈夫曼樹的根結(jié)點(diǎn)。源代碼中創(chuàng)建了一個(gè)哈
3、夫曼樹結(jié)點(diǎn)類,其中有數(shù)據(jù)成員weight,parent,leftChild,rightChild分別代表了權(quán)值,雙親,左孩子,右孩子。在哈夫曼樹類中有數(shù)據(jù)成員*nodes,*LeafChars,*LeafCharCodes,curPos,num,分別用來存儲(chǔ)結(jié)點(diǎn)信息,葉結(jié)點(diǎn)字符信息,葉結(jié)點(diǎn)字符編碼信息,譯碼時(shí)從根結(jié)點(diǎn)到葉結(jié)點(diǎn)路徑的當(dāng)前結(jié)點(diǎn),葉結(jié)點(diǎn)個(gè)數(shù)。哈夫曼樹類中含有多個(gè)函數(shù),有構(gòu)造函數(shù),析構(gòu)函數(shù)等。由函數(shù)HuffmanTree(CharType ch,WeightType w,int n)來構(gòu)造由字符,權(quán)值,和字符個(gè)數(shù)構(gòu)造哈夫曼樹,在根據(jù)哈夫曼算法很容易實(shí)現(xiàn)哈夫曼類的函數(shù)以及構(gòu)造函數(shù)。在在算
4、法中,求葉結(jié)點(diǎn)字符的編碼時(shí),需要從葉結(jié)點(diǎn)出發(fā)走一條從高葉結(jié)點(diǎn)到根結(jié)點(diǎn)的路徑,而編碼卻是從根結(jié)點(diǎn)出發(fā)到葉結(jié)點(diǎn)的路徑,由左分支為編碼0,右分支為編碼1,得到的編碼,因此從葉結(jié)點(diǎn)出發(fā)到根結(jié)點(diǎn)的路徑得到的編碼是實(shí)際編碼的逆序,并且編碼長度不確定,又由于可以再線性鏈表中構(gòu)造串,因此將編碼的信息儲(chǔ)存在一個(gè)線性立案標(biāo)準(zhǔn),每得到一位編碼都將其插入在線性鏈表的最前面。在求某個(gè)字符的編碼是由函數(shù)EnCode(CharType ch)來求,返回字符編碼。在進(jìn)行譯碼時(shí),用一個(gè)線性鏈表存儲(chǔ)字符序列,由函數(shù)Decode(String strCode)來求,對(duì)編碼串strCode進(jìn)行譯碼,返回編碼前的字符序列。函數(shù)Comp
5、ress()用哈夫曼編碼壓縮文件。函數(shù)Decompress()解壓縮用哈夫曼編碼壓縮的文件。1 / 14在主函數(shù)中有兩個(gè)選項(xiàng),一個(gè)是選擇編碼壓縮,一個(gè)是解壓。在函數(shù)中使用了文件輸入輸出流,我們可以選擇要壓縮的文件名輸入,在選出壓縮文件保存的地方和文件類型,將壓縮所得到的文件存儲(chǔ)在另一個(gè)文件中,解壓也是如此。2. 源代碼#ifndef _HUFFMAN_TREE_NODE_H_#define _HUFFMAN_TREE_NODE_H_/ 哈夫曼樹結(jié)點(diǎn)類模板template <class WeightType>struct HuffmanTreeNodeWeightType weigh
6、t;/ 權(quán)數(shù)據(jù)域unsigned int parent, leftChild, rightChild;/ 雙親,左右孩子域HuffmanTreeNode();/ 無參數(shù)的構(gòu)造函數(shù)模板HuffmanTreeNode(WeightType w, int p = 0, int lChild = 0, int rChild = 0);/ 已知權(quán),雙親及左右孩子構(gòu)造結(jié)構(gòu);/ 哈夫曼樹結(jié)點(diǎn)類模板的實(shí)現(xiàn)部分template <class WeightType>HuffmanTreeNode<WeightType>:HuffmanTreeNode()/ 操作結(jié)果:構(gòu)造空結(jié)點(diǎn)parent
7、 = leftChild = rightChild = 0;template <class WeightType>HuffmanTreeNode<WeightType>:HuffmanTreeNode(WeightType w, int p, int lChild, int rChild)/ 操作結(jié)果:構(gòu)造一個(gè)權(quán)為w,雙親為p,左孩子為lChild,右孩子為rChild的結(jié)點(diǎn)weight = w;/ 權(quán)parent = p;/ 雙親leftChild = lChild;/ 左孩子rightChild = rChild;/ 右孩子#endif#ifndef _HUFFMA
8、N_TREE_H_#define _HUFFMAN_TREE_H_#include "string.h"/ 串類#include "huffman_tree_node.h"/ 哈夫曼樹結(jié)點(diǎn)類模板/ 哈夫曼樹類模板template <class CharType, class WeightType>class HuffmanTreeprotected:HuffmanTreeNode<WeightType> *nodes;/ 存儲(chǔ)結(jié)點(diǎn)信息,nodes0未用CharType *LeafChars;/ 葉結(jié)點(diǎn)字符信息,LeafChars0
9、未用String *LeafCharCodes;/ 葉結(jié)點(diǎn)字符編碼信息,LeafCharCodes0未用int curPos;/ 譯碼時(shí)從根結(jié)點(diǎn)到葉結(jié)點(diǎn)路徑的當(dāng)前結(jié)點(diǎn)int num;/ 葉結(jié)點(diǎn)個(gè)數(shù)/輔助函數(shù)模板:void Select(int cur, int &r1, int &r2);/ nodes1 cur中選擇雙親為,權(quán)值最小的兩個(gè)結(jié)點(diǎn)r1,r2void CreatHuffmanTree(CharType ch, WeightType w, int n);/ 由字符,權(quán)值和字符個(gè)數(shù)構(gòu)造哈夫曼樹public:/ 哈夫曼樹方法聲明及重載編譯系統(tǒng)默認(rèn)方法聲明:HuffmanT
10、ree(CharType ch, WeightType w, int n);/ 由字符,權(quán)值和字符個(gè)數(shù)構(gòu)造哈夫曼樹virtual HuffmanTree();/ 析構(gòu)函數(shù)模板String Encode(CharType ch);/ 編碼LinkList<CharType> Decode(String strCode);/ 譯碼HuffmanTree(const HuffmanTree<CharType, WeightType> ©);/ 復(fù)制構(gòu)造函數(shù)模板HuffmanTree<CharType, WeightType> &operat
11、or=(const HuffmanTree<CharType, WeightType>& copy);/ 重載賦值運(yùn)算符;/ 孩子兄弟表示哈夫曼樹類模板的實(shí)現(xiàn)部分template <class CharType, class WeightType>void HuffmanTree<CharType, WeightType>:Select(int cur, int &r1, int &r2)/ 操作結(jié)果:nodes1 cur中選擇雙親為,權(quán)值最小的兩個(gè)結(jié)點(diǎn)r1,r2r1 = r2 = 0;/ 0表示空結(jié)點(diǎn)for (int pos = 1
12、; pos <= cur; pos+)/ 查找樹值最小的兩個(gè)結(jié)點(diǎn)if (nodespos.parent != 0) continue;/ 只處理雙親不為的結(jié)點(diǎn)if (r1 = 0)r1 = pos; / r1為空,將pos賦值給r1else if (r2 = 0)r2 = pos; / r2為空,將pos賦值給r2else if(nodespos.weight < nodesr1.weight)r1 = pos; / nodespos權(quán)值比nodesr1更小,將pos賦為r1else if (nodespos.weight < nodesr2.weight)r2 = pos;
13、 / nodespos權(quán)值比nodesr2更小,將pos賦為r2template <class CharType, class WeightType>void HuffmanTree<CharType, WeightType>:CreatHuffmanTree(CharType ch, WeightType w, int n)/ 操作結(jié)果:由字符,權(quán)值和字符個(gè)數(shù)構(gòu)造哈夫曼樹num = n;/ 葉結(jié)點(diǎn)個(gè)數(shù)int m = 2 * n - 1;/ 結(jié)點(diǎn)個(gè)數(shù)nodes = new HuffmanTreeNode<WeightType>m + 1;/ nodes0未用
14、LeafChars = new CharTypen + 1;/ LeafChars0未用LeafCharCodes = new Stringn + 1;/ LeafCharCodes0未用 int pos;/ 臨時(shí)變量for (pos = 1; pos <= n; pos+)/ 存儲(chǔ)葉結(jié)點(diǎn)信息nodespos.weight = wpos - 1;/ 權(quán)值LeafCharspos = chpos - 1;/ 字符for (pos = n + 1; pos <= m; pos+)/ 建立哈夫曼樹int r1, r2;Select(pos - 1, r1, r2);/ nodes1 po
15、s - 1中選擇雙親為,權(quán)值最小的兩個(gè)結(jié)點(diǎn)r1,r2/ 合并以r1,r2為根的樹nodesr1.parent = nodesr2.parent = pos;/ r1,r2雙親為posnodespos.leftChild = r1;/ r1為pos的左孩子nodespos.rightChild = r2;/ r2為pos的右孩子nodespos.weight = nodesr1.weight + nodesr2.weight;/pos的權(quán)為r1,r2的權(quán)值之和for (pos = 1; pos <= n; pos+)/ 求n個(gè)葉結(jié)點(diǎn)字符的編碼LinkList<char> cha
16、rCode;/ 暫存葉結(jié)點(diǎn)字符編碼信息for (unsigned int child = pos, parent = nodeschild.parent; parent != 0; child = parent, parent = nodeschild.parent)/ 從葉結(jié)點(diǎn)到根結(jié)點(diǎn)逆向求編碼if (nodesparent.leftChild = child) charCode.Insert(1, '0');/ 左分支編碼為'0'else charCode.Insert(1, '1');/ 右分支編碼為'1'LeafCharC
17、odespos = charCode;/ charCode中存儲(chǔ)字符編碼curPos = m;/ 譯碼時(shí)從根結(jié)點(diǎn)開始,m為根template <class CharType, class WeightType>HuffmanTree<CharType, WeightType>:HuffmanTree(CharType ch, WeightType w, int n)/ 操作結(jié)果:由字符,權(quán)值和字符個(gè)數(shù)構(gòu)造哈夫曼樹CreatHuffmanTree(ch, w, n);/ 由字符,權(quán)值和字符個(gè)數(shù)構(gòu)造哈夫曼樹template <class CharType, class
18、 WeightType>HuffmanTree<CharType, WeightType>:HuffmanTree()/ 操作結(jié)果:銷毀哈夫曼樹if (nodes != NULL) delete nodes;/ 釋放結(jié)點(diǎn)信息if (LeafChars != NULL) delete LeafChars;/ 釋放葉結(jié)點(diǎn)字符信息if (LeafCharCodes != NULL) delete LeafCharCodes;/ 釋放葉結(jié)點(diǎn)字符編碼信息template <class CharType, class WeightType>String HuffmanTree
19、<CharType, WeightType>:Encode(CharType ch)/ 操作結(jié)果:返回字符編碼for (int pos = 1; pos <= num; pos+)/ 查找字符的位置if (LeafCharspos = ch) return LeafCharCodespos;/ 找到字符,得到編碼throw Error("非法字符,無法編碼!");/ 拋出異常template <class CharType, class WeightType>LinkList<CharType> HuffmanTree<Cha
20、rType, WeightType>:Decode(String strCode)/ 操作結(jié)果:對(duì)編碼串strCode進(jìn)行譯碼,返回編碼前的字符序列LinkList<CharType> charList;/ 編碼前的字符序列for (int pos = 0; pos < strCode.Length(); pos+)/ 處理每位編碼if (strCodepos = '0') curPos = nodescurPos.leftChild;/ '0'表示左分支else curPos = nodescurPos.rightChild;/
21、9;1'表示左分支if (nodescurPos.leftChild = 0 && nodescurPos.rightChild = 0)/ 譯碼時(shí)從根結(jié)點(diǎn)到葉結(jié)點(diǎn)路徑的當(dāng)前結(jié)點(diǎn)為葉結(jié)點(diǎn)charList.Insert(charList.Length() + 1, LeafCharscurPos);curPos = 2 * num - 1;/ curPos回歸根結(jié)點(diǎn)return charList;/ 返回編碼前的字符序列template <class CharType, class WeightType>HuffmanTree<CharType, Wei
22、ghtType>:HuffmanTree(const HuffmanTree<CharType, WeightType> ©)/ 操作結(jié)果:由哈夫曼樹copy構(gòu)造新哈夫曼樹復(fù)制構(gòu)造函數(shù)模板num = copy.num;/ 葉結(jié)點(diǎn)個(gè)數(shù)curPos = copy.curPos;/ 譯碼時(shí)從根結(jié)點(diǎn)到葉結(jié)點(diǎn)路徑的當(dāng)前結(jié)點(diǎn)int m = 2 * num - 1;/ 結(jié)點(diǎn)總數(shù)nodes = new HuffmanTreeNode<WeightType>m + 1;/ nodes0未用LeafChars = new CharTypenum + 1;/ LeafC
23、hars0未用LeafCharCodes = new Stringnum + 1;/ LeafCharCodes0未用int pos;/ 臨時(shí)變量for (pos = 1; pos <= m; pos+)/ 復(fù)制結(jié)點(diǎn)信息nodespos = copy.nodespos;/ 結(jié)點(diǎn)信息for (pos = 1; pos <= num; pos+)/ 復(fù)制葉結(jié)點(diǎn)字符信息與葉結(jié)點(diǎn)字符編碼信息LeafCharspos = copy.LeafCharspos;/ 葉結(jié)點(diǎn)字符信息LeafCharCodespos = copy.LeafCharCodespos;/ 葉結(jié)點(diǎn)字符編碼信息templat
24、e <class CharType, class WeightType>HuffmanTree<CharType, WeightType> &HuffmanTree<CharType, WeightType>:operator=(const HuffmanTree<CharType, WeightType>& copy)/ 操作結(jié)果:將哈夫曼樹copy賦值給當(dāng)前哈夫曼樹重載賦值運(yùn)算符if (© != this)if (nodes != NULL) delete nodes;/ 釋放結(jié)點(diǎn)信息if (LeafChars
25、 != NULL) delete LeafChars;/ 釋放葉結(jié)點(diǎn)字符信息if (LeafCharCodes != NULL) delete LeafCharCodes;/ 釋放葉結(jié)點(diǎn)字符編碼信息num = copy.num;/ 葉結(jié)點(diǎn)個(gè)數(shù)curPos = copy.curPos;/ 譯碼時(shí)從根結(jié)點(diǎn)到葉結(jié)點(diǎn)路徑的當(dāng)前結(jié)點(diǎn)int m = 2 * num - 1;/ 結(jié)點(diǎn)總數(shù)nodes = new HuffmanTreeNode<WeightType>m + 1;/ nodes0未用LeafChars = new CharTypenum + 1;/ LeafChars0未用LeafC
26、harCodes = new Stringnum + 1;/ LeafCharCodes0未用int pos;/ 臨時(shí)變量for (pos = 1; pos <= m; pos+)/ 復(fù)制結(jié)點(diǎn)信息nodespos = copy.nodespos;/ 結(jié)點(diǎn)信息for (pos = 1; pos <= num; pos+)/ 復(fù)制葉結(jié)點(diǎn)字符信息與葉結(jié)點(diǎn)字符編碼信息LeafCharspos = copy.LeafCharspos;/ 葉結(jié)點(diǎn)字符信息LeafCharCodespos = copy.LeafCharCodespos;/ 葉結(jié)點(diǎn)字符編碼信息return *this;#endif
27、#ifndef _HUFFMAN_COMPRESS_H_#define _HUFFMAN_COMPRESS_H_#include "huffman_tree.h"/ 哈夫曼樹類struct BufferType/ 字符緩存器char ch;/ 字符unsigned int bits;/ 實(shí)際比特?cái)?shù) ;class HuffmanCompress/ 哈夫曼壓縮類protected:HuffmanTree<char, unsigned long> *pHuffmanTree;FILE *infp,*outfp;/ 輸入/出文件BufferType buf;/ 字符緩存
28、void Write(unsigned int bit);/ 向目標(biāo)文件中寫入一個(gè)比特void WriteToOutfp();/ 強(qiáng)行將字符緩存寫入目標(biāo)文件public:HuffmanCompress();/ 無參數(shù)的構(gòu)造函數(shù)HuffmanCompress();/ 析構(gòu)函數(shù)void Compress();/ 壓縮算法void Decompress();/ 解壓縮算法HuffmanCompress(const HuffmanCompress ©);/ 復(fù)制構(gòu)造函數(shù)HuffmanCompress &operator=(const HuffmanCompress& c
29、opy);/ 賦值運(yùn)算符;HuffmanCompress:HuffmanCompress()pHuffmanTree = NULL;/ 空哈夫曼樹HuffmanCompress:HuffmanCompress()if (pHuffmanTree != NULL) delete pHuffmanTree;/ 釋放空間void HuffmanCompress:Write(unsigned int bit)/ 操作結(jié)果:向目標(biāo)文件中寫入一個(gè)比特buf.bits+;/ 緩存比特?cái)?shù)自增buf.ch = (buf.ch << 1) | bit;/ 將bit加入到緩存字符中if (buf.bit
30、s = 8)/ 緩存區(qū)已滿,寫入目標(biāo)文件fputc(buf.ch, outfp);/ 寫入目標(biāo)文件buf.bits = 0;/ 初始化bitsbuf.ch = 0;/ 初始化chvoid HuffmanCompress:WriteToOutfp()/ 操作結(jié)果:強(qiáng)行將字符緩存寫入目標(biāo)文件unsigned int len = buf.bits;/ 緩存實(shí)際比特?cái)?shù)if (len > 0)/ 緩存非空, 將緩存的比特個(gè)數(shù)增加到, 自動(dòng)寫入目標(biāo)文件for (unsigned int i = 0; i < 8 - len; i+) Write(0);void HuffmanCompress:
31、Compress()/ 操作結(jié)果:用哈夫曼編碼壓縮文件char infName256, outfName256;/ 輸入(源)/出(目標(biāo))文件名cout << "請(qǐng)輸入源文件名(文件小于GB):"/ 被壓縮文件小于GBcin >> infName;/ 輸入源文件名if (infp = fopen(infName, "r+b") = NULL)throw Error("打開源文件失敗!");/ 拋出異常 fgetc(infp);/ 取出源文件第一個(gè)字符if (feof(infp)throw Error("
32、;空源文件!");/ 拋出異常cout << "請(qǐng)輸入目標(biāo)文件:"cin >> outfName; if (outfp = fopen(outfName, "w+b") = NULL)throw Error("打開目標(biāo)文件失敗!");/ 拋出異常cout << "正在處理,請(qǐng)稍候." << endl;const unsigned long n = 256;/ 字符個(gè)數(shù) char chn;/ 字符數(shù)組unsigned long wn;/ 字符出現(xiàn)頻度(權(quán))un
33、signed long i, size = 0;char cha;for (i = 0; i < n; i+)/ 初始化ch和wchi = (char)i;/ 初始化chiwi = 0;/ 初始化wirewind(infp);/ 使源文件指針指向文件開始處cha = fgetc(infp);/ 取出源文件第一個(gè)字符while (!feof(infp)/ 統(tǒng)計(jì)字符出現(xiàn)頻度w(unsigned char)cha+;/ 字符cha出現(xiàn)頻度自加size+;/ 文件大小自加cha=fgetc(infp);/ 取出源文件下一個(gè)字符if (pHuffmanTree != NULL) delete pH
34、uffmanTree;/ 釋放空間pHuffmanTree = new HuffmanTree<char, unsigned long>(ch, w, n); / 生成哈夫曼樹 rewind(outfp);/ 使目標(biāo)文件指針指向文件開始處fwrite(&size, sizeof(unsigned long), 1, outfp);/ 向目標(biāo)文件寫入源文件大小for (i = 0; i < n; i+)/ 向目標(biāo)文件寫入字符出現(xiàn)頻度fwrite(&wi, sizeof(unsigned long), 1, outfp);buf.bits = 0;/ 初始化bit
35、sbuf.ch = 0;/ 初始化chrewind(infp);/ 使源文件指針指向文件開始處cha = fgetc(infp);/ 取出源文件的第一個(gè)字符 while (!feof(infp)/ 對(duì)源文件字符進(jìn)行編碼,并將編碼寫入目標(biāo)文件String strTmp = pHuffmanTree->Encode(cha);/ 字符編碼for (i = 0; i<strTmp.Length(); i+)/ 向目標(biāo)文件寫入編碼if (strTmpi = '0') Write(0);/ 向目標(biāo)文件寫入else Write(1);/ 向目標(biāo)文件寫入 cha = fgetc(
36、infp);/ 取出源文件的下一個(gè)字符WriteToOutfp();/ 強(qiáng)行寫入目標(biāo)文件fclose(infp); fclose(outfp);/ 關(guān)閉文件cout << "處理結(jié)束." << endl;void HuffmanCompress:Decompress()/ 操作結(jié)果:解壓縮用哈夫曼編碼壓縮的文件char infName256, outfName256;/ 輸入(壓縮)/出(目標(biāo))文件名cout << "請(qǐng)輸入壓縮文件名:"cin >> infName; if (infp = fopen(in
37、fName, "r+b") = NULL)throw Error("打開壓縮文件失敗!");/ 拋出異常 fgetc(infp);/ 取出壓縮文件第一個(gè)字符if (feof(infp)throw Error("壓縮文件為空!");/ 拋出異常cout << "請(qǐng)輸入目標(biāo)文件名:"cin >> outfName; if (outfp = fopen(outfName, "w+b") = NULL)throw Error("打開目標(biāo)文件失敗!");/ 拋出
38、異常cout << "正在處理,請(qǐng)稍候." << endl;const unsigned long n = 256;/ 字符個(gè)數(shù) char chn;/ 字符數(shù)組unsigned long wn;/ 權(quán)unsigned long i, size = 0;char cha;rewind(infp);/ 使源文件指針指向文件開始處fread(&size, sizeof(unsigned long), 1, infp);/ 讀取目標(biāo)文件的大小for (i = 0; i < n; i+)chi = (char)i;/ 初始化chifread(&a
39、mp;wi, sizeof(unsigned long), 1, infp);/ 讀取字符頻度if (pHuffmanTree != NULL) delete pHuffmanTree;/ 釋放空間pHuffmanTree = new HuffmanTree<char, unsigned long>(ch, w, n); / 生成哈夫曼樹unsigned long len = 0;/ 解壓的字符數(shù)cha = fgetc(infp);/ 取出源文件的第一個(gè)字符while (!feof(infp)/ 對(duì)壓縮文件字符進(jìn)行解碼,并將解碼的字符寫入目標(biāo)文件String strTmp = &q
40、uot;"/ 將cha轉(zhuǎn)換二進(jìn)制形式的串unsigned char c = (unsigned char)cha;/ 將cha轉(zhuǎn)換成unsigned char類型for (i = 0; i < 8; i+)/ 將c轉(zhuǎn)換成二進(jìn)制串if (c < 128) Concat(strTmp, "0");/ 最高位為else Concat(strTmp, "1");/ 最高位為c = c << 1;/ 左移一位LinkList<char> lkText = pHuffmanTree->Decode(strTmp);/ 譯碼String strTemp(lkText);for (i = 1; i<=strTemp.Length(); i+)/ 向目標(biāo)文件寫入字符len+;/ 目標(biāo)文件長度自加fputc(strTempi-1, outfp);/ 將字符寫入目標(biāo)文件中if (len = size) break;/ 解壓完畢退出內(nèi)循環(huán) if (len = size) break;/ 解壓完畢退出外循環(huán)cha = fgetc(infp);/ 取出源文件的下一個(gè)字符fclose(infp); fclose(outfp);/ 關(guān)閉文件cout << "處理結(jié)束
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 人員撤離車輛管理制度
- 大慶醫(yī)學(xué)高等專科學(xué)校《曲式與作品分析》2023-2024學(xué)年第二學(xué)期期末試卷
- 下屬單位財(cái)務(wù)管理制度
- 膿毒血癥急救護(hù)理
- 機(jī)場消防演練課件教學(xué)
- 幼兒園園長2025工作計(jì)劃范文
- 新生兒窒息的定義
- 衛(wèi)生院的工作計(jì)劃4
- 山東中醫(yī)藥大學(xué)《教育評(píng)價(jià)學(xué)》2023-2024學(xué)年第二學(xué)期期末試卷
- 遼寧財(cái)貿(mào)學(xué)院《數(shù)據(jù)分析基礎(chǔ)》2023-2024學(xué)年第二學(xué)期期末試卷
- 2024年廣西壯族自治區(qū)中考地理試題(含解析)
- 2024-2030年牛樟芝行業(yè)市場深度調(diào)研及未來發(fā)展戰(zhàn)略規(guī)劃研究報(bào)告
- 北京市昌平區(qū)2023-2024學(xué)年高一下學(xué)期期末考試地理試題 含解析
- 西方經(jīng)濟(jì)學(xué)考試題庫(含參考答案)
- 2024詳解《鑄牢中華民族共同體意識(shí)》黨課課件
- 國家開放大學(xué)2024春《1379人文英語3》期末考試真題及答案-開放本科
- 2025年高中自主招生模擬考試數(shù)學(xué)試卷試題(含答案詳解)
- 園林綠化樹木的修剪方案
- 國企集團(tuán)公司各崗位廉潔風(fēng)險(xiǎn)點(diǎn)防控表格(廉政)范本
- 吉林鄉(xiāng)土地理知識(shí)要點(diǎn)(試卷)
- 2024年士兵職業(yè)技能鑒定考試-軍械員理論知識(shí)筆試考試歷年高頻考點(diǎn)試題摘選含答案
評(píng)論
0/150
提交評(píng)論