#include <stdio.h>
#include <cstring>
#include <cstdlib>
#include <conio.h>
struct head
{
unsigned char b; //record the position
long count; //frequency
long parent,lch,rch; //tree variables
char bits[256]; //tree size
}
header[512],tmp;
void compress() //compression function
{
char filename[256],outputfile[256],buf[512];
unsigned char c;
long i,j,m,n,f;
long min1,pt1,flength,length1,length2;
double div;
FILE *ifp,*ofp;
printf(" Please input your file or drag it to console (file name should be
short):");
gets(filename);
ifp=fopen(filename,"rb");
if(ifp==NULL)
{
printf(" Fail to open the file!");
return;
}
printf(" Please enter the file name after compression:");
gets(outputfile);
ofp=fopen(strcat(outputfile,".compressed"),"wb");
if(ofp==NULL)
{
printf(" Fail to compress!");
return;
}
flength=0;
while(!feof(ifp))
{
fread(&c,1,1,ifp);
header[c].count++;
flength++;
}
flength--;
length1=flength;
header[c].count--;
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++)