哈夫曼編碼解碼試驗報告_第1頁
哈夫曼編碼解碼試驗報告_第2頁
哈夫曼編碼解碼試驗報告_第3頁
哈夫曼編碼解碼試驗報告_第4頁
哈夫曼編碼解碼試驗報告_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、哈夫曼編碼解碼實驗.實驗要求掌握二叉樹的相關(guān)概念掌握構(gòu)造哈夫曼樹,進(jìn)行哈夫曼編碼。對編碼內(nèi)容通過哈夫曼樹進(jìn)行解碼。.實驗內(nèi)容編碼完成通過二叉樹構(gòu)造哈夫曼樹,并用哈夫曼樹對讀取的 txt文件進(jìn)行哈夫曼編碼。后通過哈夫曼樹進(jìn)行解碼。#include#include#define MAX 100/定義哈夫曼樹的存儲結(jié)構(gòu)typedef structchar data;int weight;int parent;int lch;int rch;HuffNode;/定義哈夫曼編碼的存儲結(jié)構(gòu)typedef structchar bitMAX;int start;HuffCode;HuffNode ht2*M

2、AX;HuffCode hcdMAX;int Coun127=0;int n;char s1200000;char text5000;/構(gòu)造哈夫曼樹 void HuffmanTree()int i,j,k,left,right,min1,min2;/printf(輸入葉子的節(jié)點數(shù):);/scanf(%d,&n);printf(字符數(shù)量=%dn,n);for(i=1;i=2*n-1;i+)hti.parent=hti.lch=hti.rch=0;j=0;for(i=1;i=n;i+)/*getchar();printf( 輸入第%d個葉子節(jié)點的值:,i);scanf(%c,&hti.data);

3、printf( 輸入該節(jié)點的權(quán)值:);scanf(%d,&hti.weight);*/for(;j127;j+) if(Counj!=0) hti.data=j;/printf(%c,hti.data);hti.weight=Counj;/printf(%d,hti.weight); break; j+;printf(n);for(i=1;i=n;i+)printf(%c,hti.data);printf(n);for(i=n+1;i=2*n-1;i+)/在前n個結(jié)點中選取權(quán)值最小的兩個結(jié)點構(gòu)成一顆二叉樹 min1=min2=10000;/ 為min1和min2設(shè)置一個比所有權(quán)值都大的值 le

4、ft=right=0;for(k=1;k=i-1;k+) if(htk.parent=0)/若是根結(jié)點和 right/ 令min1和min2為最小的兩個權(quán)值,left為權(quán)值最小的兩個結(jié)點位置if(htk.weightmin1) (min2=min1;right=left;min1=htk.weight;left=k;)else if (htk.weightmin2)(min2=htk.weight;right=k;)htleft.parent=i;htright.parent=i;hti.weight=htleft.weight+htright.weight; hti.lch=left;hti

5、.rch =right;)/構(gòu)造哈夫曼編碼void HuffmanCode()(int i,c,k,f;HuffCode cd;for(i=1;i=n;i+)(cd.start=n;c=i;f=hti.parent;while(f!=0)(if(htf.lch=c)cd.bitcd.start=0;elsecd.bitcd.start=1;cd.start-;c=f;f=htf.parent;hcdi=cd;)printf( 輸出哈夫曼編碼:n);for(i=1;i=n;i+)(printf(%c:,hti.data);for(k=hcdi.start+1;k=n;k+) printf(%c,

6、hcdi.bitk);printf(n);)/對字母進(jìn)行編碼void Code()/將字符與相應(yīng)的哈夫曼編碼進(jìn)行匹配,輸出編碼結(jié)果(int i=0,j,k,h=0;while(texti!=0) (for(j=1;j=n;j+)(if(texti=htj.data)(for(k=hcdj.start+1;k=n;k+)(s1h=hcdj.bitk;h+;) break;) i+;)/printf( 編碼 n);/puts(s1);printf(n);/解碼void HuffmanDecode()(printf(解碼 n);int len,i,f;char C;/char SMAXCODE;/s

7、canf(%s,S);/ 使用 gets()直接跳過len=strlen(s1);printf(s1:%dn,len);f=2*n-1;for(i=0;ilen;i+)(if(s1i=0) ( f=htf.lch;if(htf.lch=0&htf.rch=0) (C=htf.data;printf(%c,C); f=2*n-1;else if(s1i=1) (f=htf.rch;if(htf.lch=0&htf.rch=0) (C=htf.data;printf(%c,C); f=2*n-1; printf(n);/統(tǒng)計字母個數(shù)及其權(quán)值void Count()(int i,j,m;n=0;i=

8、0;/printf(請僅輸入小寫字母n);/例程本省存在一個 BUG只輸入一個字母不能進(jìn)行編碼(并未解決)/scanf(%s,s);while(texti!=0)/使用 ASCII 碼表進(jìn)行統(tǒng)計(m=texti;printf(%dn,m);Counm+;i+;for(j=0;j127;j+)(if(Counj!=0) n+; /mark Codevoid main() (int l=0;FILE *fp;fp=fopen(text.txt,r);if(fp=NULL) (printf(文件打開失敗n);while(1);while(!feof(fp)(textl = fgetc(fp);l+;

9、printf(輸入文本 n);printf(%sn,text);fclose(fp);Count();HuffmanTree();HuffmanCode();Code();HuffmanDecode();文本文件,口 the begiiminK Uod created the JieaveuE and the 歸虹th. Nov tbe earth wa.s loruiless iind enoJtyB dirknesE wan twer tlii uurfHB uf the*and thu Spa ri L of Gud ws Iiuv avf?r the vitursi. AeiJ Gol

10、l n id, ILuttheccr tir 1 icht. * fluvl Hirrn? wm li 曲3. Gng tht tht 11 iht -巾壽 后??谥倍?mdi hf srp4ratHii the 11 nht f rno the 4arfciwss. God 11sdl tho 1 ight *riay,and the darfciKss he cal 1(3 -ni sht, And there wis BTRning., andthere s swtfiiiMl.lhg firstGod Midi. L修T 牙h紂電 曲型 tn 總電Th戶 wuterB t*w4tr f

11、iuflSq- Cud ad Ihv ApXLiv 4iid avpiu; &te4 th water uiKwr th穹 aimsv tioiui th# 視Eeabove It. Anil ii so. G(l itm diy .rmrn。and thn gJithDrodi wjtitrn I191 cvllml *tioao- Arid God ebv t|id -cu岫rili 鵬 Lu their klfLdzf nd tre-Hii b trit ri ng frui I wi th d史 4 lei it aGcardtim in thRir kinds. And Ged n#

12、v Iha 1 iit. *rnn sond. And thFrrfi waE: nrrnninKw arKi tlmrn was momiriE-the third day fcid God sfliid. Let there be 1 iahts in the 曰hr卻nse of The sky to sei?flratB the Wy fxoB. Lhv Eiichi. 4nd LhI thejn sfexve n&lu UKik 耳芒由上迎目 aul1 4風(fēng)/耳 and 1rlm d -uid 191 tlies b。IICHnin Ihf;nf Ihfs sky tn civn l

13、iitht nn the fiirth. Mid it 辦件 sn. Gud na壯升 two I ifchtstheroatar 1 igbi tn gmrQirn th day nzid Lhs Ioe-ebf 11ght. to g.nVEim tha night. Ho alsn Kado ttic ictarc. &ori 歸日t th曰ft. Itt lh& hSpanFH ths 派甘 加 Mv日 IImt otl tlw Baf !th, 3 EurBTn Uio day ahdl Ih日 tiltr and Uipath liht Itob dtrlmtiiB. A1 God

14、 stw that it wt 加厚 ftisd thr vis 小心 Mid tn#r* n* iiDurriiiig-the1 fiourth day, Anxi God. Eaiii. Let the mtn tee* irith lining creatures, anjd let birds fly a bore tlm iarth. aeries trie HMpage 口工 ITxe sfcy- - Sa Cud cniatecl the great cremLuns of th更 sna and every 1 Ivlng 卜mil j町“iy thins Hh which L

15、h? tor 好白屯.乳tc牛rdi此考 to thoir kinds itid u*ijr virkfutlbirrf &庫 to :ig Wind And Gtd bsv that it vtn gand. God blu ithflir kind*: 11rnetock, 匚丁atuTW that wir口 11nn its k i nd. * Aitd it wras so. Gad 港日曰 Uis ti Id jiniwls accordiing to Xheir kinilEa the liTBEWck aicciurdiTig S their kinds, and all th

16、creBturi: tlhAt move hLoue the ground according to th&lr ki白. And Uod sav that It ,修日 ftchjdL TAiuel uudiL ufi mIlu bud Iql mr lei our li*4UHru. axidl let xhy.n euJt ,號ue tbu r i sh of Lh.e sea and the bl rite of tfie airR over thre 11 vestock:, oter all ilie earth., ami offer all tM cru4turBs th 才

17、I biikivb ung tliB Ki m d. 5 口 God Gnatud man in bis owniii the i bun kb of God he文本輸入nvprcalledcalledromcalledttiKirthentheir kindsSaw I ,Andgathered watersland produo voDtation: ood-boiring plantswas nrirrtiheir various kindspl ante bearine ,n it according ng, ard there 1and ej(pty: hnveri ng the

18、light ihero 卡mn ?ie waters:ovemne, an anse between nd sep iratsd. God called SRcnnri day.1 kinjds. Aiid I ng-thfi thirdha earth ws.s form the Spirit ! Ged it, and thfiFF 藥吊s.KpcAIltit! U:黨勾刁三 k*!Umorning-the first day. rd Go to separaLe lyater irom vaserA seasons and 訊 give ligh二 an - light s govern

19、 ths st are. Gad 經(jīng)d that bear fruit with seed in it, so. Thm 1 and preduesd veg&tation: and trees bearing frul L v,i Lii stjed at. i t was gnod. And t.hpc-E 痔s ryrhlight froni the dai; “night. , And there ., Let 二here be antre cxpanca al tnc cwy t great lights-the greater ths night. H al sc mad白s&pa

20、rae the 口父 nd years arth4 *Ijncler thu expatiet ft t if xsky. And thwitd,th WAt.RTwas a7 lining; and Uriere was Dorm ng-th iinrier the sky be gathRTRd to nnp /1才。 q. God g11 ed the dry groknid T&nd,” nd G?!縮aw that it vas good. Then Godd sid, JLet there te lights , the night* and let than serve1 ot

21、thoni bo li jhtc was so. God mde口目q巨r(nóng) 1ight進(jìn)行哈夫曼編碼對文本進(jìn)行編碼 KBm32cmdiexe挪3JOIOIOOOOIIOOIOOIIOOOIOCIOIIOOIOIDOOOOIIIIIIOOIIOIIIOOIIOOIIIOILIOOIIII1000010100D1111011000000101111111101110U110001110D0001100010001100010001110110101010111001 1101000101 nooiinoooQ.i jooioooiicooii 10111 in iiiconwoiniowooi i

22、iooioooaiiiiioIIOIOOIODLIOOOIOOOIIOOOIIIOIIIIIIIIIOOOIOOOOIOIOOILOIIIIOIOOOIIIIOIIULOIIILIIOI iJlOOlllODOOlinOlOlLOlOODlOlllOOllOOOOOOllOlOlOOOlOUOlllOOlOlOllOlllOOllOOlCOOl 101111111010110(11 K101111 HOL 1010K1010011C1111010COU10101010101111 LllOOllOOClOO JoilOOllOlClOlOlllllLlllll10111011010111011

23、00111011111011001100010001100100001101 rlOlOHOllllOOllOHOLllOOllOOOOOllOOOlOOOllOOlOlOlOOOllOlOllOllLOlllllltllOllllOO iooilioill11011001010001111011000D01D100110111101000010011101010101011111111L0111101111:000011 ICiPWLOlOU 111110011000100011001010011011110001111111110101111C1003111001011001100300L

24、0100mil011DC00011010101111311L000111001100101010L1010lCOP 111 WOOD llOOQlODOlLliniOUOOlOlOOOOOl IDO 1110001.1QL 11 UlOOOWOllOOl 110011101010 10010111001LOOOOD1100010001 UmiOllOOlOlOOl 101111010001110001L01111110001001100 111 10100010100011110110000011010101HOIODIOO HOOD 1001011110000LW00100011001110

25、0 piioiii ii loooiooiwoooioioo hoi ii w loooii iiooiiioi ii in iwooi iiooiioowuiooiiooo DOO LOOQUOOllOlOOllOlOnOllOlim 11101IIIQOOIIIOOOOOUQOOIOOOILOCII11000110111111 000100: DOOOir 1011111111110101010000110K106011001000101IIIIILOIOILOOIOOIOUIIO 101101011110100010100011110110000001011110111110001110

26、000111000001100010001L0011 dlOOQllOllllllQOQlOOllOOODlOlOlOllOOQlDlllOlOllOlllOOlllOlOlQlOOlOlllOOllQOODCllO 0010001100100010111L1110101100100101111O1C11O1OOOC1OOO11OOO1O1L11O1111LOOO1L1OOO 011 lODOODlOlOlO 1100 L1101111110001001100 111 10 lOlOlOlDlOOlllOOlOLWO 11000001100010 ooi in: noi looioiooi 1

27、0111101000011010101 ii ii oonic ii won ii looi iiooLiooioii icmi 0000011000100011111LW11001010Q11011UQW0OD101001110111111100 L11011100111 HOC LI 瞪酎再吉半:輸出解碼結(jié)果kr: 101011Jearth.fornLeoverwaternami n?oneLU1tlieir訓(xùn)祚markfrom romni ght, ,than beexpanse d there ,c waterabove1 it. d there 科; e gatherodyater Lnderguvtirii3 gcodwas

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(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)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論