資源描述:
《Huffman壓縮紅樓夢(mèng)源程序》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在工程資料-天天文庫(kù)。
1、Huffman壓縮紅樓夢(mèng)源程序-…計(jì)科4班,張青/*huffman算法壓縮紅樓夢(mèng)*/#include#include#include#include#include#include//時(shí)間統(tǒng)計(jì)頭文件usingnamespacestd;structHuffman_node{//定義huffman數(shù)據(jù)結(jié)構(gòu)類型chara;charstring[20];intfreq_;intlc_;intrc_;};〃函數(shù)申明voidsearch(Huffman_nodehuf
2、f[],intNjnttop[]);voidcode(Huffman_nodehuff[],introot,intp,charstring[],intijnt);voidzipfHuffman_nodehuff[],intN);chartrans(charstring[]);voidrelease(Huffman_nodehuff[],intN);voidtransback(charch,char*);intcheck(Huffman_nodehuff[],charstr[],intN);FILE*infile,*outfile;intmain(){Huffma
3、n_nodehuff[514];ifstreammytilef'紅樓夢(mèng).txt”);inti,N=O,j;charchi;if(myfile==NULL)cout?"無(wú)法打開文本文件;”;elsewhile(myfile?chl){//遍歷紅樓夢(mèng)文本,統(tǒng)計(jì)出現(xiàn)的不同字符for(j=0;j4、建haffman樹search(huff,i,top);huff[i].lc_=top[OJ;huff[i].rc_=top[l];huff[i].freq_=huff[top[0]].freq_+huff[top[l]].freq_;huff[top[0]].freq_=0;huff[top[l]].freq_=0;}charstring[20];code(huff,2*N-2,0,string,0,N);clock_tprevioust=clock();zip(huff?N);clock_tcurrentt=clock()-previoust;cout?"壓
5、縮用時(shí):"?currentt?endl;previoust=clock();release(huff,N);currentt=clock()-previoust;cout?"解壓用時(shí):"?currentt?endl;system(,'pause,,);return0;}voidsearch(Huffman_nodehuff[],intnzinttop[])//找出huffman數(shù)組中最小的兩個(gè)頻率函數(shù){inti,min;for(i=0;huff[i].freq_==0;i++)■/min二huff[i]?freq_;top[0]=i;for(;ivn;i++)i
6、f(huff[i].freq_0){top[0]=i;min二huff[i].freq_;}for(i=0;i==top[0]11huff[i].freq_==O;i++)min=huff[i].freq_;top[l]=i;for(;ivn;i++)if(huff[i].freq_0){top[l]=i;min=huff[i].freq_;}}〃基于huffman的’01'編碼voidcode(Huffman_nodehuff[],introot,intp,c
7、harstring[],inti,intN){if(i!=O){p==O?string[i-l]='O':string[i-l]='l';}if(root