哈夫曼文本壓縮C語言實(shí)現(xiàn)_第1頁
哈夫曼文本壓縮C語言實(shí)現(xiàn)_第2頁
哈夫曼文本壓縮C語言實(shí)現(xiàn)_第3頁
哈夫曼文本壓縮C語言實(shí)現(xiàn)_第4頁
哈夫曼文本壓縮C語言實(shí)現(xiàn)_第5頁
已閱讀5頁,還剩2頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

本文格式為Word版,下載可任意編輯——哈夫曼文本壓縮C語言實(shí)現(xiàn)/*文件中有些參數(shù)定義的比較大,主要是為了適應(yīng)較大文件的壓縮*/#include#include#include

#include//用以刪除多余的中間文件#defineM100000000000//最大字符數(shù)

intop,co[100];//編碼表的掃描指針,簡(jiǎn)易棧co[]typedefstructHfnode//哈弗曼樹結(jié)點(diǎn)類型{

intdata;//權(quán)值域charzimu;//存儲(chǔ)字母

structHfnode*Lson,*Rson,*next;//兒子鏈域和森林鏈域

}Hfnode,*Hfptr;

typedefstructsnode//靜態(tài)數(shù)組結(jié)點(diǎn)類型{

charc;//字符intf1;//頻度}snode;

typedefstructLnode//編碼表結(jié)點(diǎn)類型{charc;structLnode*next;}Lnode,*Lptr;

voidgotoxy(intx,inty){COORDcoord;coord.X=x;coord.Y=y;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);}

voidpaint1(){

inti,j;for(j=0;jc=fgetc(fp)p->next=newLnode;p=p->next;

/*putchar(ch[i]);*/i++;

}

putchar(10);fclose(fp);return(i-1);}

intstat(Lptrp,snodech2[],intnum)//統(tǒng)計(jì)函數(shù){

inti,j,last=0;for(i=0;ic)j++;if(j==last){}

ch2[j].c=p->c;ch2[j].f1=1;last++;

elsech2[j].f1++;p=p->next;}

return(last);}

voidorder(snodech2[],intn)//冒泡排序{inti,j,k,t;

chara;

for(i=0;idata=M;//構(gòu)造監(jiān)視原結(jié)點(diǎn)for(i=0;inext=newHfnode;//申請(qǐng)結(jié)點(diǎn)

p=p->next;p->Lson=p->Rson=NULL;//做葉節(jié)點(diǎn)}

p->zimu=ch2[i].c;

p->data=ch2[i].f1;//讀入葉之權(quán)wi

p->next=h;//構(gòu)成循環(huán)鏈returnh;

}

HfptrcreatHftree(Hfptrhead,intn)//構(gòu)造樹{

inti;

Hfptrp,q,r,t1,t2;

for(i=1;inext;//t1,t2指向兩棵權(quán)最小的樹根t2=t1->next;

r->data=t1->data+t2->data;//權(quán)值相加r->Lson=t1;//t1,t2做新根r的左右兒子

r->Rson=t2;head->next=t2->next;//從森林中刪去t1,t2

q=head;//準(zhǔn)備進(jìn)行有序插入

p=head->next;//p是探尋指針,q是p的前驅(qū)while(1)//循環(huán)的為r尋覓有序位置

if(p->datadata)

{q=p;p=p->next;}//尚未找到時(shí),繼續(xù)循環(huán)

else{r->next=p;q->next=r;break;}//找到后,插入p}//終止for循環(huán)

p=head->next;

deletehead;//刪去監(jiān)視元

return(p);//返回huffman樹之根}

//構(gòu)造完畢

Lptrpop(intlast){}

voidcodelist(Hfptrp,Lnodelist[],intlast,inta)//造編碼表的函數(shù){if(p->Lson==NULL}

chartrans(inta[])//8位01整數(shù)轉(zhuǎn)換為10進(jìn)制數(shù){

intj=0,y=0;do

{

y=y+a[j]*(int)pow(2,7-j%8);j++;

list[op].next=pop(last);op++;

return;}

codelist(p->Lson,list,last+1,co[last]=0);codelist(p->Rson,list,last+1,co[last]=1);inti;Lptrhead,p;head=p=newLnode;for(i=0;ic=co[i];

p->next=newLnode;p=p->next;

}

p->c=-1;

return(head);

}while(j%8);return(y);

}

intrestoretree(snodech2[],intnum2,intnum1,charoutfile[])

{}

FILE*out1;inti=0,j=0;

if((out1=fopen(outfile,\{

printf(\文件開啟錯(cuò)誤!\\n\exit(0);

}

fwrite(fwrite(j=j+4;

for(i=0;ic!=list[j].c)j++;p=list[j].next;i++;

q=q->next;j=0;

while(p->c!=-1p=p->next;

top++;if(top==8)

{fputc(trans(a),out);

top=0;

n++;}}}

if(topLson;elsep=p->Rson;

}

if(!feof(out1)fclose(out2);

DeleteFile(\if(i==-1)return0;elsereturn1;

voidmain(){

intnum1,num2,num3,choice,choice2;charoutfile[30];Lptrstrhead;

snodech2[300];

Lnodelist[3000];//編碼表HfptrHfroot,head;do

{gotoxy(25,0);

printf(\小型文本壓縮器\\n\

printf(\請(qǐng)輸入需要進(jìn)行的操作:\\n1——壓縮;2——解壓\\n\strhead=newLnode;op=0;

scanf(\gotoxy(0,3);

if(choice==1)//壓縮部分{paint1();//優(yōu)化界面函數(shù)

num1=putdata(strhead);//輸入函數(shù),num1記錄字符總數(shù)num2=stat(strhead,ch2,num1);//統(tǒng)計(jì)函數(shù),num2記錄葉子數(shù)order(ch2,num2);//排序函數(shù)

head=inition(num2,ch2);//調(diào)用初始化函數(shù)

Hfroot=creatHftree(head,num2);//調(diào)用造樹函數(shù)codelist(Hfroot,list,0,0);//造編碼表gotoxy(0,8);

scanf(\

gotoxy(0,11);

printf(\請(qǐng)稍后...\

num3=restoretree(ch2,num2,num1,outfile);//存儲(chǔ)哈夫曼樹

num3=num3+compress(strhead,list,num1,outfile);//壓縮函數(shù),num3記錄壓縮后的字gotoxy(0,11);

printf(\計(jì)算壓縮率gotoxy(0,13);

符數(shù)

}{

elseif(choice==2)//還原部分

paint2();//優(yōu)化界面函數(shù)if(recover(ch2))

printf(\操作成功!\

gotoxy(0,1

溫馨提示

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

最新文檔

評(píng)論

0/150

提交評(píng)論