2023年哈弗曼編碼實(shí)驗(yàn)報(bào)告無損壓縮實(shí)驗(yàn)報(bào)告_第1頁
2023年哈弗曼編碼實(shí)驗(yàn)報(bào)告無損壓縮實(shí)驗(yàn)報(bào)告_第2頁
2023年哈弗曼編碼實(shí)驗(yàn)報(bào)告無損壓縮實(shí)驗(yàn)報(bào)告_第3頁
2023年哈弗曼編碼實(shí)驗(yàn)報(bào)告無損壓縮實(shí)驗(yàn)報(bào)告_第4頁
2023年哈弗曼編碼實(shí)驗(yàn)報(bào)告無損壓縮實(shí)驗(yàn)報(bào)告_第5頁
已閱讀5頁,還剩8頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論