



版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、精品哈弗曼編碼實(shí)現(xiàn)無損壓縮實(shí)驗(yàn)報(bào)告一、實(shí)驗(yàn)內(nèi)容通過 C+ 編程實(shí)現(xiàn)。要求: 1) 字符串的輸入是手工輸入的;2) 通過程序?qū)崿F(xiàn)編碼, 最終在屏幕上顯示編碼結(jié)果, 例如,如果選用 huffman 編碼,則要顯示字符串的編碼以及平均碼長;二、源代碼#include<iostream.h>#include<math.h>#include<string.h>#include<stdlib.h>#if !defined _HUFFMANTREE_H_#define _HUFFMANTREE_H_class HuffmanTreepublic:unsigne
2、d int Weight;unsigned int Parent;-可編輯 -精品unsigned int lChild;unsigned int rChild;typedef char *HuffmanCode;/* 從結(jié)點(diǎn)集合中選出權(quán)值最小的兩個(gè)結(jié)點(diǎn)將值分別賦給s1 和 s2*/void Select(HuffmanTree* HT,int Count,int *s1,int *s2)unsigned int temp1=0;unsigned int temp2=0;unsigned int temp3;for(int i=1;i<=Count;i+)if(HTi.Parent=0)
3、if(temp1=0)temp1=HTi.Weight;(*s1)=i;elseif(temp2=0)-可編輯 -精品temp2=HTi.Weight;(*s2)=i;if(temp2<temp1)temp3=temp2;temp2=temp1;temp1=temp3;temp3=(*s2);(*s2)=(*s1);(*s1)=temp3;elseif(HTi.Weight<temp1)temp2=temp1;temp1=HTi.Weight;(*s2)=(*s1);(*s1)=i;-可編輯 -精品if(HTi.Weight>temp1&&HTi.Weight
4、<temp2)temp2=HTi.Weight;(*s2)=i;/* 霍夫曼編碼函數(shù)*/void HuffmanCoding(HuffmanTree * HT,HuffmanCode * HC,int *Weight,int Count)int i;int s1,s2;int TotalLength;char* cd;unsigned int c;-可編輯 -精品unsigned int f;int start;if(Count<=1) return;TotalLength=Count*2-1;HT = new HuffmanTree(TotalLength+1)*sizeof(H
5、uffmanTree);for(i=1;i<=Count;i+)HTi.Parent=0;HTi.rChild=0;HTi.lChild=0;HTi.Weight=(*Weight);Weight+;for(i=Count+1;i<=TotalLength;i+)HTi.Weight=0;HTi.Parent=0;HTi.lChild=0;HTi.rChild=0;/ 建造霍夫曼樹-可編輯 -精品for(i=Count+1;i<=TotalLength;+i)Select(HT, i-1, &s1, &s2);HTs1.Parent = i;HTs2.Pare
6、nt = i;HTi.lChild = s1;HTi.rChild = s2;HTi.Weight = HTs1.Weight + HTs2.Weight;/ 輸出霍夫曼編碼(*HC)=(HuffmanCode)malloc(Count+1)*sizeof(char*);cd = new charCount*sizeof(char);cdCount-1='0'for(i=1;i<=Count;+i)start=Count-1;for(c = i,f = HTi.Parent; f != 0; c = f, f = HTf.Parent)if(HTf.lChild = c)
7、cd-start='0'elsecd-start='1'-可編輯 -精品(*HC)i = new char (Count-start)*sizeof(char);strcpy(*HC)i, &cdstart);delete HT;delete cd;/* 在字符串中查找某個(gè)字符* 如果找到,則返回其位置*/int LookFor(char *str, char letter, int count)int i;for(i=0;i<count;i+)if(stri=letter) return i;return -1;void OutputWeight
8、(char *Data,int Length,-可編輯 -精品char *WhatLetter,int *Weight,int *Count)int i;char* Letter = new charLength;int* LetterCount = new intLength;int AllCount=0;int Index;int Sum=0;float Persent=0;for(i=0;i<Length;i+)if(i=0)Letter0=Datai;LetterCount0=1;AllCount+;elseIndex=LookFor(Letter,Datai,AllCount)
9、;if(Index=-1)-可編輯 -精品LetterAllCount=Datai;LetterCountAllCount=1;AllCount+;elseLetterCountIndex+;for(i=0;i<AllCount;i+)Sum=Sum+LetterCounti;(*Weight) = new intAllCount;(*WhatLetter) = new charAllCount;for(i=0;i<AllCount;i+)Persent=(float)LetterCounti/(float)Sum;(*Weight)i=(int)(1000*Persent);-可
10、編輯 -精品(*WhatLetter)i=Letteri;(*Count)=AllCount;delete Letter;delete LetterCount;#endif/*main.cppvoid main()HuffmanTree * HT = NULL;HuffmanCode HC;char Data100;char *WhatLetter;int *Weight;int Count;cout<<"請輸入一行文本數(shù)據(jù):"<<endl;cin>>Data;-可編輯 -精品cout<<endl;OutputWeight(Data,strlen(Data),&WhatLetter,&Weight,&Count);HuffmanCoding(HT, &HC, Weight, Count);cout<<"字符出現(xiàn)頻率編碼結(jié)果 "<<endl;int k=0;for(int i = 0; i<Count; i+)cout<<WhatLetteri<<""cout<<Weight
溫馨提示
- 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)僅提供信息存儲空間,僅對用戶上傳內(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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 酒店電梯間的藝術(shù)裝飾策略
- 跨境B2B電商平臺用戶體驗(yàn)研究
- 浙江國企招聘2025中移鐵通嘉興海鹽分公司招聘10人筆試參考題庫附帶答案詳解
- 山西2025年02月山西省事業(yè)單位公開招考筆試歷年典型考題(歷年真題考點(diǎn))解題思路附帶答案詳解
- 江蘇專用2025版高考數(shù)學(xué)大一輪復(fù)習(xí)第八章立體幾何高考專題突破四高考中的立體幾何問題教案含解析
- 質(zhì)量管理體系與商業(yè)競爭力的關(guān)系
- 廣東省2024-2025學(xué)年高中化學(xué)第三章第一節(jié)有機(jī)化合物的合成訓(xùn)練無答案魯科版選修5
- 金融行業(yè)中的財(cái)務(wù)審計(jì)特殊要求分析
- 跨國企業(yè)全球供應(yīng)鏈中的跨區(qū)域物流管理
- 達(dá)州市公共交通有限公司2024年第二批公交駕駛員招聘復(fù)審以及理論考試筆試參考題庫附帶答案詳解
- 醫(yī)療機(jī)構(gòu)注銷登記申請書
- GB/T 678-2023化學(xué)試劑乙醇(無水乙醇)
- 影視鑒賞-第一章-認(rèn)識電影-課件
- 船舶塢修廠修工程單審批稿
- 教科版小學(xué)科學(xué)三年級上冊《空氣》單元解讀與試教課件
- 電機(jī)學(xué)同步電機(jī)-全套課件
- 公路工程施工安全管理及其實(shí)例
- 教科版高中信息技術(shù)(2019)必修一全冊教案
- 左洛復(fù)怡諾思專家講座
- 行政確認(rèn)專題教育課件
- 2023年道德與法治課程標(biāo)準(zhǔn)
評論
0/150
提交評論