level 9
void yasuo() /*压缩*/{ char filename[255],outputfile[255],buf[512]; unsigned char c; char wenjianming[255]; long i,j,m,n,f; long min1,pt1,flength; FILE *ifp,*ofp; printf("输入文件地址及文件名:"); gets(filename); ifp=fopen(filename,"rb"); /*打开源文件*/while(ifp==NULL){ printf("打开文件出错\n"); printf("重新输入文件地址及文件名"); gets(filename); ifp=fopen(filename,"rb"); }printf("输入压缩后的文件地址和文件名及后缀:"); gets(wenjianming); ofp=fopen(wenjianming,"wb"); /*创建并打开目的文件*/while(ofp==NULL){printf("重新输入压缩后的文件地址和文件名及后缀:"); ofp=fopen(wenjianming,"wb"); } flength=0; while(!feof(ifp)) /*读取ifp文件*/ { fread(&c,1,1,ifp); /*按位读取*/ header[c].count++; flength++; } flength-1; header[c].count-1; /*读取文件结束*/
2013年05月11日 16点05分
1
level 9
for(i=0;i<512;i++) /*构造哈弗曼树*/ { if(header[i].count!=0) header[i].b=(unsigned char)i; else header[i].b=0; header[i].parent=-1; header[i].lch=header[i].rch=-1; } for(i=0;i<256;i++) { for(j=i+1;j<256;j++) { if(header[i].count<header[j].count) { tmp=header[i]; header[i]=header[j]; header[j]=tmp; } } } for(i=0;i<256;i++) if(header[i].count==0) break;
n=i; m=2*n-1; for(i=n;i<m;i++) { min1=999999999; for(j=0;j<i;j++) { if(header[j].parent!=-1) continue; if(min1>header[j].count) { pt1=j; min1=header[j].count; continue; } } header[i].count=header[pt1].count; header[pt1].parent=i; header[i].lch=pt1; min1=999999999; for(j=0;j<i;j++) { if(header[j].parent!=-1) continue; if(min1>header[j].count) { pt1=j; min1=header[j].count; continue; } } header[i].count+=header[pt1].count; header[i].rch=pt1; header[pt1].parent=i; }
2013年05月11日 16点05分
2
level 9
for(i=0;i<n;i++) { f=i; header[i].bits[0]=0; while(header[f].parent!=-1) { j=f; f=header[f].parent; if(header[f].lch==j) { j=strlen(header[i].bits); memmove(header[i].bits+1,header[i].bits,j+1); header[i].bits[0]='0'; } else { j=strlen(header[i].bits); memmove(header[i].bits+1,header[i].bits,j+1); header[i].bits[0]='1'; } } } /*哈弗曼构造结束*/
2013年05月11日 16点05分
3
level 9
fseek(ifp,0,SEEK_SET); /*把文件指针指向文件的开头*/ fwrite(&flength,sizeof(int),1,ofp); //把哈弗曼代码写入ofp文件 fseek(ofp,8,SEEK_SET); buf[0]=0; f=0; pt1=8; while(!feof(ifp)) { c=fgetc(ifp); //从流中读取一个字符,并增加文件指针的位置 f++; for(i=0;i<n;i++) { if(c==header[i].b) break; } strcat(buf,header[i].bits); //把header[i].bits所指字符串添加到buf结尾处 j=strlen(buf); //计算字符串buf的长度 c=0; while(j>=8) { for(i=0;i<8;i++) { if(buf[i]=='1') c=(c<<1)|1; else c=c<<1; } fwrite(&c,1,1,ofp); pt1++; strcpy(buf,buf+8); j=strlen(buf); } if(f==flength) break; } if(j>0) { strcat(buf,"00000000"); for(i=0;i<8;i++) { if(buf[i]=='1') c=(c<<1)|1; else c=c<<1; } fwrite(&c,1,1,ofp); pt1++; } fseek(ofp,4,SEEK_SET); /*fseek 用于二进制方式打开的文件,移动文件读写指针位置.第一个是文件流,第3个是指针零点位置,第2个是把指针移动到的地点. */ fwrite(&pt1,sizeof(long),1,ofp); /*是要输出数据的地址,每次写入的位数,数据项的个数,目标文件地址*/ fseek(ofp,pt1,SEEK_SET); fwrite(&n,sizeof(long),1,ofp); for(i=0;i<n;i++) { fwrite(&(header[i].b),1,1,ofp); c=strlen(header[i].bits); fwrite(&c,1,1,ofp); j=strlen(header[i].bits); if(j%8!=0) /*按八位读取*/ { for(f=j%8;f<8;f++) strcat(header[i].bits,"0"); } while(header[i].bits[0]!=0) { c=0; for(j=0;j<8;j++) { if(header[i].bits[j]=='1') c=(c<<1)|1; else c=c<<1; } strcpy(header[i].bits,header[i].bits+8); /*把从header[i].bits+8地址开始且含有NULL结束符的字符串赋值到以header[i].bits开始的地址空间 */ fwrite(&c,1,1,ofp); } } fclose(ifp); fclose(ofp); printf("压缩成功\n"); }
2013年05月11日 16点05分
4
level 9
void yasuo() /*压缩*/
{
char filename[255],outputfile[255],buf[512];
unsigned char c;
char wenjianming[255];
long i,j,m,n,f;
long min1,pt1,flength;
FILE *ifp,*ofp;
printf("输入文件地址及文件名:");
gets(filename);
ifp=fopen(filename,"rb"); /*打开源文件*/
while(ifp==NULL)
{
printf("打开文件出错\n");
printf("重新输入文件地址及文件名");
gets(filename);
ifp=fopen(filename,"rb");
}
printf("输入压缩后的文件地址和文件名及后缀:");
gets(wenjianming);
ofp=fopen(wenjianming,"wb"); /*创建并打开目的文件*/
while(ofp==NULL)
{printf("重新输入压缩后的文件地址和文件名及后缀:");
ofp=fopen(wenjianming,"wb");
}
flength=0;
while(!feof(ifp)) /*读取ifp文件*/
{
fread(&c,1,1,ifp); /*按位读取*/
header[c].count++;
flength++;
} flength-1;
header[c].count-1; /*读取文件结束*/
2013年05月12日 04点05分
8
level 9
for(i=0;i<512;i++) /*构造哈弗曼树*/
{
if(header[i].count!=0)
header[i].b=(unsigned char)i;
else header[i].b=0;
header[i].parent=-1;
header[i].lch=header[i].rch=-1;
}
for(i=0;i<256;i++)
{
for(j=i+1;j<256;j++)
{ if(header[i].count<header[j].count)
{ tmp=header[i];
header[i]=header[j];
header[j]=tmp;
}
}
}
for(i=0;i<256;i++)
if(header[i].count==0) break;
n=i;
m=2*n-1;
for(i=n;i<m;i++) {
min1=999999999;
for(j=0;j<i;j++) {
if(header[j].parent!=-1) continue;
if(min1>header[j].count)
{
pt1=j; min1=header[j].count; continue;
}
}
header[i].count=header[pt1].count;
header[pt1].parent=i;
header[i].lch=pt1;
min1=999999999;
for(j=0;j<i;j++)
{ if(header[j].parent!=-1) continue;
if(min1>header[j].count)
{ pt1=j;
min1=header[j].count;
continue;
}
}
header[i].count+=header[pt1].count;
header[i].rch=pt1;
header[pt1].parent=i; }
2013年05月12日 04点05分
9
level 9
for(i=0;i<n;i++)
{
f=i; header[i].bits[0]=0;
while(header[f].parent!=-1)
{ j=f;
f=header[f].parent;
if(header[f].lch==j)
{ j=strlen(header[i].bits);
memmove(header[i].bits+1,header[i].bits,j+1);
header[i].bits[0]='0';
}
else { j=strlen(header[i].bits);
memmove(header[i].bits+1,header[i].bits,j+1);
header[i].bits[0]='1'; } } } /*哈弗曼构造结束*/
2013年05月12日 04点05分
10
level 9
fseek(ifp,0,SEEK_SET); /*把文件指针指向文件的开头*/
fwrite(&flength,sizeof(int),1,ofp); //把哈弗曼代码写入ofp文件
fseek(ofp,8,SEEK_SET);
buf[0]=0; f=0; pt1=8;
while(!feof(ifp))
{
c=fgetc(ifp); //从流中读取一个字符,并增加文件指针的位置
f++;
for(i=0;i<n;i++)
{ if(c==header[i].b) break; }
strcat(buf,header[i].bits); //把header[i].bits所指字符串添加到buf结尾处
j=strlen(buf); //计算字符串buf的长度
c=0;
while(j>=8)
{
for(i=0;i<8;i++)
{ if(buf[i]=='1') c=(c<<1)|1;
else c=c<<1;
}
fwrite(&c,1,1,ofp);
pt1++;
strcpy(buf,buf+8);
j=strlen(buf);
}
if(f==flength) break;
}
if(j>0)
{ strcat(buf,"00000000");
for(i=0;i<8;i++)
{
if(buf[i]=='1') c=(c<<1)|1;
else c=c<<1; } fwrite(&c,1,1,ofp);
pt1++;
}
fseek(ofp,4,SEEK_SET); /*fseek 用于二进制方式打开的文件,移动文件读写指针位置.第一个是文件流,第3个是指针零点位置,第2个是把指针移动到的地点. */
fwrite(&pt1,sizeof(long),1,ofp); /*是要输出数据的地址,每次写入的位数,数据项的个数,目标文件地址*/
fseek(ofp,pt1,SEEK_SET);
fwrite(&n,sizeof(long),1,ofp);
for(i=0;i<n;i++)
{ fwrite(&(header[i].b),1,1,ofp);
c=strlen(header[i].bits); fwrite(&c,1,1,ofp);
j=strlen(header[i].bits);
if(j%8!=0) /*按八位读取*/
{ for(f=j%8;f<8;f++)
strcat(header[i].bits,"0");
}
while(header[i].bits[0]!=0)
{
c=0; for(j=0;j<8;j++)
{ if(header[i].bits[j]=='1') c=(c<<1)|1;
else c=c<<1;
}
strcpy(header[i].bits,header[i].bits+8); /*把从header[i].bits+8地址开始且含有NULL结束符的字符串赋值到以header[i].bits开始的地址空间 */
fwrite(&c,1,1,ofp);
}
}
fclose(ifp);
fclose(ofp);
printf("压缩成功\n"); }
2013年05月12日 04点05分
11