版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
WordWord■■哈夫曼編碼解碼實(shí)驗(yàn)1.實(shí)驗(yàn)要求掌握二叉樹(shù)的相關(guān)概念掌握構(gòu)造哈夫曼樹(shù),進(jìn)行哈夫曼編碼。對(duì)編碼容通過(guò)哈夫曼樹(shù)進(jìn)行解碼。2.實(shí)驗(yàn)容通過(guò)二叉樹(shù)構(gòu)造哈夫曼樹(shù),并用哈夫曼樹(shù)對(duì)讀取的txt文件進(jìn)行哈夫曼編碼。編碼完成后通過(guò)哈夫曼樹(shù)進(jìn)行解碼。#include<stdio.h>#include<string.h>#defineMAX100//定義哈夫曼樹(shù)的存儲(chǔ)結(jié)構(gòu)typedefstruct{chardata;intweight;intparent;intlch;intrch;}HuffNode;//定義哈夫曼編碼的存儲(chǔ)結(jié)構(gòu)typedefstruct{charbit[MAX];intstart;}HuffCode;HuffNodeht[2*MAX];HuffCodehcd[MAX];intCoun[127]={0};intn;chars1[200000];chartext[5000];//構(gòu)造哈夫曼樹(shù)voidHuffmanTree(){inti,j,k,left,right,min1,min2;//printf("輸入葉子的節(jié)點(diǎn)數(shù):");//scanf("%d",&n);printf("字符數(shù)量=%d\n",n);for(i=1;i<=2*n-1;i++){ht[i].parent=ht[i].lch=ht[i].rch=0;}j=0;for(i=1;i<=n;i++){/*getchar();printf("輸入第%~個(gè)葉子節(jié)點(diǎn)的值:",i);scanf("%c",&ht[i].data);printf("輸入該節(jié)點(diǎn)的權(quán)值:");scanf("%d",&ht[i].weight);*/for(;j<127;j++){if(Coun[j]!=0){ht[i].data=j;//printf("%c",ht[i].data);ht[i].weight=Coun[j];//printf("%d",ht[i].weight);break;}}j++;}printf("\n");for(i=1;i<=n;i++){printf("%c",ht[i].data);}printf("\n");for(i=n+1;i<=2*n-1;i++){//在前n個(gè)結(jié)點(diǎn)中選取權(quán)值最小的兩個(gè)結(jié)點(diǎn)構(gòu)成一顆二叉樹(shù)min1=min2=10000;//為min1和min2設(shè)置一^個(gè)比所有權(quán)值都大的值left=right=0;for(k=1;k<=i-1;k++)if(ht[k].parent==0)//若是根結(jié)點(diǎn)//令mini和min2為最小的兩個(gè)權(quán)值,left和right為權(quán)值最小的兩個(gè)結(jié)點(diǎn)位置if(ht[k].weight<min1){min2=min1;right=left;min1=ht[k].weight;left=k;}elseif(ht[k].weight<min2){min2=ht[k].weight;right=k;ht[left].parent=i;ht[right].parent=i;ht[i].weight=ht[left].weight+ht[right].weight;ht[i].lch=left;ht[i].rch=right;//構(gòu)造哈夫曼編碼voidHuffmanCode(){inti,c,k,f;HuffCodecd;for(i=1;i<=n;i++){cd.start=n;c=i;f=ht[i].parent;while(f!=0){if(ht[f].lch==c)cd.bit[cd.start]='0';elsecd.bit[cd.start]='1';cd.start--;WordWord■■WordWordc=f;f=ht[f].parent;}hcd[i]=cd;}printf("輸出哈夫曼編碼:\n");for(i=1;i<=n;i++){printf("%c:",ht[i].data);for(k=hcd[i].start+1;k<=n;k++)printf("%c",hcd[i].bit[k]);printf("\n");}}//對(duì)字母進(jìn)行編碼voidCode()〃將字符與相應(yīng)的哈夫曼編碼進(jìn)行匹配,輸出編碼結(jié)果{inti=0,j,k,h=0;while(text[i]!='\0'){for(j=1;j<=n;j++){if(text[i]==ht[j].data){for(k=hcd[j].start+1;k<=n;k++){s1[h]=hcd[j].bit[k];h++;}break;}}i++;}//printf("編碼\n");//puts(s1);//printf("\n");}//解碼voidHuffmanDecode(){printf("解碼\n");intlen,i,f;charC;//charS[MAXCODE];//$。2/("%5"5);//使用gets()直接跳過(guò)len=strlen(s1);printf("s1:%d\n",len);f=2*n-1;for(i=0;i<len;i++){if(s1[i]=='0'){f=ht[f].lch;if(ht[f].lch==0&&ht[f].rch==0){C=ht[f].data;printf("%c",C);f=2*n-1;}}elseif(s1[i]=='1'){f=ht[f].rch;if(ht[f].lch==0&&ht[f].rch==0){C=ht[f].data;printf("%c",C);f=2*n-1;}}}printf("\n");}//統(tǒng)計(jì)字母?jìng)€(gè)數(shù)及其權(quán)值voidCount(){inti,j,m;n=0;i=0;//printf("請(qǐng)僅輸入小寫字母\n");〃例程本省存在一個(gè)BUG,只輸入一個(gè)字母不能進(jìn)行編碼(并未解決)//scanf("%s",s);while(text[i]!=''0')//使用ASCII碼表進(jìn)行統(tǒng)計(jì){m=text[i];//printf("%d\n",m);Coun[m]++;i++;}for(j=0;j<127;j++){if(Coun[j]!=0)n++;}}//markCodevoidmain(){intl=0;FILE*fp;fp=fopen("text.txt","r");if(fp==NULL){printf("文件打開(kāi)失敗\n");while(1);}while(!feof(fp)){text[l]=fgetc(fp);l++;}printf("輸入文本\n");printf("%s\n",text);fclose(fp);Count();
HuffmanTree();HuffmanCode();Code();HuffmanDecode();}文本文件]-記王在ISiWl裝及出格式?j查Ma即HiAInth?bng_1mning,G-od仁irga+m出thshgmvo-iis;and1tha?sarth.Hdiht?ar-th口awfom.]Bi=s=and?snptypdlairkxirias?2meotsethesurfacBoftbede0pP審ndtheSpiritofGodwashoveriniBoverthewaters.AndGods審id."Letnthere1>blight,,andtherew?*3light.GadsairttattheliiihtwsgoodiandheEsparotsdthelightfrom5thed^rfcneBS.God四片11血thelight*d輯y."thedqrknesBhecalled,night?"AndthereW45eveniiiKj.andtherevasmDrnine7thefiz/irtd&y,如idGodsaida"Lsttherebeanez^anseteetseenthetoEeparatewterfromwaterBASa^od山此the日工andaagdlthewaterund白nthe日期皿何fromthawater吊曲buveituAndit池白白如GodglLedthaFky「Aiidthar日was外0口訕噩取百日thsra墻的biwrnijig-tlxBIsecoridid.:&v-todsfiid3"LetThemteruridertheslk步beEfitlhg丘。taohig^lace.H皿letdrycromdappe-ar.wfcidItwais2D.God.calledthie-'dry卷irnurid*1-B.ndpandthegathEre-d-強(qiáng)teushecalled"neas."AirtdGad£awthatitwas-good.TluRnG口同sa.i.dpLastthia1強(qiáng)門城praducavago-tatiani:FB?idl-[b@aiiri.ngplunit總修門通tms;onUb1-andthatIbearfruitwithGBBdinit?ACGording.tothsirvariauskinds,Anditr#asso.ThelandproducBdveeetAtiani:plantsbearins日daccDrdinstotheirkindsandtreesbearingfruitwithseedinitBccording:totheirJrintfe,AndGod后a*thatitwmsgaod.AndtherewusBveni-nKjandtherewas?,IreomiTiE-thethirdday.AndGodS4idpLettharebelishtsinthaexpAnseoftheflkytoseparatethedayfromthenighty國(guó)mdletthe瓦aexveassigi^tomarkseasonsandaiidyears,曲占let^hi&sibeligiltsin.the舊乂戶曲皿日oithe曲卡togivelishlonliteearttu*Andit血與曰口.值口式mdetwereaitLitht-s-thegreaterLig^itocavernthedavanmdtheLeaserli^ht工口fidvemthenight.Healsomadethestars.G>o。setthemiintheexpanse-?費(fèi)thes:kytogi¥e1ifhtantheE-airthptcg,口貨astmthadayand.theniE.htnand<dscpar;a.t>Dlightfrsindarkne^ifi;.AndG口由saiwthatit看強(qiáng)石goiod.Andtheir*mnahovisnirirg.,andthorewasmamins-thefourthday.J*kndGodsaidT"LgtttK>mtert日好mwithlivingcrBatureEP4ndletbirdsflyabovetheearthacrossth日expansoofthesltyTSoGodcrBatsdthegraatcreaturos:ofttisEB3審ndBveryliving:andthingmth品ichthewnterteem8aaccordinstothsirkiTids,“4indeverywingsd?,toitskind.AndGodsawth^titwsb乳口口d?Godb-lessedthem4ndBAidj"Refruitlulandincreaseinilili&ema*fillthe?aterin.theafidletthebirdsi£Lcre^s?onthe白azt瓦"Aftd.there中出晝avenLngi.andtharewasno或皿1皿一寸如白litthdayBAndCodEftid(th?Imdl。=口duc^livxio^creaturesaccoriilnstotheirkinds:livestock,creaturesthat皿邛日alonetheground,and皆ildanimls.eadi.accordingtoitsklndL蒯dlItwasso.Co向血口白The窗ililaccordihik^totheir3cS.nd^i?the1iveatockactoralrigt<othffi-irk£ndsaandallttbEcreaturesthatm口"史-blongtheGroundaccardingt口theia~klndiSLAndGudsawthaiit11x口口cLThssnG-ods*3idp*1>向*usfuakomacn£ndut1inagD-piriioletlifc^n.'HSFEpand]IsstttismrulBovertho-fishofthesbaandthebirdsafthe^ir7ovsrthelirsEtocl;,overzbllth日Barthr己ndover司]1theIcreaturesthatwinve01Mg:thsgroumd.SoGod€rBAtBdmaninIhisownimgR,inWeimageofGodhe文本輸入SEDOWS\sys.tem32\cmd.exebsanslthenight,andlettheir,seryea£signstjmarkletthenibelightsintheexpanE:Roftaeskytc半:elighttroindarkness.AndGadsawthatit評(píng)asscod.Andthere即之SEDOWS\sys.tem32\cmd.exebsanslthenight,andlettheir,seryea£signstjmarkletthenibelightsintheexpanE:Roftaeskytc半:elighttroindarkness.AndGadsawthatit評(píng)asscod.Andthere即之slasevening:,andthsrewacirmnin目一thefirstday.AndGodsaid:Letthere|patitwzwgood.Andthere用之工evening,andthere|GodEaid,''"Letthtrebmlightsintheesi:anseof耳浜叱手KEHL■一口反吏1日沐口113口5QLNFRj唉心L叫?曰叱旦i、;也由八網(wǎng)否:yTlLie|淡e副ntheb昭inningGodcreatedtheheavensandtheearth.Nowtheearthwasiormle"1ssandBinpty,darknesswasoverthesurfaceofthedeep,andtheSpJiritofGodw^ashoveringoverthewaters.AndGodsaid,r/Lettherebelight,'andtherewas1JishtuQjdea-A;thatthelight-.vasgood,andheeepar:?.tedthelightfromthedarkr^Eiss.Godcalledt.lie1igrit''Azy."andthedarknesEhecalled^r.ight,"Andthere3"t:.va£sc-Godnadst-.-ogreatlightc-thegreater1lghttogovernthedayar.dthelesserlighttcgrvernthenight」Healsamadethestars,GodsetthenintheeKp:mscofth已Likyt口give1ightont.hucarthntogovernthedcyandtheni.ghtnaTfKpansebef.VHHnthewaterstoseparatev;aterironwater,"Sc(jcdmdetheBJipanse6andseparatedthewaterund匚i?thecz|>ansefrumthewateraboveit-Anditwau□b.Godzslledthe&x:)anE:e"skv.Andthere\^sevening,andtherewaswrning-th_:□cccndda;r.AndCodoaidj"Letthewaterundertheskyb□gctheredtooneplacLandLetdrygroundappear."zAnditv;asszlGodcslledthedrysromd""Lsndazzpndthegatheredwatershecalled"seaE.''AndGodsavrthatitwasgood.ThenCod[4:=:aid,rzLntthslandprodue:pvegntation:seed-bnarirgplants工nd十r門曰三onthela匕d.thatbearfruitwith占巳巳diniInaccordingtotheirvariouskinds.,Anditwa=:u口.Thslandproducedvegetation:plantebearings自u(píng)darcordingtotheirkinds?andtree5buaringfruitwithseedinitaccordingtutheirkinds.AndGodsavtwa:-morning-thethirdday.AndH:.hest:ytoEep&ra:.e:.hecayfrlseasonsanddaymandy-eaiSjanH11givelightcntheearth,And■進(jìn)行哈夫曼編碼SIC:\WINDOWS\systeri32\cmd.exe,rpsnplantforfood."Anditwassa.jodsawallthathehadrnads,anditwasve|?iygood.Andtherewasevening,andtherewasmorning-thesixthday.H;1110010000100010101D11110011111001001進(jìn)行哈夫曼編碼SIC:\WINDOWS\systeri32\cmd.exe,rpsnplantforfood."Anditwassa.jodsawallthathehadrnads,anditwasve|?iygood.Andtherewasevening,andtherewasmorning-thesixthday.H;1110010000100010101D111100111110010011111010101010000010101D0010111100101111001000001010001T:1010100001;10101D010:111001000011:10101D00100:10101D0011:111CO1O0O1:1011%軟芬音半:elightfromdarkness.AndGodsawthatitwasgood.Andthere卵a(bǔ)srr,.:;ABGHILNRSTabcdefghiklnmDprstuvwxy『輸出哈夫曼編碼:對(duì)文本進(jìn)行編碼mt鎏2AWINDOWS\systeri32\cmd.exemtIIOIOIOODOIIOOIOOIIOODIOOOIIOOIDIOOOOOIIIIIIOOIIOIIIOOIIOOIIIOIIIOOIUIIOODOIOIOO0111101100000010111111110111011110001110D0001100010001100010001110110101010111001110100D101110011000D011Q00100D110Q011101111111110001D011110100D111Q01000D1111101101001D011000100011D0011101111111110001D000101001101111010001111011111011111101OlOOlllDOOOlinOlOllDlOOOlOlllDOllOOOOOOllOlOlOOOlOllDlllOOlOlOllOlllOOllDOlOOOl101111111010110010010111101011010001010011011110100011101010101011111110011000100rOllOOllDlOlOlOlllLllllllllOlllDllOLOlllOllOOUlOllLllOllOOllOOOlOOOLlOOlODOOllOlI-101011011110011001011100110000D11000100011001010100011010110111D111111111D111100F00111011111011001010D011110110D0001010011011110100001D0111010101010111111111011100111110000111010101010111111100110001000110010100110111100011111111101011110100OlllOOlDllOOllOOOOOlDlOOOllllOllOOOOOllOlOlOllllOlLlODOlllOOllODlOlOlOllOlOlOOlOOllllOODOllOOOlOOOiniinOllOOlOlOOOOOllDOlllOOOllOlHlllOOOlOOllOOLllOOlllOlOlO10010111001100000L10D01000111111101100101001101111010D011100011D111111000100110011110100010100011110110000011010101110100100110001001011110000110001000110011100OllOllllllOOOlOOlLOODOlOlOOllOllllOlOOOllllOOlllOlLllDllOOOlllODllOOlOlllDOllOOO000100011001101001101011011011111111011110001110000011000100011D011100011D1111110001001100001111011111111110101010000110001000110010001011111110101100100101111010110101111010001010D011110110D0000101111011111000111D000111000D011000100D110011rlOOOllOllllllOOOlOOllOOOOlOlOlDllOOOlOlllOlOllOlllOOlllOlOlOlOOlOllLOOllODOOOllO00100011001000101111111010110010010111101011010000100D1100010111101111100D11100001110000010101011001110111111000100110011110101010101001110010110011000001100010001111111011001010011011110100D011010101D111001110111D0111110011100L1001011100110000011D0010001111111011001010D110111101D0001010011101111111001110111001111100111微軟拼音半:輸出解碼結(jié)果
噩'2O'iAJS\sy&tem32\cmd.exeI:/:1010110翻碼hl:16674摩InthebtgireninsGadcrisatedtheheavensandtheearth.Nov;theearthwasformle"55andenpty,darkr.esswasuverthesurfaceofthedeep,andtheSpiritofCud河p=j:=:hovnringn^erthep=j:=:hovnringn^erthe皆弓十巾丁丹1sAndGod:=!aid?”Lattherebelight,"閂ndthere幗二1■ight.Godsawthatthelightwatgood,andheseparatedthelightfromthedarkn時(shí)me三Godcalledthslight"d:?y,'andthedarknesshe?allednight."Andthery5①eisevenir.g,andtherewasULuzning-thefirstday.AtidGodsaid:”Lettherebeane^spanse
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024至2030年蛋黃保鮮劑項(xiàng)目投資價(jià)值分析報(bào)告
- 2024至2030年氧化鈦陶瓷項(xiàng)目投資價(jià)值分析報(bào)告
- 陜西藝術(shù)職業(yè)學(xué)院《工程造價(jià)軟件應(yīng)用》2023-2024學(xué)年第一學(xué)期期末試卷
- 陜西學(xué)前師范學(xué)院《機(jī)械CAD》2023-2024學(xué)年第一學(xué)期期末試卷
- 產(chǎn)品購(gòu)銷合同范例教程
- 2024年高頻開(kāi)關(guān)(脈沖)電鍍電源項(xiàng)目可行性研究報(bào)告
- 陜西師范大學(xué)《文本數(shù)據(jù)挖掘》2023-2024學(xué)年第一學(xué)期期末試卷
- 方形燈具采購(gòu)合同范例
- 2024年租賃購(gòu)買合同:商業(yè)房地產(chǎn)租賃權(quán)與購(gòu)買權(quán)
- 2024年心形陰陽(yáng)籃項(xiàng)目可行性研究報(bào)告
- 山東省建筑自動(dòng)消防設(shè)施檢測(cè)收費(fèi)標(biāo)準(zhǔn)
- 高血壓心臟病的護(hù)理查房
- 2023年4月自考11742商務(wù)溝通方法與技能試題及答案
- 食品試驗(yàn)設(shè)計(jì)與統(tǒng)計(jì)分析期末復(fù)習(xí)資料
- 項(xiàng)目計(jì)劃書:3D數(shù)字設(shè)計(jì)和制造平臺(tái)創(chuàng)業(yè)方案
- 航空餐飲服務(wù)的注意事項(xiàng)
- DB42T 1144-2016燃?xì)庥貌讳P鋼波紋軟管安裝及驗(yàn)收規(guī)范
- 二級(jí)醫(yī)院規(guī)章制度匯編
- 2023-2024學(xué)年安徽省合肥市小學(xué)數(shù)學(xué)五年級(jí)上冊(cè)期末自測(cè)題
- GB/T 702-2017熱軋鋼棒尺寸、外形、重量及允許偏差
- 四年級(jí)上冊(cè)英語(yǔ)試題-Unit 12 Peter can jump high 湘少版(含答案)
評(píng)論
0/150
提交評(píng)論