C实现哈夫曼编/译码器

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
    int n;
   struct node{
       int w;
      int flag;
      char c;
      struct node *plink,*llink,*rlink;
      char code[50];

      }*num[100],*root;
   FILE *fp;
   char tmpcode[50];
   int t=0;

int main(void)
{
    int i;
   void settree();
   void code();  
   void decode();
   void disp()  ;
     root=(struct node*)malloc(sizeof(struct node));
   puts("*******************hfm edit/encode demonstration******************************");

   while(1){
start:

   puts("1. Initrialization        2.display the list of decoding   3. end");
   while(scanf("%d",&i)!=1)
   {
      while(getchar()!='/n')
      continue;
       puts("input errors!");
      puts("reinpuf");
      puts("1. Initrialization          2.display the list of decoding  3. end");
    }
   switch (i)
   {
       case 1:
          settree();
       
         
          break;
      case 2:
           disp();
          break;
      case 3:
           exit(0);
      default:
          puts("input errors!");
         puts("reinpuf!");
         goto start;
   }
    }
}
void settree()
{
     int i,j,k;
     struct node *p1,*p2,*tmp,*p;
     void go(struct node *);
     void setcode(struct node *);
     void printtree(struct node *);
     puts("input the length of charactor");
     scanf("%d",&n);
     while(getchar()!='/n')
     continue;

     for(i=0;i<n;i++)
     {
         p=(struct node *)malloc(sizeof(struct node));
         puts("input a charactor");
         scanf("%c",&p->c);
         while(getchar()!='/n')
         continue;
         puts("input the weight of the  charactor:");
         scanf("%d",&p->w);
         while(getchar()!='/n')
         continue;

         p->plink=NULL;
             p->rlink=NULL;
             p->llink=NULL;
         num[i]=p;
     }

     for(i=0;i<n-1;i++)
     {
         for(j=i+1;j<n;j++)
      {
         if(num[i]->w>num[j]->w)
         {
             tmp=num[i];
            num[i]=num[j];
            num[j]=tmp;
         }
      }
     }


     num[n]=NULL;
     k=n;
     while(num[1]!=NULL)
     {
         p=(struct node *)malloc(sizeof(struct node));
         p1=num[0];
         p2=num[1];
         p->llink=p1;
         p->rlink=p2;
         p->plink=NULL;
         p1->plink=p;
         p2->plink=p;
         p->w=p1->w+p2->w;

         for(i=1;i<k;i++)
         {
            num[i]=num[i+1];
         }

         k--;
         num[0]=p;
         for(i=0;i<k-1;i++)
             {
                 for(j=i+1;j<k;j++)
              {
                 if(num[i]->w>num[j]->w)
                 {
                     tmp=num[i];
                    num[i]=num[j];
                    num[j]=tmp;
                 }
              }
             }
     }

     root=num[0];

     if((fp=fopen("c://hfmtree.wxl","wb"))==NULL)
     {
      puts("Openning file is false");
         getchar();
         exit(0);
     }
     setcode(root);
     go(root);
     fclose(fp);
}
void setcode(struct node *p)
{
     if(p->llink==NULL&&p->rlink==NULL)
     {
         tmpcode[t]='/0';
         strcpy(p->code,tmpcode);
     }
     else
     {
         tmpcode[t++]='0';
             setcode(p->llink);
         t--;
         tmpcode[t++]='1';
         setcode(p->rlink);
         t--;
     }
}

 

void go(struct node *p)
{

     if(p->llink==NULL&&p->rlink==NULL)
     {
         fputc('(',fp);
         fputc(p->c,fp);
         fputs(p->code,fp);
         fputc(')',fp);
     }
     else
     {

             go(p->llink);
         go(p->rlink);
     }
}

void code()
{
    FILE *fp1,*fp2,*fp3;
   char ch1,ch2,c;
   if((fp1=fopen("c://hfmtree.wxl","rb"))==NULL)
     {
      puts("Openning file is false");
         getchar();
         exit(0);
     }
    if((fp2=fopen("c://tobetrans.txt","rb"))==NULL)
     {
      puts("Openning file is false");
         getchar();
         exit(0);
     }
     if((fp3=fopen("c://codefile.wxl","wb"))==NULL)
     {
      puts("Openning file is false");
         getchar();
         exit(0);
     }

     while((ch1=fgetc(fp2))!=EOF)
     {
         t=0;


         while((ch2=fgetc(fp1))!=EOF)
         {
            if(ch1==ch2)
            {
                while((c=fgetc(fp1))!=')')
                {
                    tmpcode[t++]=c;
                }
                tmpcode[t]='/0';
                fputs(tmpcode,fp3);
                fputc(
'@',fp3);
                rewind(fp1);
                break;
            }
         }
     }
     fclose(fp1);
     fclose(fp2);
     fclose(fp3);
}

void decode()
{
    FILE *fp1,*fp2,*fp3;
   char ch1,ch2,ch3;
   char temp_3[20];
   char temp_1[20];
   int t1,t3;
   if((fp1=fopen("c://hfmtree.wxl","rb"))==NULL)
   {
      puts("Openning file is false");
         getchar();
         exit(0);
   }
    if((fp2=fopen("c://textfile.txt","wb"))==NULL)
    {
      puts("Openning file is false");
         getchar();
         exit(0);
    }
    if((fp3=fopen("c://codefile.wxl","rb"))==NULL)
     {
      puts("Openning file is false");
         getchar();
         exit(0);
     }

     while((ch3=fgetc(fp3))!=EOF)
     {
         t3=0;
          while(
ch3!='@')
          {
             temp_3[t3++]=ch3;
             ch3=fgetc(fp3);
          }
          temp_3[t3]='/0';
          while((ch1=fgetc(fp1))!=EOF)
          {
                  if(isalpha(ch1))
               {
                   ch2=ch1;
                   t1=0;
                   while((ch1=fgetc(fp1))!=')')
                   {
                         temp_1[t1++]=ch1;
                   }
                   temp_1[t1]='/0';

                   if(strcmp(temp_1,temp_3)==0)
                      {
                          fputc(ch2,fp2);
                     rewind(fp1);
                     break;
                      }
               }
          }
     }
     fclose(fp1);
     fclose(fp2);
     fclose(fp3);
}


void disp(void)
{
    FILE *fp1,*fp2;
   char ch1,ch2;
   char tmp[20];
   int t;
   if((fp1=fopen("c://hfmtree.wxl","rb"))==NULL)
   {
  puts("Openning file is false");
         getchar();
         exit(0);
   }
   if((fp2=fopen("c://hfmcode.txt","wb"))==NULL)
   {
      puts("Openning file is false");
         getchar();
         exit(0);
   }
   while((ch1=fgetc(fp1))!=EOF)
   {
       if(ch1=='(')
      {
         t=0;
         ch1=fgetc(fp1);
         ch2=ch1;
         while((ch1=fgetc(fp1))!=')')
         {
             tmp[t++]=ch1;
         }
         tmp[t]='/0';
         printf("%c-----%s/n",ch2,tmp);
         fputc(ch2,fp2);
         fputc('-',fp2);
         fputc('-',fp2);
         fputc('-',fp2);
         fputs(tmp,fp2);
         fputc('/n',fp2);
      }
   }
   fclose(fp1);
   fclose(fp2);
}

posted on 2012-06-29 13:28  carekee  阅读(496)  评论(0编辑  收藏  举报