?? hf.cpp
字號:
#include<conio.h>
#include<stdio.h>
#include<malloc.h>
#define n 100
int h[50],m[50];
int i=0,j=0,v=1,t=1; /*用來標(biāo)記huffman樹的深度*/
int k=195,K=192,chp=179,chr=27; /*輸出huffman時用到的字符*/
int huffrecod=0; /*用于記錄huffman的編碼與字符的個數(shù)*/
typedef struct Lnode{ /*用來記錄字符的個數(shù)*/
char code;
int values;
struct Lnode *next;
}Lnode,*listnode;
typedef struct charhuffman{ /*用來記錄字符的huffman編碼序列*/
char code;
int huffuman[15];
int count; /*記錄編碼的個數(shù)*/
}charhuffman,*huffmanchar;
typedef struct treenode{ /*二叉樹的構(gòu)造體*/
char code;
int values;
struct treenode *lchild;
struct treenode *rchild;
}treenode,*ntree;
typedef struct queuenode/*隊列的構(gòu)造體*/
{
int values;
struct treenode *pntree;
struct queuenode *next;
}queuenode,*listqueue;
huffmanchar huff;
listqueue returnroot(listnode heap)
{
listqueue s,t,k;
ntree p;
s=(listqueue)malloc(sizeof(queuenode)); /*建立隊列的頭結(jié)點*/
k=s;
s->values=0; s->pntree=NULL; s->next=NULL;
while(heap!=NULL)
{
p=(ntree)malloc(sizeof(treenode));
p->code=heap->code;
p->values=heap->values;p->lchild=NULL;p->rchild=NULL;
t=(listqueue)malloc(sizeof(queuenode));
t->values=heap->values;
t->pntree=p; s->next=t; t->next=NULL;
s=s->next;
heap=heap->next;
}
return k;
}
listnode creatleaflist(char s[]) /*輸入字符序列,確定葉子結(jié)點及權(quán)值*/
{listnode heap,p,r;
int i,j=0;
p=(listnode)malloc(sizeof(Lnode));
heap=p;
p->next=NULL;p->code=s[0];p->values=1;
while(s[j]!=NULL)
{for(i=0;i<j;i++)
{ if(s[i]==s[j])
{ r=heap;
while(s[i]!=r->code)
r=r->next;
if(s[i]==r->code)
r->values++;
break;
}/*if(s[i]==s[j])*/
else
{ if(i==(j-1))
{
r=(listnode)malloc(sizeof(Lnode));
p->next=r;r->next=NULL;
p=p->next;p->code=s[j];p->values=1;
break;
}/*if(i==(j-1))*/
}
}/*for(i=0;i<j;i++)*/
j++;
} /*while(s[j]!=NULL)*/
return heap;
}
listqueue heaplistqueue(listqueue heapqueue) /*確定哈曼樹*/
{listqueue pk,pks,rs,rss,freep;
int a,b,i,j;
ntree pa,pb,ps;
pk=heapqueue;
pks=heapqueue;
while(pk->next->next!=NULL)
{ i=0,j=0; /*用來確定是否刪除最小兩權(quán)植的結(jié)點*/
pks=pks->next;
a=pks->values;
b=pks->next->values;
while(pks->next!=NULL) /*找到最小兩個權(quán)值*/
{pks=pks->next;
if(a>pks->values){b=a;a=pks->values;}
else{if(b>pks->values) b=pks->values;}
}
rs=pk;
heapqueue=rs->next;
while(heapqueue!=NULL)
{ if(heapqueue->values==a&&i==0)
{pa=heapqueue->pntree;
rs->next=heapqueue->next;
freep=heapqueue;
free(freep);
heapqueue=rs->next;
i=1;
}else
{if(heapqueue->values==b&&j==0)
{pb=heapqueue->pntree;
rs->next=heapqueue->next;
freep=heapqueue;
free(freep);
heapqueue=rs->next;
j=1;
}else
{rs=heapqueue;
heapqueue=heapqueue->next;
}
}
}/*while(heapqueue!=NULL)*/
heapqueue=rs;
ps=(ntree)malloc(sizeof(treenode));
ps->code=‘‘;ps->lchild=pa;ps->rchild=pb;ps->values=a+b;
rss=(listqueue)malloc(sizeof(queuenode));
rss->values=a+b;
rss->pntree=ps;
heapqueue->next=rss;
rss->next=NULL;
pks=pk;
}
return pk;
}
void printdata(ntree heaptree) /*輸出哈夫曼編碼及輸出哈夫曼樹*/
{
int j;
if(heaptree->code!=‘‘)
{printf("%d",heaptree->values);
printf(" %c",chr);
printf("%c",heaptree->code);
printf("--(");
huff[huffrecod].code=heaptree->code;
huff[huffrecod].count=i;
for(j=0;j<i;j++){
huff[huffrecod].huffuman[j]=h[j];
printf("%d",h[j]);
}
printf(")\n");
huffrecod++;
i--;
}
else
{h[i++]=0;
v=i;
printf("%d\n",heaptree->values);
while(--v)
printf("%c ",chp);
printf("%c ",k);
printdata(heaptree->lchild);
h[i++]=1;
t=i;
while(--t)
printf("%c ",chp);
printf("%c ",K);
printdata(heaptree->rchild);
i--;
}
}
void encode(char s[],huffmanchar huff)
{ int nt=0;
int hfcount;
int ihuff;
while(s[nt]!=NULL)
{ for(ihuff=0;ihuff<50;ihuff++)
{ if(s[nt]==huff[ihuff].code)
{for(hfcount=0;hfcount<huff[ihuff].count;hfcount++)
{printf("%d",huff[ihuff].huffuman[hfcount]);
}
break;
}
}
nt++;
}
}
void main()
{
int i=0,j=0;
char s[n],c;
listnode heap; /*確定葉子結(jié)點*/
ntree heaptree; /*確定哈夫曼樹*/
listqueue heapqueue,pk;
huff=(huffmanchar)malloc(50*sizeof(charhuffman));
gotoxy(30+wherex(),wherey());
textcolor(BLACK);
textbackground(WHITE);
cprintf("This huffman program\r\n");
printf("please input char list and in Enter of end!:\n\n");
c=getchar();
if(c==‘\n‘)
exit(0);
while(c!=‘\n‘)
{s[i++]=c;
c=getchar();
}
heap=creatleaflist(s);
heapqueue=returnroot(heap);
printf("\nthe chars appear of frequency:\n");
while(heap!=NULL)
{printf("%c%d\t",heap->code,heap->values);
heap=heap->next;
}
printf("\n");
pk=heaplistqueue(heapqueue);
printf("\nthe huffman:\n");
printdata(pk->next->pntree);
printf("\nthe char coding:\n");
for(j=0;j<huffrecod;j++)
{ printf("%c-",huff[j].code);
for(i=0;i<huff[j].count;i++)
{printf("%d",huff[j].huffuman[i]);}
printf("\t");
}
printf("\nthe chars encode:\nSTART--<");
encode(s,huff);
printf(">--END");
getch();
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -