谁会用C语言编程???

#include<stdio.h>#include<math.h>#defineMAX100#defineMAXSYMBS30#defineMAXNODE59#defin... #include <stdio.h>
#include <math.h>
#define MAX 100
#define MAXSYMBS 30
#define MAXNODE 59
#define DEEP 10
typedef struct{
float weight;
int flag;
int parent;
int lchilde;
int rchilde;
}huffnode;
/*哈夫曼树结构定义*/
typedef struct{
int bits[MAXSYMBS];
int start;
}huffcode;
int main(void)
{
huffnode huff_node[MAXNODE];
huffcode huff_code[MAXSYMBS],cd;
int i,j,x1,x2,n,c,p;
float m1,m2,temp,hx=0,KL=0;
clrscr();
printf("Please input the leaf num of tree:\n");
scanf("%d",&n);
for(i=0;i<=2*n-1;i++)
{
huff_node[i].weight=0;
huff_node[i].parent=0;
huff_node[i].flag=0;
huff_node[i].lchilde=-1;
huff_node[i].rchilde=-1;
}
printf("Please input the weight of every leaf\n");
for(i=0;i<n;i++)
{ printf("input %dth weight>>",i+1);
scanf("%f",&temp);
huff_node[i].weight=temp;
hx=hx-temp*3.332*log10(temp);
}
/*构建哈夫曼树*/
for(i=0;i<n-1;i++)
{
m1=m2=MAX;
x1=x2=0;
for(j=0;j<n+i;j++)
{
if(huff_node[j].weight<m1&&huff_node[j].flag==0)
{
m2=m1;
x2=x1;
m1=huff_node[j].weight;
x1=j;
}
else
if(huff_node[j].weight<m2&&huff_node[j].flag==0)
{
m2=huff_node[j].weight;
x2=j;
}
}
huff_node[x1].parent=n+i;
huff_node[x2].parent=n+i; /*将找出的两棵子树合并为一棵子树*/
huff_node[x1].flag=1;
huff_node[x2].flag=1;
huff_node[n+i].weight=huff_node[x1].weight+huff_node[x2].weight;
huff_node[n+i].lchilde=x1;
huff_node[n+i].rchilde=x2;
}
/*求字符的哈夫曼编码*/
for(i=0;i<n;i++)
{
cd.start=n;
c=i;
p=huff_node[c].parent;
while(p!=0)
{
if(huff_node[p].lchilde==c) cd.bits[cd.start]=0;
else cd.bits[cd.start]=1;
cd.start=cd.start-1;
c=p;
p=huff_node[p].parent;
}
cd.start++;
for(j=cd.start;j<=n;j++)
{
huff_code[i].bits[j]=cd.bits[j];
huff_code[i].start=cd.start;
}
}
/*输出字符的编码*/
printf("\nweight\thuffmancode\n");
for(i=0;i<n;i++)
{
printf("%2.3f:\t",huff_node[i].weight);
for(j=huff_code[i].start;j<=n;j++)
printf("%d",huff_code[i].bits[j]);
KL=KL+(n-huff_code[i].start+1)*huff_node[i].weight;
printf("\n");
}
printf("\nH(X)=%f\tKL=%f\nR=%f",hx,KL,hx/KL);
return 0;
}

就是要把程序修改一下,不能跟他里头的完全一样,要实现的是两个编码的问题
展开
 我来答
fengzhiyeq
2007-07-04 · TA获得超过824个赞
知道小有建树答主
回答量:535
采纳率:0%
帮助的人:326万
展开全部
#include"stdio.h"
#include"malloc.h"
#include"stdlib.h"
#include"string.h"
#define MAXSIZE 10000
typedef struct {
char data;
unsigned int weight;
unsigned int parent, lchild, rchild;
}HTNode, *HufTree;
typedef struct {
char key;
char *code;
} HufCode,*HufCodep ;
void select(HufTree t,int i,int &s1,int &s2){
int j;
unsigned int m1;
for(j=1;j<=i;j++)
{
if(t[j].parent==0){
m1=t[j].weight;s1=j;
for(j++;j<=i;j++)
if(t[j].parent==0&&t[j].weight<m1)
{
m1=t[j].weight;
s1=j;
}
}
}
for(j=1;j<=i;j++)
{
if(j!=s1&&t[j].parent==0){
m1=t[j].weight;s2=j;
for(j++;j<=i;j++)
if(t[j].parent==0&&t[j].weight<m1&&j!=s1)
{
m1=t[j].weight;
s2=j;
}
}
}
}
void HuffmanCoding(HufTree &HT,HufCodep &HC,int *w,int n,char *l) {
if(n<=1) return ;
int m=2*n-1,i;
HufTree p;
HT=(HufTree)malloc((m+1)*sizeof(HTNode));
for(p=HT+1,i=1;i<=n;++i,++p,++w,++l)
{ p->data=*l;
p->weight=*w;
p->parent=0;
p->lchild=0;
p->rchild=0;
}
for( ;i<=m;++i,++p) {
p->weight=0;
p->parent=0;
p->lchild=0;
p->rchild=0;
}
int s1,s2;
for(i=n+1;i<=m;++i) {
select(HT,i-1,s1,s2) ;
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;
}
printf("\n ======================the huffmatree======================\n");
for(i=1;i<=m;++i)if(i<=29)
printf("\n data: %2c weight: %-4d parent: %-3d lchild: %-3d rchild: %-3d", HT[i].data,HT[i].weight,HT[i].parent,HT[i].lchild,HT[i].rchild);
else
printf("\n data: no weight: %-4d parent: %-3d lchild: %-3d rchild: %-3d", HT[i].weight,HT[i].parent,HT[i].lchild,HT[i].rchild);
HC=(HufCode *)malloc((n+1)*sizeof(HufCode)) ;
unsigned int f,c;
char *cd;
cd=(char *)malloc(n*sizeof(char));
cd[n-1]='\0';
printf("\n\n ==============the codes of huffmantree=================\n");
for(i=1;i<=n;++i) {
int start=n-1;
for(HC[i].key=HT[i].data,c=i,f=HT[i].parent;f!=0;c=f,f=HT[f].parent)
if(HT[f].lchild==c) cd[--start]='0';
else cd[--start]='1';
HC[i].code=(char *)malloc((n-start)*sizeof(char));
strcpy(HC[i].code,cd+start);
printf("\n the huffmancode of %c is: %s",HC[i].key,HC[i].code);
}
free(cd);
}
void creatcode(char *s,HufCodep hc,int n){
int i1,i2,l=strlen(s);
for(i1=0;i1<=l;i1++)
for(i2=1;i2<=n;i2++)
if(s[i1]==hc[i2].key)
printf("%s",hc[i2].code);

}
void tracode(HufTree ht,int n,char*s){
int i=0,l,l1=strlen(s);
char ch=s[i];
do{
l=2*n-1;
while(ht[l].lchild!=ht[l].rchild&&ht[l].rchild!=0){
if(ch=='0')
l=ht[l].lchild;
else if(ch=='1')
l=ht[l].rchild;
ch=s[++i];if(i>l1-1)break;
}
printf("%c",ht[l].data);
}while(i<=l1-1);

}
void main()
{
HufTree HT;HufCodep HC;char text[MAXSIZE];
char war[30]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',' ',',','.'};
int w[29]={10,14,6,13,17,29,11,34,16,13,55,69,9,102,6,45,45,103,7,51,59,24,36,6,103,27,28,30,32},i=29;
HuffmanCoding(HT,HC,w,i,war );
printf("\n\n -----------data test----------\n");
printf("\n input the string:");
gets(text);
printf("\n the codes of the string are: ");
creatcode(text,HC,i);
printf("\n\n please input the codes: ");
gets(text);
printf("\n the string of the codes is: ");
tracode(HT,i,text);printf("\n");
}
我很久以前写的,你看看
军莲单于米琪
2019-08-29 · TA获得超过3928个赞
知道大有可为答主
回答量:3057
采纳率:26%
帮助的人:406万
展开全部
使用两层循环来输出即可,以下是在手机上使用易历知食软件里面的微C程序设计功能编写的一个示例,供参考。
手机上的C语言代码如下图所示:
手机上运行程序的效果如下图所示:
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
小笨狼队长
2007-07-05 · TA获得超过256个赞
知道小有建树答主
回答量:437
采纳率:0%
帮助的人:222万
展开全部
这么长/。。。。。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式