




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
哈弗曼編碼實(shí)現(xiàn)無損壓縮試驗(yàn)匯報(bào)試驗(yàn)內(nèi)容通過C++編程實(shí)現(xiàn)。規(guī)定:1)字符串旳輸入是手工輸入旳;2)通過程序?qū)崿F(xiàn)編碼,最終在屏幕上顯示編碼成果,例如,假如選用huffman編碼,則要顯示字符串旳編碼以及平均碼長;源代碼#include<iostream.h>#include<math.h>#include<string.h>#include<stdlib.h>#if!defined_HUFFMANTREE_H_#define_HUFFMANTREE_H_classHuffmanTree{public:unsignedintWeight;unsignedintParent;unsignedintlChild;unsignedintrChild;};typedefchar**HuffmanCode;/*從結(jié)點(diǎn)集合中選出權(quán)值最小旳兩個(gè)結(jié)點(diǎn)將值分別賦給s1和s2*/voidSelect(HuffmanTree*HT,intCount,int*s1,int*s2){unsignedinttemp1=0;unsignedinttemp2=0;unsignedinttemp3;for(inti=1;i<=Count;i++){if(HT[i].Parent==0){if(temp1==0){temp1=HT[i].Weight;(*s1)=i;}else{if(temp2==0){temp2=HT[i].Weight;(*s2)=i;if(temp2<temp1){temp3=temp2;temp2=temp1;temp1=temp3;temp3=(*s2);(*s2)=(*s1);(*s1)=temp3;}}else{if(HT[i].Weight<temp1){temp2=temp1;temp1=HT[i].Weight;(*s2)=(*s1);(*s1)=i;}if(HT[i].Weight>temp1&&HT[i].Weight<temp2){temp2=HT[i].Weight;(*s2)=i;}}}}}}/*霍夫曼編碼函數(shù)*/voidHuffmanCoding(HuffmanTree*HT,HuffmanCode*HC,int*Weight,intCount){inti;ints1,s2;intTotalLength;char*cd;unsignedintc;unsignedintf;intstart;if(Count<=1)return;TotalLength=Count*2-1;HT=newHuffmanTree[(TotalLength+1)*sizeof(HuffmanTree)];for(i=1;i<=Count;i++){HT[i].Parent=0;HT[i].rChild=0;HT[i].lChild=0;HT[i].Weight=(*Weight);Weight++;}for(i=Count+1;i<=TotalLength;i++){HT[i].Weight=0;HT[i].Parent=0;HT[i].lChild=0;HT[i].rChild=0;}//建造霍夫曼樹for(i=Count+1;i<=TotalLength;++i){Select(HT,i-1,&s1,&s2);HT[s1].Parent=i;HT[s2].Parent=i;HT[i].lChild=s1;HT[i].rChild=s2;HT[i].Weight=HT[s1].Weight+HT[s2].Weight;}//輸出霍夫曼編碼(*HC)=(HuffmanCode)malloc((Count+1)*sizeof(char*));cd=newchar[Count*sizeof(char)];cd[Count-1]='\0';for(i=1;i<=Count;++i){start=Count-1;for(c=i,f=HT[i].Parent;f!=0;c=f,f=HT[f].Parent){if(HT[f].lChild==c)cd[--start]='0';elsecd[--start]='1';(*HC)[i]=newchar[(Count-start)*sizeof(char)];strcpy((*HC)[i],&cd[start]);}}delete[]HT;delete[]cd;}/**在字符串中查找某個(gè)字符*假如找到,則返回其位置*/intLookFor(char*str,charletter,intcount){inti;for(i=0;i<count;i++){if(str[i]==letter)returni;}return-1;}voidOutputWeight(char*Data,intLength,char**WhatLetter,int**Weight,int*Count){inti;char*Letter=newchar[Length];int*LetterCount=newint[Length];intAllCount=0;intIndex;intSum=0;floatPersent=0;for(i=0;i<Length;i++){if(i==0){Letter[0]=Data[i];LetterCount[0]=1;AllCount++;}else{Index=LookFor(Letter,Data[i],AllCount);if(Index==-1){Letter[AllCount]=Data[i];LetterCount[AllCount]=1;AllCount++;}else{LetterCount[Index]++;}}}for(i=0;i<AllCount;i++){Sum=Sum+LetterCount[i];}(*Weight)=newint[AllCount];(*WhatLetter)=newchar[AllCount];for(i=0;i<AllCount;i++){Persent=(float)LetterCount[i]/(float)Sum;(*Weight)[i]=(int)(1000*Persent);(*WhatLetter)[i]=Letter[i];}(*Count)=AllCount;delete[]Letter;delete[]LetterCount;}#endif//*********************************main.cppvoidmain(){HuffmanTree*HT=NULL;HuffmanCodeHC;charData[100];char*WhatLetter;int*Weight;intCount;cout<<"請輸入一行文本數(shù)據(jù):"<<endl;cin>>Data;cout<<endl;OutputWeight(Data,strlen(Data),&WhatLetter,&Weight,&Count);HuffmanCoding(HT,&HC,Weight,Count);cout<<"字符出現(xiàn)頻率編碼成果"<
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(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ǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五年度國有企業(yè)員工績效評(píng)估及激勵(lì)合同
- 2025年度環(huán)保項(xiàng)目評(píng)估外聘專家合作協(xié)議
- 二零二五年度日租房短租與藝術(shù)展覽合作協(xié)議
- 二零二五年度家長參與校園安全責(zé)任協(xié)議書反饋流程
- 環(huán)保能源行業(yè)項(xiàng)目投資風(fēng)險(xiǎn)免責(zé)
- 農(nóng)業(yè)行業(yè)精準(zhǔn)農(nóng)業(yè)技術(shù)支持平臺(tái)搭建方案
- 幼兒園繪本故事集征文
- 電商平臺(tái)商戶退換貨規(guī)則協(xié)議
- 高考語文大一輪總復(fù)習(xí) 文言文閱讀 文言實(shí)詞題題組訓(xùn)練試題
- 高考語文備考文言文閱讀專項(xiàng)練習(xí)之文言文斷句
- 森林防火安全生產(chǎn)工作
- 《服裝市場營銷》課件
- 網(wǎng)絡(luò)安全風(fēng)險(xiǎn)評(píng)估報(bào)告模板
- 什么是法律談判課件
- 成考教材-數(shù)學(xué)教程(文史財(cái)經(jīng)類)
- 保安服務(wù)管理制度范文
- 汽車行業(yè)維修記錄管理制度
- 老年護(hù)理團(tuán)隊(duì)建設(shè)方案
- 《跨學(xué)科實(shí)踐活動(dòng)3 水質(zhì)檢測及自制凈水器》教學(xué)設(shè)計(jì)
- 起重吊裝作業(yè)安全培訓(xùn)考核試卷
- 開塞露的使用
評(píng)論
0/150
提交評(píng)論