版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領
文檔簡介
WordWord■■哈夫曼編碼解碼實驗1.實驗要求掌握二叉樹的相關概念掌握構(gòu)造哈夫曼樹,進行哈夫曼編碼。對編碼容通過哈夫曼樹進行解碼。2.實驗容通過二叉樹構(gòu)造哈夫曼樹,并用哈夫曼樹對讀取的txt文件進行哈夫曼編碼。編碼完成后通過哈夫曼樹進行解碼。#include<stdio.h>#include<string.h>#defineMAX100//定義哈夫曼樹的存儲結(jié)構(gòu)typedefstruct{chardata;intweight;intparent;intlch;intrch;}HuffNode;//定義哈夫曼編碼的存儲結(jié)構(gòu)typedefstruct{charbit[MAX];intstart;}HuffCode;HuffNodeht[2*MAX];HuffCodehcd[MAX];intCoun[127]={0};intn;chars1[200000];chartext[5000];//構(gòu)造哈夫曼樹voidHuffmanTree(){inti,j,k,left,right,min1,min2;//printf("輸入葉子的節(jié)點數(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("輸入第%~個葉子節(jié)點的值:",i);scanf("%c",&ht[i].data);printf("輸入該節(jié)點的權(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個結(jié)點中選取權(quán)值最小的兩個結(jié)點構(gòu)成一顆二叉樹min1=min2=10000;//為min1和min2設置一^個比所有權(quán)值都大的值left=right=0;for(k=1;k<=i-1;k++)if(ht[k].parent==0)//若是根結(jié)點//令mini和min2為最小的兩個權(quán)值,left和right為權(quán)值最小的兩個結(jié)點位置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");}}//對字母進行編碼voidCode()〃將字符與相應的哈夫曼編碼進行匹配,輸出編碼結(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()直接跳過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)計字母個數(shù)及其權(quán)值voidCount(){inti,j,m;n=0;i=0;//printf("請僅輸入小寫字母\n");〃例程本省存在一個BUG,只輸入一個字母不能進行編碼(并未解決)//scanf("%s",s);while(text[i]!=''0')//使用ASCII碼表進行統(tǒng)計{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("文件打開失敗\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-強teushecalled"neas."AirtdGad£awthatitwas-good.TluRnG口同sa.i.dpLastthia1強門城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國mdletthe瓦aexveassigi^tomarkseasonsandaiidyears,曲占let^hi&sibeligiltsin.the舊乂戶曲皿日oithe曲卡togivelishlonliteearttu*Andit血與曰口.值口式mdetwereaitLitht-s-thegreaterLig^itocavernthedavanmdtheLeaserli^ht工口fidvemthenight.Healsomadethestars.G>o。setthemiintheexpanse-?費thes:kytogi¥e1ifhtantheE-airthptcg,口貨astmthadayand.theniE.htnand<dscpar;a.t>Dlightfrsindarkne^ifi;.AndG口由saiwthatit看強石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評asscod.Andthere即之SEDOWS\sys.tem32\cmd.exebsanslthenight,andlettheir,seryea£signstjmarkletthenibelightsintheexpanE:Roftaeskytc半:elighttroindarkness.AndGadsawthatit評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自udarcordingtotheirkinds?andtree5buaringfruitwithseedinitaccordingtutheirkinds.AndGodsavtwa:-morning-thethirdday.AndH:.hest:ytoEep&ra:.e:.hecayfrlseasonsanddaymandy-eaiSjanH11givelightcntheearth,And■進行哈夫曼編碼SIC:\WINDOWS\systeri32\cmd.exe,rpsnplantforfood."Anditwassa.jodsawallthathehadrnads,anditwasve|?iygood.Andtherewasevening,andtherewasmorning-thesixthday.H;1110010000100010101D11110011111001001進行哈夫曼編碼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卵asrr,.:;ABGHILNRSTabcdefghiklnmDprstuvwxy『輸出哈夫曼編碼:對文本進行編碼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時me三Godcalledthslight"d:?y,'andthedarknesshe?allednight."Andthery5①eisevenir.g,andtherewasULuzning-thefirstday.AtidGodsaid:”Lettherebeane^spanse
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 廣東省肇慶市2024年中考一模數(shù)學試題含答案
- 晉中學院《數(shù)字化教學資源設計與開發(fā)(C)》2023-2024學年第一學期期末試卷
- 淮陰工學院《豎向設計A》2023-2024學年第一學期期末試卷
- 【物理】第九章壓強 復習++2024-2025學年人教版物理八年級下冊
- 黑龍江八一農(nóng)墾大學《大數(shù)據(jù)審計虛擬仿真實驗》2023-2024學年第一學期期末試卷
- 浙江育英職業(yè)技術學院《火電廠典型控制與保護策略專題研討》2023-2024學年第一學期期末試卷
- 浙江廣廈建設職業(yè)技術大學《企業(yè)虛擬仿真綜合實訓》2023-2024學年第一學期期末試卷
- 長治職業(yè)技術學院《土木工程結(jié)構(gòu)抗震》2023-2024學年第一學期期末試卷
- 云南外事外語職業(yè)學院《GIS軟件應用實驗(一)》2023-2024學年第一學期期末試卷
- 企業(yè)社會責任在價值鏈中的作用機理
- 常用靜脈藥物溶媒的選擇
- 當代西方文學理論知到智慧樹章節(jié)測試課后答案2024年秋武漢科技大學
- 2024年預制混凝土制品購銷協(xié)議3篇
- 2024-2030年中國高端私人會所市場競爭格局及投資經(jīng)營管理分析報告
- GA/T 1003-2024銀行自助服務亭技術規(guī)范
- 《消防設備操作使用》培訓
- 新交際英語(2024)一年級上冊Unit 1~6全冊教案
- 2024年度跨境電商平臺運營與孵化合同
- 2024年電動汽車充電消費者研究報告-2024-11-新能源
- 湖北省黃岡高級中學2025屆物理高一第一學期期末考試試題含解析
- 上海市徐匯中學2025屆物理高一第一學期期末學業(yè)水平測試試題含解析
評論
0/150
提交評論