


版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、哈夫曼編碼解碼實(shí)驗(yàn)1實(shí)驗(yàn)要求掌握二叉樹的相關(guān)概念掌握構(gòu)造哈夫曼樹,進(jìn)行哈夫曼編碼。對編碼內(nèi)容通過哈夫曼樹進(jìn)行解碼。2實(shí)驗(yàn)內(nèi)容txt文件進(jìn)行哈夫曼編碼。編碼完成通過二叉樹構(gòu)造哈夫曼樹,并用哈夫曼樹對讀取的 后通過哈夫曼樹進(jìn)行解碼。#in clude<stdio.h>#in clude<stri ng.h>#define MAX 100/定義哈夫曼樹的存儲結(jié)構(gòu)typedef structchar data;int weight;int pare nt;int lch;int rch;HuffNode;/定義哈夫曼編碼的存儲結(jié)構(gòu)typedef structchar bitMA
2、X;int start;HuffCode;HuffNode ht2*MAX;HuffCode hcdMAX;int Coun127=0;int n;char s1200000;char text5000;II構(gòu)造哈夫曼樹void Huffma nTree()int i,j,k,left,right,mi n1,mi n2;/printf(”輸入葉子的節(jié)點(diǎn)數(shù):");/scan f("%d",&n);printf("字符數(shù)量=%dn",n);for(i=1;i<=2* n-1;i+)hti.pare nt=hti.lch=hti.rc
3、h=O;j=0;for(i=1;i<=n ;i+)/*getchar();printf("輸入第%d個葉子節(jié)點(diǎn)的值:",i);scan f("%c", &hti.data);printf("輸入該節(jié)點(diǎn)的權(quán)值:"); scan f("%d",&hti .weight);*/for(;j<127;j+)if(Cou nj!=0)hti.data=j;/prin tf("%c",hti.data);hti .weight=Cou nj;/prin tf("%d&q
4、uot;,h ti .weight);break;j+;prin tf("n ”);for(i=1;i<=n ;i+)prin tf("%c",hti.data);prin tf("n ”);for(i=n+1;i<=2* n-1;i+)/在前n個結(jié)點(diǎn)中選取權(quán)值最小的兩個結(jié)點(diǎn)構(gòu)成一顆二叉樹min仁min2=10000;為min1和min2設(shè)置一個比所有權(quán)值都大的值left=right=0;for(k=1;k<=i-1;k+)if(htk.pare nt=O) 若是根結(jié)點(diǎn) /令min1和min2為最小的兩個權(quán)值,left和right 為權(quán)
5、值最小的兩個結(jié)點(diǎn)位置if(htk.weight<mi n1)mi n2=mi n1;right=left;min 1=htk.weight;left=k;else if (htk.weight<mi n2)min 2=htk.weight;right=k;htleft.pare nt=i;htright.pare nt=i;hti .weight=htleft.weight+htright.weight;hti .lch=left; hti.rch =right;/構(gòu)造哈夫曼編碼void Huffma nCode()int i,c,k,f;HuffCode cd;for(i=1;i
6、<=n ;i+)cd.start=n;c=i;f=hti.pare nt;while(f!=0)if(htf.lch=c) cd.bitcd.start='O' elsecd.bitcd.start='1' cd.start-;c=f;f=htf.pare nt;hcdi=cd;printf("輸出哈夫曼編碼:n");for(i=1;i<=n ;i+)prin tf("%c:",hti.data);for(k=hcdi.start+1;k<=n ;k+) prin tf("%c",hc
7、di.bitk);prin tf("n");對字母進(jìn)行編碼void Code()將字符與相應(yīng)的哈夫曼編碼進(jìn)行匹配,輸出編碼結(jié)果 int i=O,j,k,h=O;while(texti!='O')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);/prin tf("n");/解碼void Huffma nDecode()printf(” 解碼
8、 n");int len ,i,f;char C;/char SMAXCODE;/scanf("%s",S); 使用 gets()直接跳過 len=strle n( s1);prin tf("s1:%dn",le n);f=2* n-1;for(i=0;i<le n;i+)if(s1i='O')f=htf.lch;if(htf.lch=0&&htf.rch=0) C=htf.data;prin tf("%c",C); f=2* n-1;else if(s1i='1')f=
9、htf.rch;if(htf.lch=0&&htf.rch=0) C=htf.data;prin tf("%c",C); f=2* n-1;prin tf("n ”);/統(tǒng)計(jì)字母個數(shù)及其權(quán)值void Cou nt()int i,j,m;n=0;i=0;/printf("請僅輸入小寫字母n”);/例程本省存在一個BUG,只輸入一個字母不能進(jìn)行編碼(并未解決)/scan f("%s",s);while(texti!='0') 使用 ASCII 碼表進(jìn)行統(tǒng)計(jì)m=texti;/pri ntf("%dn
10、",m);Coun m+;i+;for(j=0;j<127;j+)if(Cou nj!=0)n+;/mark Codevoid mai n()int l=0;FILE *fp;fp=fope n("text.txt","r"); if(fp=NULL)printf("文件打開失敗n"); while(1);while(!feof(fp)textl = fgetc(fp);l+;printf("輸入文本 n");prin tf("%sn",text);fclose(fp);Cou
11、nt();Huffma nTree();Huffma nCode();Code();Huffma nDecode();文本文件Lin thia b 砂.inning. G-ndcircia t edi LBig hcai-vQ-ius and tJua Bar th. Nuiw ttwz h a.r LBavas f or ad ehse atid Ditp tjr v darknes-s卜帖 tivor the t;tirfMuof Uw c1hup: "雷ihuSpirit of Gud1如旺1“蠢UWw-fleru, And Gud sild *Lulthftrfi b<
12、i 11 phtg. * JindThm-n wn-n l a ghtGnrinaw that thn lifht wsmariid Ihnrnar-atiHri thn 11 (ht tfrcnnthe riarfcneBg God called tlie lightmdl The durtness he called*Td宜h*"Arwi there W4<s evenink* and|th&r was KTrLiaatiie first And God Sl£(t *Lfit tbeze be an電tbeto £epara.t«v&
13、#171;t»- truuL witw.* Sa Gael wule th# «££>«us« aimI<IIj« vtt«r 血i4lr Ihw vip«Eui« fruHL thw it«rsbove kta Arid it 吃£ eix Godthe 舍些亂hse slty. * And th&ra 曲匕辛歸口Heul 乳cd thre was JM>mirLfl-lhehsifcijrjd dy. And Cud. uald. *LbI th
14、e 蔚國1巳urjdBi* the uky be gathered Lu 口陀aevl Hut tlry ruurijHipihsar. " And i IGihI c-tl liEiidi thr dr-r griwfcil *1 indt * anil Un? gathrired. wtt lt?rfl iw cnl 1 trd "«e*J3* " ArulGnri cav tlhai i t laras 露口rwl. Thon Cnd eai.dig, Hut;吐IKr land pircidue:a- rajQtali c口: sqnilbnar
15、 iHi pl anti; and. Itmes on the 1-and that bear fruit with 石屯日日 in it3 Jiccordito thsir various kinds. Andi ni was so. The larid prutlu<iMl yusutJitiun; pljiriu; butrlrij mliU rtcctndli/it to thoirtTUE btriong fruit wiuh i;u阿il init rteenrdinc: tn thmir kinri甌 And God fiftw that it w«fT (nnd
16、 AtwI tlinrn r HreniriEi arid thfirn nn mcrmiinB-the third day. And God SRidlP "l_Et there be 1 igilts in the expanse of the slty to separate the day Irun tbrij«Qd 11 ihtB Mtirv#Hiui&b tti uukik 越裁直diul d<yti <LLd yeuba aiull>e lightuifj 5q ciQ*nir內(nèi) of tho ofcy toon the arth.
17、* And it was召。啦 Mdo Sro 啣筋電1 Light cKi the E-airLti guverni tiiE da ami tJle 口丄 各hii# ufilI *Ldi MUTMl thirru w>t:*Let thn vatRr tRHH wi th li vins crsaturiiE:.孔i Hi bi rrie fly fibnwv So God crBatd fthw ktm I craa toriBS uf Vho 砂刃 and every 1insEreater 11 ielm to Govern the d范v and IKie Lsser 11
18、 ght m eavem tlie nlfiht. Ble al卻 口taile the stars. God set Uic uti tJiE ExpaixsiC:WI N D6VS 圻、rten N,,u m d,exeMlAxT 卑n the beginning Gcd created the heavens and the ear th. How thje earth was fornle| gjs anrl pnipty, riarkr.ez? uram over th Riivfacp of thp cfepp anrl Ihp Spiri t of Go ci Jf(s hove
19、ring over tt.e carers. And &od saiil, "Lex there te Light, J and There Right. Gdc »aw lliat the light wit good, and h« s»p»Tated the light from ths diiT切 ess. God called the Iigh' x/day. r and the darkness he 匚alled ''night.J,And ms eening and ther ws morning
20、-tho first day.And Gad eid: Lot there b電 an Expanse between the witers to separate warer iron water.“ Sa Gcd ntaie the espanse $ and 3ep£rated the 'Viter under the expanse from the water above it. And it was s Bn. God cElled the expanse 'sky. " And there wam eenins, and there was m
21、nrnin-th Wz? second diy» And Cod said, Let the water under the sky be gathered to one pac 卜.2nd Let drv srouni appear." And it ws so. God called the erv eround "Iani," mnd the gathered wwtsrs lie called "sens'And God saw thdt it was good Then tod ? said, 'Let. the la
22、nd produce vagetati on: seed-baring plants ani trees cn the 1;> niil thsiL be-ai fruit wi tli sbkcI in It, <accoircLiti£ tu tlieir villous kinds*/K And it wa Je sd. The 1 and produced pegstaticn: plants bearing 首©ad atearcing to th&ir leitide and Wees bearing fruit with seed in I
23、t accordlus lo tliulr kinds. And God sav t hat 11 wss gand. Ar.d theTP was evening, and. thereGod saic, "L巳t there be lights in the expanse ol the niglnt ind let them ser?& as sie> to mark lei them be lights in :he expanse of tlie sky toLit wac dc. God nado tvo groat Lighter-the gr&a
24、tor light to govern tho day and hol lesser light :匚 govern :he rdht. He aIsl macte the stars. Got set :hem in tiae el xpanu匚 3i the sky toli rht dtl theto §overn 七h(yuǎn)e dciy and the ni§ht,卯音 半:e list.t iron darkness. And God saw that it was goo± nd t上ere w/&e 氏* 旦環(huán) 鬥去kmr 口呂 Jfr GLAll
25、HlJAjLi也 -心蘭,驢 匹:二空占 D”,L I 日匕, 口矍 Lhe aky to gi a訐鼻即昌11 hfi. frnn dsrknwe Anil Cud uiw Ih-it ii i wig acd AtkI Ihnru vac 砂口fd 呷 ranmins-tJhH fD>ur(h diiy And tiofi Eiid.the eajrth acrosE the expanse ut the sflky._+=nntfl mivi thbn電 vi th whi ch tho wo/ior toowi5 4&cnrdi ns to thair kindOi or
26、iid avott *i fig:o4 bi rd according tn it? kind. And God sav that it ns <nori. Gmi bl PSFPd then and Biidj °B« tTui tful -Andi 5ner>ftai?<* in iiualxir ajxdl fill ttie water in theuhd 丄日t the birds iu<xe<s« on the b&rthu " AjlkI there was eveiujris.uuil Uuiri#
27、iru urniug'-tltw Xif Ih 山1稱 Ami Cud uhIlL "Lut thu 1 hi til oruduav liviim. cltifhMuiw tLccurdlnH. tu thair kinds; liYaatacke creatures that wre along the groundP and vild mi Bala, each aefordine lo its klndL,P AinJI 11 was so. Go<d Ude rhe wl Idl an!ul/s according to Uhei r klnd. the I.
28、 i.vrest<Mrk aeeordin to Lhci.t1 kinds. and al t llbe CJrEiaLilrejC Lba I utve -aLhii grutiTLiJ ae<tirdi.ii.g lti thizi丁 kiruld-. Arid Ciid. js-aw lti4 L it嚙駟血 Thnn tiad trii rla *l.at us wikp mt 脾 in nur i m rb, in oinr t i konas 口. and 1 巾ruin ever thnfisli of IJih sea and ttiB birds of ths
29、r. ovar the 1 ir!Stac:fcP nr ill this <BartH, and 口ver all the cti&i*riire.0: tbit m旳審旦 a! ong th目 肛nuncL * So Godlwiri In bis enm 1ms» iti the imbb of (>od hewas 1there wwes norning-±he tbird day. Anri the sky to separate the day fr sefans and days 4nd yaar, an give light cn thm
30、 earth. /J 罰d文本輸入進(jìn)行哈夫曼編碼I DOWS 詁、sten;2c rnd.tKeplant for fond. v And it was §n.Gcdsawalithat had ikadj,atTriitwasve 呂odcL And there was eveningB and there was mom!ng;-the sizlh day.行數(shù)量=劭t - :; AEGHILNRTatdefshikli«nDprff'i.ivvxy歸出哈夫員編礎(chǔ)nl> :ooE* : moonK:111OQWU11111010;ioioidoooo!11
31、100101勺?b:iiiooiooooo片M010001 H;TIOIOIDOOOI1:101010010f4<;10101D00100L?:10101D0011lr:iiwoioooi:10Ll_ IIJCawt尸isiOH * = 口jti些昌琳uit airei/j.vb=ertc八図"sYir1 i eral音_半_:mfr口 darkness. And God saw that it was good. And t上ere .vas |對文本進(jìn)行編碼31Wl N DOWSsysteri 32c md, exeHgETT哼邛 jOLUOOIDIIOOIIOOOOOIDIOOO1111011111WOO11D111111 ”QQU)UL(JWCIiminn 丄 lLUllMMOKWOOMOMlOQQlKKH 叫 11:10 1十、ifiT】F- + 4輸出解碼結(jié)果LOH: LilIn-iornle 'God wavens end thE earth. Now the earth w; surface of the ce&p, and the Spirit ri s
溫馨提示
- 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)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 藥店員工二零二五年度勞動合同與藥品庫存優(yōu)化方案
- 二零二五年度合伙人投資協(xié)議書-大數(shù)據(jù)中心建設(shè)項(xiàng)目合作
- 二零二五年度城市更新改造方借款協(xié)議書
- 2025年度知識產(chǎn)權(quán)運(yùn)營聘用合同
- 二零二五年度企業(yè)新員工個人信息及商業(yè)秘密保護(hù)合同
- 二零二五年度高空作業(yè)安全責(zé)任協(xié)議書(高空管道清潔服務(wù)合同)
- 2025年度生態(tài)旅游果園土地承包經(jīng)營協(xié)議
- 2025年度智能化養(yǎng)殖場租賃及數(shù)據(jù)共享合作協(xié)議
- 二零二五年度魚塘承包與漁業(yè)人才培養(yǎng)及交流合作協(xié)議
- 開展敬老月活動總結(jié)
- 會計(jì)人才培養(yǎng)方案調(diào)研報(bào)告書
- 企業(yè)標(biāo)準(zhǔn)自我聲明公開
- 大學(xué)生創(chuàng)新創(chuàng)業(yè)(微課版第3版)課件 第1、2章 了解創(chuàng)業(yè)規(guī)劃你的職業(yè)生涯、創(chuàng)新與創(chuàng)新思維
- E時(shí)代大學(xué)英語-讀寫教程2 第四單元
- 四年級語文上冊第一單元單元整體教學(xué)設(shè)計(jì)
- 玩具安全標(biāo)準(zhǔn)測試培訓(xùn)-(SGS)課件
- 員工工資條模板
- 高考英語備考-英語單詞構(gòu)詞法詞根和詞綴課件
- 病例報(bào)告表格模板CRF
- 中公中學(xué)教育知識與能力(對應(yīng)中公教材)課件
- 電動托盤車(搬運(yùn)車)培訓(xùn)-課件
評論
0/150
提交評論