데이터 구조 - 파일 압축 과 압축 해제

91279 단어
파일 압축 및 압축 해제 코드
#include
#include
#include
#define maxsize 100  /*      ,             */
#define max 100  /*         */

typedef struct  /*     HUFFNODE   */
{
  char data;    /*     */
  int weight;   /*     */
  int parent;   /*     */
  int left;     /*      */
  int right;    /*      */
}huffnode;

typedef struct  /*     HUFFCODE   */
{
   char cd[max];  /*   cd        */
   int start;
}huffcode;

huffnode ht[max];
huffcode hcd[2*max];

huffnode ht2[max];
huffcode hcd2[2*max];

void input(); 

void init()    /*         */
{
  ht[0].weight=27;
  ht[1].data=' '; ht[1].weight=186;
  ht[2].data='A'; ht[2].weight=64;
  ht[3].data='B'; ht[3].weight=23;
  ht[4].data='C'; ht[4].weight=22;
  ht[5].data='D'; ht[5].weight=32;
  ht[6].data='E'; ht[6].weight=103;
  ht[7].data='F'; ht[7].weight=21;
  ht[8].data='G'; ht[8].weight=15;
  ht[9].data='H'; ht[9].weight=47;
  ht[10].data='I'; ht[10].weight=57;
  ht[11].data='J'; ht[11].weight=1;
  ht[12].data='K'; ht[12].weight=5;
  ht[13].data='L'; ht[13].weight=32;
  ht[14].data='M'; ht[14].weight=20;
  ht[15].data='N'; ht[15].weight=20;
  ht[16].data='O'; ht[16].weight=56;
  ht[17].data='P'; ht[17].weight=19;
  ht[18].data='Q'; ht[18].weight=2;
  ht[19].data='R'; ht[19].weight=50;
  ht[20].data='S'; ht[20].weight=51;
  ht[21].data='T'; ht[21].weight=55;
  ht[22].data='U'; ht[22].weight=30;
  ht[23].data='V'; ht[23].weight=10;
  ht[24].data='W'; ht[24].weight=11;
  ht[25].data='X'; ht[25].weight=2;
  ht[26].data='Y'; ht[26].weight=21;
  ht[27].data='Z'; ht[27].weight=2;

}

void hfmtree(huffnode ht[])  /*          */
{
  int i,k,x1,x2,n,m1,m2;  /* m1     ,x1   ht    ;m2      ,x2     */
  n=ht[0].weight;
  for(i=1;i<=2*n-1;i++)
     ht[i].parent=ht[i].left=ht[i].right=0;  /*  ht parent,left,right       */
  for(i=n+1;i<=2*n-1;i++){
    
	 m1=m2=10000;  /* m1,m2      */
     x1=x2=0;      /* x1, x2   0 */
     for(k=1;k<=i-1;k++)  /* k              */
       if(ht[k].parent==0)     /*              */
	 if(ht[k].weight<m1)   /*                 */
	 {
	   m2=m1;
	   x2=x1;
	   m1=ht[k].weight;
	   x1=k;
	 }
       else if(ht[k].weight<m2)    /*                         */
	    {
	      m2=ht[k].weight;
	      x2=k;
	    }
     ht[x1].parent=i;
     ht[x2].parent=i;
     ht[i].weight=ht[x1].weight+ht[x2].weight;
     ht[i].left=x1;
     ht[i].right=x2;
  }
}

void output(huffnode ht[],huffcode hcd[])  /*         */
{
  huffcode d;
  int i,n,c,f,k,x;
  n=ht[0].weight;
  for(i=1;i<=n;i++)
  {
    d.start=n+1; /* d.start    */
    c=i;   /* c       */
    f=ht[i].parent;   /* f           */
    while(f!=0)
    {
      if(ht[f].left==c)  /*                */
	     d.cd[--d.start]='0';
      else
	     d.cd[--d.start]='1'; /*               */
         c=f;   /*            c */
         f=ht[f].parent;  /* c      f */
    }
    hcd[i]=d;  /*              hcd[i]   */
  }
  for(i=1;i<=n;i++)  /*              */
  {
    printf("%c: ",ht[i].data);
    x=hcd[i].start;
    for(k=x;k<=n;k++)  /*            */
      printf("%c",hcd[i].cd[k]);
    printf("
"
); } } void coding1(huffcode hcd[],huffnode ht[]) /* , , */ { int i,j,n,m,k,x; char in[maxsize],out[2*maxsize]; /* in ,out */ m=0; /* m out */ printf(" :
"
); getchar(); gets(in); n=strlen(in); for(i=0;i<n;i++) { for(j=1;j<=ht[0].weight;j++) /* ht[0].weight */ if(in[i]==ht[j].data) /* */ { x=hcd[j].start; for(k=x;k<=ht[0].weight;k++){ out[m]=hcd[j].cd[k]; //printf("%c",out[m]); m++; } } } printf(" :
"
); for(i=0;i<m;i++) printf("%c",out[i]); printf("
"
); getchar(); } void coding2(huffcode hcd[],huffnode ht[]) /* , , */ { int i,j,n,m,k,x; char in[maxsize],out[2*maxsize]; /* in ,out */ m=0; /* m out */ printf(" :
"
); //getchar(); gets(in); n=strlen(in); for(i=0;i<n;i++) { for(j=1;j<=ht[0].weight;j++) /* ht[0].weight */ if(in[i]==ht[j].data) /* */ { x=hcd[j].start; for(k=x;k<=ht[0].weight;k++){ out[m]=hcd[j].cd[k]; //printf("%c",out[m]); m++; } } } printf(" :
"
); for(i=0;i<m;i++) printf("%c",out[i]); printf("
"
); getchar(); } void decoding(huffcode hcd[],huffnode ht[]) /* , , */ { int i,j,n,k,x,m,w; char in[maxsize*2],out[maxsize]; /* in ,out */ printf(" :
"
); scanf("%s",in); n=strlen(in); i=0; m=0; /* i in ,m out */ while(i<n) { for(j=1;j<=ht[0].weight;j++) /* ht[0].weight */ { x=hcd[j].start; for(k=x,w=i;k<=ht[0].weight;k++,w++) /* k hcd[j].cd[] */ if(in[w]!=hcd[j].cd[k]) break; if(k>ht[0].weight) { out[m++]=ht[j].data; break; } } i=w; } printf(" :
"
); for(i=0;i<m;i++) /* */ printf("%c",out[i]); printf("
"
); getchar(); } void disp(huffnode ht[]) /* */ { int top,i,j,stack[maxsize]; top=0; printf("the tree is:"); for(i=1;i<=ht[0].weight*2-1;i++) /* 0 */ if(ht[i].parent==0) break; do { if(i!=0) /* i */ { printf("%d ",ht[i].weight); stack[top++]=i; /* */ while(ht[i].left!=0) { i=ht[i].left; /* i */ printf("%d ",ht[i].weight); stack[top++]=i; } } j=stack[--top]; i=ht[j].right; }while(top>=0||i!=0); getchar(); getchar(); } void Automatic(huffnode ht2[],huffcode hcd2[]){ system("cls"); char num[max]; int count[300]; memset(count,0,sizeof(count)); printf("*************************
"
); printf("
"
); printf(" :
"
); scanf("%s",num); int n=strlen(num); int cnt=0; // for(int i=0;i<n;i++) count[(int)num[i]]++; for(int j=0;j<256;j++){ if(count[j]!=0){ cnt++; ht2[cnt].weight=count[j]; ht2[cnt].data=(char)j; } } ht2[0].weight=cnt; hfmtree(ht2); printf(" :
"
); output(ht2,hcd2); do{ printf("**********************
"
); printf(" :
"
); for(int i=1;i<=ht2[0].weight;i++) printf("%c ",ht2[i].data); printf("
"
); printf("1.
"
) ; printf("2.
"
); printf("0.
"
); printf(" (0~2):
"
); int n1; scanf("%d",&n1); if(n1==1) coding1(hcd2,ht2); else if(n1==2) decoding(hcd2,ht2); else break; }while(1); } void welcome(){ // system("cls"); printf("***************************
"
); printf("
"
); printf("
"
); printf("1.
"
); printf("2.
"
); printf("3.
"
); printf("0.
"
); printf("
"
); printf("***************************
"
); printf("
"
); } int precompiled() { // int select; do { system("cls"); init(); // hfmtree(ht); // output(ht,hcd); // printf("*************************
"
); printf("
"
); printf("
"
); printf("1.
"
); printf("2.
"
); printf("3.
"
); printf("0.
"
); printf("
"
); printf("*************************
"
); printf(" (0~3):
"
); scanf("%d",&select); switch(select) { case 1: coding1(hcd,ht); break; case 2: decoding(hcd,ht); getchar(); break; case 3: disp(ht); break; case 0: return 0; default: printf(" (0~3)!"); } }while(1); getchar(); return 0; } int Manual(){ int select; system("cls"); input(); hfmtree(ht); // output(ht,hcd); // do { printf("******************************
"
); printf("
"
); printf("
"
); printf("1.
"
); printf("2.
"
); printf("3.
"
); printf("0.
"
); printf("
"
); printf("*************************
"
); printf(" (0~3):
"
); scanf("%d",&select); getchar(); switch(select) { case 1: coding2(hcd,ht); break; case 2: decoding(hcd,ht); getchar(); break; case 3: disp(ht); break; case 0: return 0; default: printf(" (0~3)!
"
); } }while(1); getchar(); return 0; } void input() /* ,( huffnode *init() ) */ { int i,n; printf(" :
"
); scanf("%d",&n); /* */ ht[0].weight=n; for(i=1;i<=n;i++) { getchar(); printf(" %d : ",i); scanf("%c%d",&ht[i].data,&ht[i].weight); /* */ } } int main() { int select; while(1){ welcome(); printf(" (0~3):
"
); scanf("%d",&select); getchar(); switch(select) { case 1 : precompiled(); // break; case 2 : Manual(); // break; case 3 : Automatic(ht2,hcd2); // break; case 0 : printf(" !!!"); exit(1); default : printf(" (1~3)!"); } getchar(); } return 0; }

좋은 웹페이지 즐겨찾기