⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 huffman.c

📁 哈夫曼编码^译码器 数据结构经典实验 我们大多数情况下作为参考
💻 C
字号:
#define  TRUE       1
#define  ERROR   0
#define  OVEREFLOW 0
#define  LIST_INIT_SIZE 100
#define  LISTINCREMENT10
/*status是函数的类型,其值是函数结果状态代码,*/
typedef  int  Status;
#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));
   
   while(1){
start:
    printf("\n");
       printf("*************************************************\n");
       printf("*     1. 创建记录表                                     *\n");
       printf("*     2. 输入待编码的文件                               *\n");
       printf("*     3. 输入待译码的文件                               *\n");
       printf("*     0. 退出                                           *\n");
       printf("*************************************************");
	   printf("\n    please input your choose (1 2 3 0)i=?");
       scanf("%d",&i);
    switch (i)
   {
      case 1: 
      case 2:
          settree();
          break;
      case 3:
          disp();
          break;
	  case 0:
          exit(0);
     
   }
    }
}
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);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -