데이터 구조 기 말 작업 - 하프 만 파일 압축

      ,     。      ,           
      



       
    :vc++ 6.0 / vs2010
    :     
  :                       vc++ 6.0    vs2010         


  :
       :
①            ,            ;
②     (         )        (            );
③                              ,       
                 ,         ;
④    ③               
⑤          ,         ,         0,         1,
              0 1    ,               。


   :
typedef struct node
{
    int count;					 //      
    unsigned char ch;			 //         
    int parent,lchild,rchild;    //     
    char bits[256];				 //        ‘010101010101...’
}header[512],temp;


 、    :
①    :         ,    count           ,
           ,     byte,  [(unsigned char)byte].count++。
②      :  header[]  ,             ,                    ,
                     。
③    :      ,                    header[i].bits;
④    :           ,           ,                  。
⑤      :              byte, header[i].bits       (      
         ),               (BIT)     char       ,            
     ,     ,                 。          ,          。
  PS:                 ,     0   




         :          ->         (    )、         
      、       。


 :   
                  ;
        ;    itoa              
     memcmp                       。


  :
        :’ABBCCCDDDD‘
       :
A  '100'
B  '101'
C  '11'
D  '0'
  ->10010110,11111110,000"00000"->     ->150,254,0
               
    ->10  
    ->3  (    )
   ->   150,254,0
   ‘0101...’      ->10010110,11111110,0->  (10010110,11111110,00000000)
  memcmp              。
100(A)101(B)101(B)11(C)11(C)11(C)11(C)0(D)0(D)0(D)0(D)0(D)   0           0        。

/*       */
#include 
#include 
#include 
#include 
#include 
#include 
/*     */
struct node
{
	int count;
	unsigned char ch;
	int parent,lchild,rchild;
	char bits[256];
}header[512],temp;

long huffmancoding(char inputfile[]);
void transfer_to_file(long sum,char inputfile[],char sto[]);
void uncompress(char sto[],char filename[]);
void print();
void print_history();
int print_info();

long n;
/*    */
int cmp(const void *a,const void *b)
{
	long *pa = (long*)a,*pb = (long*)b;
	return (int)((*pb)-(*pa));
}

int main()
{
	int i,k;
	char picture[128];
	char input_file[128],info[128],result_file[128];
	long length;
	char ch;
	int flag_file = 0;
	FILE *coding_history;
	char coding_[128];
	printf("            :
"); printf(" :.
"); printf(" 1: .
"); printf(" 2: .
"); printf(" 3: .
"); printf(" 4: .
"); printf(" 0: .
"); while(1) { scanf("%c*%c",&ch); if(ch == '0') { printf(" .
"); exit(1); } else if(ch=='1') { flag_file = 1; print(); printf(" .
"); scanf("%s",input_file); printf(" .
"); scanf("%s*%c",info); /* */ length = huffmancoding(input_file); /* */ transfer_to_file(length,input_file,info); scanf("%c*%c",&ch); } else if(ch =='2') { if(!print_info()) { scanf("%c*%c",&ch); } else { printf(" .
"); scanf("%s",info); printf(" .
"); scanf("%s",result_file); /* */ uncompress(info,result_file); scanf("%c*%c",&ch); } } else if(ch == '3') { print_history(); scanf("%c*%c",&ch); } else if(ch == '4') { if(flag_file==1) { coding_history = fopen("coding.txt","r"); if(coding_history == NULL) { printf(" .
"); exit(1); } while(!feof(coding_history)) { fscanf(coding_history,"%s",coding_); printf("%s
",coding_); } fclose(coding_history); scanf("%c*%c",&ch); flag_file = 0; } else if(flag_file == 0) { printf(" , .
"); scanf("%c*%c",&ch); } } else { printf(" , .
"); scanf("%c*%c",&ch); } } return 0; } /* */ long huffmancoding(char inputfile[]) { FILE *coding_result; unsigned char ch; int i,j; long m,pt,prelength,ioflength,tmp; long min; FILE *input_file; input_file = fopen(inputfile,"rb"); if(input_file==NULL) { printf(" .
"); exit(1); } prelength=0; while(!feof(input_file)) { fread(&ch,1,1,input_file); header[ch].count++; prelength++; } prelength--; ioflength = prelength; header[ch].count--; for(i=0;i<512;i++) { if(header[i].count!=0) header[i].ch=(unsigned char)i; else header[i].ch=0; header[i].parent=-1;header[i].lchild=header[i].rchild=-1; } qsort(header,512,sizeof(struct node),cmp); for(i=0;i<256;i++) if(header[i].count==0) break; n=i; m=2*n-1; for(i=n;iheader[j].count) { pt=j; min=header[j].count; continue; } } header[i].count=header[pt].count; header[pt].parent=i; header[i].lchild=pt; min=32767; for(j=0;jheader[j].count) { pt=j; min=header[j].count; continue; } } header[i].count+=header[pt].count; header[i].rchild=pt; header[pt].parent=i; } for(i=0;i=8) { for(i=0;i<8;i++) { if(buf[i]=='1') ch=(ch<<1)|1; else ch=ch<<1; } fwrite(&ch,1,1,out_file); pt++; strcpy(buf,buf+8); k=strlen(buf); } if(f==prelength)break; } if(k>0) { strcat(buf,"00000000"); for(i=0;i<8;i++) { if(buf[i]=='1') ch=(ch<<1)|1; else ch=ch<<1; } fwrite(&ch,1,1,out_file); pt++; } fseek(out_file,4,SEEK_SET); fwrite(&pt,sizeof(long),1,out_file); fseek(out_file,pt,SEEK_SET); fwrite(&n,sizeof(long),1,out_file); for(i=0;i0) m=p/8+1; else m=p/8; for(j=0;jf;l--) { strcat(header[i].bits,"0"); } strcat(header[i].bits,buf); } header[i].bits[p]=0; } for(i=0;istrlen(header[j].bits)) { temp=header[i]; header[i]=header[j]; header[j]=temp; } } } p=strlen(header[n-1].bits); fseek(ifp,8,SEEK_SET); m=0; bx[0]=0; while(1) { while(strlen(bx)f;l--) { strcat(bx,"0"); } strcat(bx,buf); } for(i=0;i




좋은 웹페이지 즐겨찾기