user-0 提问: 求助贴，输入大写英文字母，求哈弗曼编码，编码后的正文，解码后的正文，调试了一天了，整不出来

回复:
user: user-1
content: 希望对你有帮助：https://blog.csdn.net/it_xiangqiang/category_10581430.html 希望对你有帮助：https://blog.csdn.net/it_xiangqiang/category_10768339.html

user: user-2
content: 拥有史上最强 IDE，不会调试吗？  可视化工具都不会用，神也救不了你。  看看变量的值的异常，数组下标越界啊，等等等  还有错误描述一定要清清楚楚，怎么触发的等等一系列信息，不管有用没有，用最精炼的句子描述出来

user: user-0
content: #include<string.h>  #include<stdlib.h>  #include<stdio.h>  int m,s1,s2; typedef struct  {  	int weight;  	int parent,lchild,rchild;  }HuffmanTree; void Select(HuffmanTree *HT,int n) {      int i,flag=0;      for(i=1;i<=n;i++)      {         if((HT[i].parent==0) && (flag==0))         {             s1=i;             flag=1;             continue;         }         if(HT[s1].weight>HT[i].weight && (!HT[i].parent))             s1=i;     }     flag=0;     for(i=1;i<=n;i++)     {         if((HT[i].parent==0) && (flag==0) && (i!=s1))         {             s2=i;             flag=1;             continue;         }         if(HT[s2].weight>HT[i].weight && (HT[i].parent==0) && (i!=s1))             s2=i;     } }  char **HuffmanCoding(HuffmanTree *HT,char **HC, int w[], int n)  {     int i;      char *cd;      int f,start,c;      if(n<=1) return NULL;            m = 2*n-1;                   HT = (HuffmanTree *)malloc((m+1) * sizeof(HuffmanTree));     for (i=1; i<=n; i++)               {          HT[i].weight=w[i-1];          HT[i].parent=0;          HT[i].lchild=0;          HT[i].rchild=0;      }       for (i=n+1; i<=m; i++)             {          HT[i].weight=0;          HT[i].parent=0;          HT[i].lchild=0;          HT[i].rchild=0;      }      for (i=n+1; i<=m; i++)      {          Select(HT, i-1);          HT[s1].parent = i; HT[s2].parent = i;          HT[i].lchild = s1; HT[i].rchild = s2;          HT[i].weight = HT[s1].weight + HT[s2].weight;      }      HC=(char**)malloc((n+1)*sizeof(char *));       cd=(char*)malloc((n+1)*sizeof(char));          cd[n-1]='\0';                                    for(i=1;i<=n;i++)                               {         start=n-1;               c=i;         f=HT[i].parent;         while(f!=0)         {             if( HT[f].lchild == c)                  cd[--start]='0';              else                  cd[--start]='1';               c=f;             f=HT[f].parent;              HC[i]=(char *)malloc((n-start)*sizeof(char));              strcpy(HC[i],&cd[start]);         }     }     free(cd);     return HC;       }  int main(void)  {      HuffmanTree *HT=NULL;     char **HC=NULL;     int i,j,m,n,k,d; 	int flag=0;    int count[26]; 	char a[100]={'0'}; 	char b[100]={'0'}; 	char c[100]={'0'}; 	int root,p; 	printf("请输入正文:"); 	scanf("%s",a); 	j=strlen(a); 	for(i=0;i<j;i++) 	{ 		if(a[i]>='A'&&a[i]<='Z') 			count[a[i]-'A']++; 		else  		{ 			printf("正文输入有误。"); 			break; 		} 	} 	b[0]=a[0]; 	n=1; 	count[0]=1; 	for(i=1;i<j;i++) 	{ 		flag=0; 		for(m=0;m<n;m++) 			if(a[i]==b[m]) 			{ 				flag=1; 				count[m]++; 			} 		if(flag==0) 		{ 			n++; 			count[n-1]=1; 			b[n-1]=a[i]; 		} 	} 	p=root=2*n-1;     HC=HuffmanCoding(HT,HC,count,n);      printf("哈夫曼编码为:\n");      for(i = 1;i <= n;i++)     printf("%5c%4d:  %s\n",b[i-1],count[i-1],HC[i]);  	printf("正文编码为："); 	for(i=0;i<j;i++) 	{ 		for(m=0;m<n;m++) 			if(a[i]==b[m]) 			{ 				printf("%s",HC[m+1]); 				strcat(c,HC[m+1]); 			} 	} 	printf("\n解码后的正文为:"); 	d=strlen(c); 	for(i=1;i<d;i++) 	{ 		if(c[i]=='0') 			p=HT[p].lchild; 		else if(c[i]=='1') 			p=HT[p].rchild; 		if((HT[p].lchild==0)||(HT[p].rchild==0)) 		{ 			for(k=0;k<n;k++) 				if(HT[p].weight==count[k]) 					printf("%s",b[k]); 			p=root; 		} 	}      return 0; }

user: user-0
content: 引用 1 楼 user-3的回复:乱的像二进制文件一样。CSDN论坛新手指南 下面是刚刚在电脑上发的，求大神帮忙看看

user: user-3
content: 乱的像二进制文件一样。CSDN论坛新手指南