永发信息网

哈夫曼问题课程设计

答案:3  悬赏:30  手机版
解决时间 2021-01-27 22:20
哈夫曼问题课程设计
最佳答案
#define MAXSIZE 100
#define MAXWEIGHT 32767
#define n 6

typedef char elemtype;
typedef struct
{
char data;
int weight;
int parent;
int lchild;
int rchild;
}huffnode;
typedef struct
{
char cd[MAXSIZE];
int start;
}huffcode;

void creathuffmamtree(huffnode ht[])
{

int i,k,l,r,min1,min2;

for (i=0;i<2*n-1;i++)
ht[i].parent=ht[i].lchild=ht[i].rchild=0;
for (i=n;i<2*n-1;i++)
{

min1=min2=MAXWEIGHT;
l=r=0;
for (k=0;k<=i-1;k++)

if (ht[k].parent==0)
if (ht[k].weight {
min2=min1;
r=l;
min1=ht[k].weight;
l=k;
}
else if (ht[k].weight {
min2=ht[k].weight;
r=k;
}
printf("\n");

ht[l].parent=i;
printf(" lp%d",ht[l].parent);
ht[r].parent=i;
printf(" rp%d",ht[r].parent);
ht[i].weight=ht[l].weight+ht[r].weight;

printf(" lw%d",ht[l].weight);
printf(" rw%d",ht[r].weight);
printf(" iw%d",ht[i].weight=ht[l].weight+ht[r].weight);
ht[i].lchild=l;
printf(" il%d",l);
ht[i].rchild=r;
printf(" ir%d\n",r);

}

}
void creatHuffmancode (huffnode ht[],huffcode hcd[])
{

int i,f,c;
huffcode d;
for (i=0;i {
d.start=n+1;
c=i;
ht[0].parent=6;
f=ht[i].parent;

while (f!=0)
{
if (ht[f].lchild==c)
{

d.cd[--d.start]='1';
}
else
{

d.cd[--d.start]='0';
}
c=f;

f=ht[f].parent;

}
hcd[i]=d;

}
}

void printhuffmancode(huffnode ht[],huffcode hcd[])
{

int i,k;
printf ("shu chu ha fu man bian ma:\n");
for (i=0;i {

printf(" %c:",ht[i].data);
for (k=hcd[i].start;k<=n;k++)
printf("%4c:",hcd[i].cd[k]);
printf("\n");
}
}

main()
{
huffnode ht[50];
huffcode hcd[50];
int j;
int w;
int g;
w=7;
while(w!=4)
{
printf("\n 哈夫曼\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("| |");
printf("\n");
printf("| 1.构造一棵哈夫曼树. |\n");
printf("| 2.根据哈夫曼树求哈夫曼编码的算法. |\n");
printf("| 3.输出哈夫曼编码. |\n");
printf("| 4.退出. |\n");
printf("| |");
printf("\n");
printf("-------------------------------------------------------------\n");
printf("请输入您的选择(1,2,3,4):");
scanf("%d",&w);
switch(w)
{
case 1:
{
for(j=0;j {
printf("请输入");
scanf("%d",&ht[j].weight);
}
creathuffmamtree (ht);break;
}
case 2: creatHuffmancode(ht,hcd);break;
case 3:
{
printhuffmancode(ht,hcd);break;}

default:break;
}
}
}
全部回答
#include
#include
#include
#includea
#include
#define MAXVALUE 200
#define MAXBIT 30
#define MAXNODE 30
struct haffnode
{char data;
int weight;
int flag;
int parent;
int leftchild;
int rightchild;
};
struct haffcode
{int bit[MAXNODE];
int start;
char data;
int weight;
};


void pprintf(struct haffcode haffcode[],int n);

void haffmantree(int weight[],int n,struct haffnode hafftree[],char data[]);

void haffmancode(struct haffnode hafftree[],int n,struct haffcode haffcode[]);

void test(struct haffcode haffcode[],int n);

void end();


void haffmantree(int weight[],int n,struct haffnode hafftree[],char data[])

{int i,j,m1,m2,x1,x2;

for(i=0;i<2*n-1;i++)
{if(i hafftree[i].weight=weight[i];
}
else {hafftree[i].weight=0;
hafftree[i].data='\0';
}
hafftree[i].parent=0;
hafftree[i].flag=0;
hafftree[i].leftchild=-1;
hafftree[i].rightchild=-1;
}
for(i=0;i {m1=m2=MAXVALUE;
x1=x2=0;
for(j=0;j {if(hafftree[j].weight {m2=m1;
x2=x1;
m1=hafftree[j].weight;
x1=j;
}
else if(hafftree[j].weight {m2=hafftree[j].weight;
x2=j;
}
}
hafftree[x1].parent=n+i;
hafftree[x2].parent=n+i;
hafftree[x1].flag=1;
hafftree[x2].flag=1;
hafftree[n+i].weight=hafftree[x1].weight+hafftree[x2].weight;
hafftree[n+i].leftchild=x1;
hafftree[n+i].rightchild=x2;
}
}
void haffmancode(struct haffnode hafftree[],int n,struct haffcode haffcode[])
{
int i,j,child,parent;
struct haffcode newcode;
struct haffcode *cd;
cd=&newcode;
for(i=0;i {cd->start=MAXBIT-1;
cd->weight=hafftree[i].weight;
cd->data=hafftree[i].data;
child=i;
parent=hafftree[child].parent;
while(parent!=0)
{if(hafftree[parent].leftchild==child)
cd->bit[cd->start]=0;
else
cd->bit[cd->start]=1;
cd->start--;
child=parent;
parent=hafftree[child].parent;
}
for(j=cd->start+1;j haffcode[i].bit[j]=cd->bit[j];
haffcode[i].data=cd->data;
haffcode[i].start=cd->start;
haffcode[i].weight=cd->weight;
}
}
void pprintf(struct haffcode myhaffcode[],int n)
{int i,j,count=0;
clrscr();
for(i=0;i {textcolor(YELLOW);
cprintf("字符=%c",myhaffcode[i].data);
printf(" ");
textcolor(YELLOW);
cprintf("weight=%3d",myhaffcode[i].weight);
printf(" ");
textcolor(YELLOW);
cprintf("haffcode=");
for(j=myhaffcode[i].start+1;j cprintf("%d",myhaffcode[i].bit[j]);
printf("\n");
count++;
if(count==21)
getch();
}
}
void test(struct haffcode haffcode[],int n)
{int i,j,k,s;
char sstring[MAXNODE];
struct haffcode newhaffcode[MAXNODE];
j=0;
clrscr();
textcolor(YELLOW);
cprintf("请输入哈夫曼编码测试数据,在此建议为'this programme is my favorite'");
printf("\n");
cprintf("注意小写,空格由大写字母T代替,并且字符数小于27.\n");
scanf("%s",sstring);
if(strlen(sstring)>=MAXNODE)
{printf("you input the data number >=MAXNODE.");
exit(1);
}
for(i=0;i {
for(j=0;j if(sstring[i]==haffcode[j].data)
{
k=j;
break;
}
if(k<0||k>MAXNODE-1)
{printf("在系统中找不到与第个%d字符相匹配的编码\n",i+1);
continue;
}
newhaffcode[i].start=haffcode[k].start;
newhaffcode[i].weight=haffcode[k].weight;
newhaffcode[i].data=haffcode[k].data;
for(s=haffcode[k].start+1;s newhaffcode[i].bit[s]=haffcode[k].bit[s];
}
pprintf(newhaffcode,strlen(sstring));
}
void end()
{int driver,mode;
driver=VGA;
mode=VGAHI;
initgraph(&driver,&mode," ");
setlinestyle(0,0,2);
setfillstyle(1,9);
bar(120,60,520,80);
setfillstyle(1,9);
bar(90,100,550,350);
moveto(121,65);
settextstyle(5,0,6);
setcolor(7);
outtext("This programme is designed by Dou Zheren");
settextstyle(3,0,3);
setcolor(7);
moveto(150,200);
outtext("thank you use this programme.");
moveto(100,300);
settextstyle(3,0,2);
setcolor(7);
outtext("please press anykey to end this programme.");
}
void main()
{
int i,j,n=27;
int driver=VGA,mode=VGAHI;
char ch;
int weight[27]={186,64,13,22,32,103,21,15,47,
57,1,5,32,20,57,63,15,1,48,
51,80,23,8,18,1,16,1};
char data[28]={'T','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'};
struct haffnode newhaffnode[2*MAXNODE-1];
struct haffcode newcode[MAXNODE];
struct haffnode *myhafftree=newhaffnode;
struct haffcode *myhaffcode=newcode;
if(n>MAXNODE)
{printf("you input the haffnode > MAXNODE,so you input the data is wrong");
printf("\n");
exit(1);
}
clrscr();
textcolor(YELLOW);
cprintf("WELCOME!这是一个求哈夫曼编码的问题");
printf("\n");
cprintf("即对所有的字母进行编码后,在根据用户的需要,对用户的要求进行编码。");
printf("\n");
cprintf("注意:本程序只支持小写字母,空格用大写字母T代替! ");
printf("\n");
getch();
textcolor(YELLOW);
cprintf("Ready?Enter,if you want to begin!\n");
printf("\n");
getch();
cprintf("Now,开始演示哈夫曼编码.");
getch();
haffmantree(weight,n,myhafftree,data);
haffmancode(myhafftree,n,myhaffcode);
pprintf(myhaffcode,n);
clrscr();
printf("若执行自定义编译,请输入y继续。否则程序将结束.");
if((ch=getch())=='y'||ch=='Y')
test(myhaffcode,n);
getch();
clrscr();
end();
getch();
exit(1);
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
谑笑的意思是什么啊?知道的请说下!
光大银行卡怎么办
2009年甲型H1N1流感的蔓延使相关药物不断走俏
马上要到高三了,一轮复习资料哪种可以?
我开了个烧烤害怕火芯从排烟口进去着火了,有
求不知火舞和三个小男孩全集
我的问题请回答我!请问:圣经共有多少卷(旧
北京宣武门最近的集市都是那里
构成物质的微粒有________、________、______
尽性的意思是什么啊?知道的请说下!
lol ez皮肤未来战士 什么时候出
中海油揭阳能源地址在什么地方,我要处理点事
常伴酵素在哪能买到多少钱
索普电动车48v加个电瓶有影响吗
神将世界的代码大全
推荐资讯
80%x+10=20分之21x
使用reg文件,直接删除某些文件
进口锯末价格
骥坂的意思是什么啊?知道的请说下!
轻宝的意思是什么啊?知道的请说下!
2015款280tsi速腾自动旗舰版变速器是什么型号
御毅窗帘墙布壁纸软装设计地址好找么,我有些
“去做什么”用英语怎么拼
指天骂地愤怒的诗句
1978年属马奉请大势至菩萨什么颜色为佳
乡下女老人的名字,一定要特别土的
用什么工具能自己捂死自己
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?