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

📄 huf.c

📁 huffman编码压缩
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <taskLib.h>
#define N 71
#define M 2*N-1

#define DEBUG
/*#undef DEBUG*/
#define	INPUT_FILE_NUMBER 5

typedef int datatype;
typedef struct
{ int weight;
  int lchild,rchild,parent;
} hufmtree;

hufmtree tree[M];

typedef struct
{ char bits[N];
  int start;
  char ch;
} codetype;

codetype code[N];

void hufman(hufmtree tree[], int weight[N])
{

  int i,j,p1,p2;
  int small1,small2,f;
  for (i=0;i<M;i++)
   { tree[i].parent=0;
     tree[i].lchild=0;
     tree[i].rchild=0;
     tree[i].weight=0;
   }

   for(i=0;i<N;i++){
        tree[i].weight=weight[i];
#ifdef DEBUG
taskDelay(5);
printf("tree[%d] is %d",i,weight[i]);
#endif
   }

  for (i=N;i<M;i++)
   { p1=0; p2=0;
     small1=99999; small2=99999;
     for (j=0;j<i;j++)
       if (tree[j].parent==0){
         if (tree[j].weight<small1)
           { small2=small1;
	     small1=tree[j].weight;
             p2=p1; p1=j;
           }
         else if (tree[j].weight<small2)
           { small2=tree[j].weight;
             p2=j;
           }
       }

#ifdef DEBUG
taskDelay(6);
printf("p1 is %d, p2 is %d, pareant is %d\n",p1,p2,i);
#endif

     tree[p1].parent=i;
     tree[p2].parent=i;
     tree[i].lchild=p1;
     tree[i].rchild=p2;
     tree[i].weight=tree[p1].weight+
                    tree[p2].weight;
   }
}

void hufmancode(codetype code[], hufmtree tree[])
{ int i,j,c,p;
  codetype cd;

  for (i=0;i<N;i++)
   { cd.start=N;
     c=i;
     p=tree[i].parent;

     while (p!=0)
      { cd.start--;
        if (tree[p].lchild==c)
		cd.bits[cd.start]= 0;
        else
          cd.bits[cd.start]= 1;
        c=p;
        p=tree[p].parent;
#ifdef DEBUG
taskDelay(5);
printf("p=tree[%d].parent is %d;\n", i, p);
#endif
      }
     code[i]=cd;
   }
}

int main()
{
	char character;
	FILE *fp;
	FILE *fp1;
	int count[N];
	int i,j,num;
	codetype *buf;
	codetype *buff;
	hufmtree tree[M];
	codetype code[N];
	char *filename[INPUT_FILE_NUMBER];
	char *code_file;

	

	filename[0] = "VPX.txt";
	filename[1] = "TLS.txt";
	filename[2] = "SBS_Video.txt";
	filename[3] = "readme.txt";
	filename[4] = "license.txt";
	code_file = "huffman_code";


	for(i=0;i<N;i++)
		count[i] = 0;
		
	for(i=0;i<INPUT_FILE_NUMBER;i++)
	{
		if((fp=fopen(filename[i],"r"))==NULL)  
                        {        
				printf("cannot open %s\n", filename[i]);               
				return 0;   
			}
		fscanf(fp,"%c",&character);
		while(!feof(fp)){
			switch(character)
			{
				case 'a':
					count[0]++;
					break;
				case 'b':
					count[1]++;
					break;
				case 'c':
					count[2]++;
					break;
				case 'd':
					count[3]++;
					break;
				case 'e':
					count[4]++;
					break;
				case 'f':
					count[5]++;
					break;
				case 'g':
					count[6]++;
					break;
				case 'h':
					count[7]++;
					break;
				case 'i':
					count[8]++;
					break;
				case 'j':
					count[9]++;
					break;
				case 'k':
					count[10]++;
					break;
				case 'l':
					count[11]++;
					break;
				case 'm':
					count[12]++;
					break;
				case 'n':
					count[13]++;
					break;
				case 'o':
					count[14]++;
					break;
				case 'p':
					count[15]++;
					break;
				case 'q':
					count[16]++;
					break;
				case 'r':
					count[17]++;
					break;
				case 's':
					count[18]++;
					break;
				case 't':
					count[19]++;
					break;
				case 'u':
					count[20]++;
					break;
				case 'v':
					count[21]++;
					break;
				case 'w':
					count[22]++;
					break;
				case 'x':
					count[23]++;
					break;
				case 'y':
					count[24]++;
					break;
				case 'z':
					count[25]++;
					break;
				case 'A':
					count[26]++;
					break;
				case 'B':
					count[27]++;
					break;
				case 'C':
					count[28]++;
					break;
				case 'D':
					count[29]++;
					break;
				case 'E':
					count[30]++;
					break;
				case 'F':
					count[31]++;
					break;
				case 'G':
					count[32]++;
					break;
				case 'H':
					count[33]++;
					break;
				case 'I':
					count[34]++;
					break;
				case 'J':
					count[35]++;
					break;
				case 'K':
					count[36]++;
					break;
				case 'L':
					count[37]++;
					break;
				case 'M':
					count[38]++;
					break;
				case 'N':
					count[39]++;
					break;
				case 'O':
					count[40]++;
					break;
				case 'P':
					count[41]++;
					break;
				case 'Q':
					count[42]++;
					break;
				case 'R':
					count[43]++;
					break;
				case 'S':
					count[44]++;
					break;
				case 'T':
					count[45]++;
					break;
				case 'U':
					count[46]++;
					break;
				case 'V':
					count[47]++;
					break;
				case 'W':
					count[48]++;
					break;
				case 'X':
					count[49]++;
					break;
				case 'Y':
					count[50]++;
					break;
				case 'Z':
					count[51]++;
					break;
				case ',':
					count[52]++;
					break;
				case '.':
					count[53]++;
					break;
				case ')':
					count[54]++;
					break;
				case '(':
					count[55]++;
					break;
				case '-':
					count[56]++;
					break;
				case '/':
					count[57]++;
					break;
				case ':':
					count[58]++;
					break;
				case ' ':
				     	count[59]++;
				     	break;
				case '0':
				     	count[60]++;
				     	break;
				case '1':
				     	count[61]++;
				     	break;
				case '2':
				     	count[62]++;
				     	break;
				case '3':
				     	count[63]++;
				     	break;
				case '4':
				     	count[64]++;
				     	break;
				case '5':
				     	count[65]++;
				     	break;
				case '6':
				     	count[66]++;
				     	break;
				case '7':
				     	count[67]++;
				     	break;
				case '8':
				     	count[68]++;
				     	break;
				case '9':
				     	count[69]++;
				     	break;

				default:
					count[70]++;
					break;
			}		
			if(fscanf(fp, "%c", &character)!=1)
				if(fscanf(fp, "%d", &num)!=1)
					return -1;
		}
		fclose(fp);
	}

	hufman(tree, count);
	hufmancode(code,tree);;

	/*initial asicii table char 0-9*/
	for(i=60;i<70;i++){
		code[i].ch = (char) i-12;
		printf("code[%d].ch is %c\n", i, code[i].ch);
		taskDelay(5);
	}

	/*initial char a-z*/
	for(i=0;i<26;i++){
		code[i].ch = (char) i+97;
		printf("code[%d].ch is %c\n", i, code[i].ch);
		taskDelay(5);
	}


	/*initial char A-Z*/
	for(i=26;i<52;i++){
		code[i].ch = (char)i+39;
	       printf("code[%d].ch is %c\n", i, code[i].ch);
		taskDelay(5);
	}
	/*initial char */
	code[52].ch = 39;
	code[53].ch = 46;
	code[54].ch = 41;
	code[55].ch = 40;
	code[56].ch = 45;
	code[57].ch = 47;
	code[58].ch = 48;
	code[59].ch = 32;
	printf("code[58].ch is%c\n", code[58].ch);
	
#ifdef DEBUG
	for(i=0;i<N;i++)
	{
		taskDelay(5);
		printf("code[%d].start is %d\n",i,code[i].start);
		taskDelay(5);
		printf("code[%d].ch is %c\n tree[%d].weight is %d",i, code[i].ch,i,tree[i].weight);
		taskDelay(5);
		printf("bits[] is   ");
		for(j=code[i].start;j<N;j++){
			printf("%d",code[i].bits[j]);
			taskDelay(5);
		}
		printf("\n\n");
	}
#endif	
	if((fp1 = fopen(code_file,"w")) == NULL)
	{
		printf("%s error\n", code_file);
		return 0;
	}

	buf = (codetype *)malloc(sizeof(codetype) * N);
	buff = buf;
	for(i= 0;i<N;i++){
		memcpy(buff, &code[i], sizeof(codetype));
		buff++;
	}
	fwrite(buf, sizeof(codetype), N, fp1);
	
	fclose(fp1);
	free(buf);
}





	    
		    
		
		
	    
	    

⌨️ 快捷键说明

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