當前位置:編程學習大全網 - 源碼下載 - C++自適應霍夫曼編碼程序源代碼。

C++自適應霍夫曼編碼程序源代碼。

# include & ltstdio.h & gt

# include & ltstring.h & gt

# include & ltstdlib.h & gt

# include & ltconio.h & gt

結構頭

{

無符號字符b;

長計數;

long parent,lch,rch

字符位[256];

}

表頭[512],tmp

無效壓縮()

{

char文件名[255],outputfile[255],buf[512];

無符號字符c;

long i,j,m,n,f;

龍min1,pt1,flength

FILE *ifp,* ofp

Printf("源文件名(在當前目錄下):");

獲取(文件名);

ifp=fopen(文件名,“Rb”);

if(ifp==NULL)

{

printf("錯誤!\ n ");

返回;

}

Printf("生成文件名:");

獲取(output file);

ofp=fopen(outputfile,“WB”);

if(ofp==NULL)

{

printf("錯誤!\ n ");

返回;

}

f length = 0;

而(!外國投資者協會

{

弗雷德(& ampc,1,1,ifp);

標題[c]。count++;

f length++;

}

f length-;

標題[c]。count-;

for(I = 0;我& lt512;i++)

{

if(header[i]。數數!=0)頭[i]。b=(無符號字符)I;

else頭[i]。b = 0;

標題[i]。parent =-1;

標題[i]。lch=header[i]。rch =-1;

}

for(I = 0;我& lt256;i++)

{

for(j = I+1;j & lt256;j++)

{

if(header[i]。計數& lt標題[j]。計數)

{

tmp = header[I];

頭[I]=頭[j];

header[j]= tmp;

}

}

}

for(I = 0;我& lt256;i++) if(header[i]。count = = 0)break;

n = I;

m = 2 * n-1;

for(I = n;我& ltm;i++)

{

min 1 = 99999999;

for(j = 0;j & lt我;j++)

{

if(頭[j].家長!=-1)繼續;

if(min 1 & gt;標題[j]。計數)

{

pt 1 = j;

min1=header[j]。數數;

繼續;

}

}

標題[i]。count=header[pt1]。數數;

標題[pt1]。parent = I;

標題[i]。lch = pt 1;

min 1 = 99999999;

for(j = 0;j & lt我;j++)

{

if(頭[j].家長!=-1)繼續;

if(min 1 & gt;標題[j]。計數)

{

pt 1 = j;

min1=header[j]。數數;

繼續;

}

}

標題[i]。count+=header[pt1]。數數;

標題[i]。rch = pt 1;

標題[pt1]。parent = I;

}

for(I = 0;我& ltn;i++)

{

f = I;

標題[i]。位[0]= 0;

while(標題[f].家長!=-1)

{

j = f;

f =標題[f]。父母;

if(標題[f].lch==j)

{

j=strlen(header[i].位);

memmove(header[i].位+1,標題[i]。位,j+1);

標題[i]。位[0]=“0”;

}

其他

{

j=strlen(header[i].位);

memmove(header[i].位+1,標題[i]。位,j+1);

標題[i]。位[0]= ' 1 ';

}

}

}

fseek(ifp,0,SEEK _ SET);

fwrite(& amp;flength,sizeof(int),1,ofp);

fseek(ofp,8,SEEK _ SET);

buf[0]= 0;

f = 0;

pt 1 = 8;

而(!外國投資者協會

{

c = fgetc(ifp);

f++;

for(I = 0;我& ltn;i++)

{

if(c==header[i]。b)斷裂;

}

strcat(buf,header[i].位);

j = strlen(buf);

c = 0;

while(j & gt;=8)

{

for(I = 0;我& lt8;i++)

{

if(buf[I]= = ' 1 ')c =(c & lt;& lt1)|1;

else c = c & lt& lt1;

}

fwrite(& amp;c,1,1,ofp);

pt 1++;

strcpy(buf,buf+8);

j = strlen(buf);

}

if(f = = f length)break;

}

if(j & gt;0)

{

strcat(buf," 00000000 ");

for(I = 0;我& lt8;i++)

{

if(buf[I]= = ' 1 ')c =(c & lt;& lt1)|1;

else c = c & lt& lt1;

}

fwrite(& amp;c,1,1,ofp);

pt 1++;

}

fseek(ofp,4,SEEK _ SET);

fwrite(& amp;pt1,sizeof(long),1,ofp);

fseek(ofp,pt1,SEEK _ SET);

fwrite(& amp;n,sizeof(long),1,ofp);

for(I = 0;我& ltn;i++)

{

fwrite(& amp;(標題[i]。b),1,1,ofp);

c=strlen(header[i].位);

fwrite(& amp;c,1,1,ofp);

j=strlen(header[i].位);

if(j%8!=0)

{

for(f = j % 8;f & lt8;f++)

strcat(header[i].位,“0”);

}

while(header[i]。位[0]!=0)

{

c = 0;

for(j = 0;j & lt8;j++)

{

if(header[i]。bits[j]= = ' 1 ')c =(c & lt;& lt1)|1;

else c = c & lt& lt1;

}

strcpy(header[i].位,標題[i]。位+8);

fwrite(& amp;c,1,1,ofp);

}

}

fclose(ifp);

fclose(ofp);

Printf("壓縮完成\ n ");

}

void解壓縮()

{

char文件名[255],outputfile[255],buf[255],bx[255];

無符號字符c;

long i,j,m,n,f,p,l;

長纖維長度;

FILE *ifp,* ofp

Printf("源文件名(在當前目錄下):");

獲取(文件名);

ifp=fopen(文件名,“Rb”);

if(ifp==NULL)

{

printf("錯誤!\ n ");

返回;

}

Printf("生成文件名:");

獲取(output file);

ofp=fopen(outputfile,“WB”);

if(ofp==NULL)

{

printf("錯誤!\ n ");

返回;

}

弗雷德(& ampflength,sizeof(long),1,ifp);

弗雷德(& ampf,sizeof(long),1,ifp);

fseek(ifp,f,SEEK _ SET);

弗雷德(& ampn,sizeof(long),1,ifp);

for(I = 0;我& ltn;i++)

{

弗雷德(& amp標題[i]。b,1,1,ifp);

弗雷德(& ampc,1,1,ifp);

p =(long)c;

標題[i]。count = p;

標題[i]。位[0]= 0;

if(p % 8 & gt;0)m = p/8+1;

else m = p/8;

for(j = 0;j & ltm;j++)

{

弗雷德(& ampc,1,1,ifp);

f = c;

itoa(f,buf,2);

f = strlen(buf);

for(l = 8;l & gtf;l -)

{

strcat(header[i].位,“0”);

}

strcat(header[i].bits,buf);

}

標題[i]。位[p]= 0;

}

for(I = 0;我& ltn;i++)

{

for(j = I+1;j & ltn;j++)

{

if(strlen(header[i].位)>;strlen(header[j].位))

{

tmp = header[I];

頭[I]=頭[j];

header[j]= tmp;

}

}

}

p=strlen(header[n-1])。位);

fseek(ifp,8,SEEK _ SET);

m = 0;

bx[0]= 0;

while(1)

{

while(strlen(bx)& lt;(無符號整數)p)

{

弗雷德(& ampc,1,1,ifp);

f = c;

itoa(f,buf,2);

f = strlen(buf);

for(l = 8;l & gtf;l -)

{

strcat(bx," 0 ");

}

strcat(bx,buf);

}

for(I = 0;我& ltn;i++)

{

if(memcmp(header[i].位,bx,標題[i]。count)= = 0)break;

}

strcpy(bx,bx+header[i].計數);

c =標題[i]。b;

fwrite(& amp;c,1,1,ofp);

m++;

if(m = = f length)break;

}

fclose(ifp);

fclose(ofp);

Printf("成功解碼!\ n ");

}

int main()

{

int c;

while (1) {

Printf("按1選擇編碼\ n ");

Printf("按2選擇解碼\ n ");

Printf("請選擇1或2:");

c = getch();

printf("%c\n ",c);

if(c = = ' 1 ')compress();

else if(c = = ' 2 ')uncompress();

}

返回0;

}

  • 上一篇:為什麽關羽這麽受人敬重
  • 下一篇:Php項目案例源代碼
  • copyright 2024編程學習大全網