📄 hdlg.cpp
字号:
}
if(!ptr->lchild&&!ptr->rchild)
{
ptr->mark=1;
code[index++]='\0';
b1[k2++]=ptr->weight;
ptr=tree;
index=0;
}
if(ptr->lchild->mark==1&&ptr->rchild->mark==1)
{
ptr->mark=1;
ptr=tree;
index=0;
}
}
for(k3=0;k3<k2;k3++)
{
a1[k1++]=b1[k3];
}
for(k4=0;k4<k1;k4++)
{
k5+=a1[k4];
}
for(k4=0;k4<k1;k4++)
{
c2[k4]=(float)a1[k4]/k5;
}
for(int i=0;i<k1;i++)
{
c1-= c2[i] * log(c2[i]) / log(2.0);
}
return c1;
}
//计算平均码长
double d1=0.0;
double CHDlg::Huffmancoding2(linktree tree)
{
int index=0,k1=0,k2=0,k3,k4,k5=0;
int kk1=0,kk2=0,kk3;
int a1[100],b1[100],aa1[100],bb1[100];
float c2[3];
linktree ptr=tree;
code=(char *)malloc(10*sizeof(char));
while(ptr->lchild&&ptr->rchild&&ptr->mark==0)
{
while(ptr->lchild&&ptr->lchild->mark==0)
{
ptr=ptr->lchild;
code[index++]='0';
if(!ptr->rchild&&!ptr->lchild)
{
ptr->mark=1;
code[index++]='\0';
aa1[kk1++]=strlen(code);
a1[k1++]=ptr->weight;
ptr=tree;
index=0;
}
}
if(ptr->rchild&&ptr->rchild->mark==0)
{
ptr=ptr->rchild;
code[index++]='1';
}
if(!ptr->lchild&&!ptr->rchild)
{
ptr->mark=1;
code[index++]='\0';
bb1[kk2++]=strlen(code);
b1[k2++]=ptr->weight;
ptr=tree;
index=0;
}
if(ptr->lchild->mark==1&&ptr->rchild->mark==1)
{
ptr->mark=1;
ptr=tree;
index=0;
}
}
for(k3=0;k3<k2;k3++)
{
a1[k1++]=b1[k3];
}
for(kk3=0;kk3<kk2;kk3++)
{
aa1[kk1++]=bb1[kk3];
}
for(k4=0;k4<k1;k4++)
{
k5+=a1[k4];
}
for(k4=0;k4<k1;k4++)
{
c2[k4]=(float)a1[k4]/k5;
}
for(int i=0;i<kk1;i++)
{
d1+= c2[i] * aa1[i];
}
return d1;
}
/*解码 */
char *CHDlg::decode(linktree tree,char code[])
{
int i=0,j=0,k=0;
char *char0_1;
linktree ptr=tree;
char0_1=(char *)malloc(10*sizeof(char));/*此数组用于统计输入的0、1序列*/
for(j=0,ptr=tree;code[i]!='\0'&&ptr->lchild&&ptr->rchild;j=0,ptr=tree)
{
for(j=0;code[i]!='\0'&&ptr->lchild&&ptr->rchild;j++,i++)
{
if(code[i]=='0')
{
ptr=ptr->lchild;
char0_1[j]='0';
}
if(code[i]=='1')
{
ptr=ptr->rchild;
char0_1[j]='1';
}
}
if(!ptr->lchild&&!ptr->rchild)
{
char0_1[j]='\0';
strncat(code8,&(ptr->ch),sizeof(char));
}
}
strcpy(code9,code8);
return code9;
}
void CHDlg::OnButton4()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
char character[MAXLEN];
strcpy(character,m_x);
int s=strlen(character);
linktree temp,ht,htree,ptr=NULL;
temp=initialize(character); //初始化链表
ht=Order(temp); //对链表进行排序
htree=createHftree(ht); //对排序好的链表建立哈夫曼表
m_u=Huffmancoding1(htree);
UpdateData(FALSE);
}
void CHDlg::OnButton5()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
char character[MAXLEN];
strcpy(character,m_x);
int s=strlen(character);
linktree temp,ht,htree,ptr=NULL;
temp=initialize(character); //初始化链表
ht=Order(temp); //对链表进行排序
htree=createHftree(ht); //对排序好的链表建立哈夫曼表
m_v=Huffmancoding2(htree);
UpdateData(FALSE);
}
void CHDlg::OnButton6()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
m_w=c1/d1;
UpdateData(FALSE);
}
void CHDlg::OnSelendcancelCombo1()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
char character[MAXLEN];
strcpy(character,m_x);
int s=strlen(character);
linktree temp,ht,htree,ptr=NULL;
temp=initialize(character); //初始化链表
ht=Order(temp); //对链表进行排序
if(ht->next->weight==s) //判断是否是非法格式
{
MessageBox("信号只有一个字符,无法编码\n","提示:");
}
htree=createHftree(ht); //对排序好的链表建立哈夫曼表
m_y=Huffmancoding(htree); //对哈夫曼表再进行处理,打印输出
UpdateData(FALSE);
}
void CHDlg::OnSelendcancelCombo2()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
int s=0;
char b[7],c[4],h[4]="000";
char character[MAXLEN];
strcpy(character,m_r);
while(character[s]!='\0') //统计字符
s++;
if(s%4!=0)
{
if(s%4==1)
{
r++;
s=s+3;
strncat(character,h,3);
}
if(s%4==2)
{
g++;
s=s+2;
strncat(character,h,2);
}
if(s%4==3)
{
t++;
s=s+1;
strncat(character,h,1);
}
}
for(int w=1;4*w<s+1;w++ )
{
for(int i=0;i<4;i++)
{
c[i]=character[i];
}
b[0]=c[0];
b[1]=c[1];
b[2]=c[2];
b[3]=c[3];
b[4]=b[1]^b[2]^b[3];
b[5]=b[0]^b[2]^b[3];
b[6]=b[0]^b[1]^b[3];
strncat(code3,b,7);
int j=1;
for( i=0;i<s-4*w;i++)
{
character[i]=character[i+4*j];
}
}
strcpy(code4,code3);
m_z=code4;
UpdateData(FALSE);
}
void CHDlg::OnSelendcancelCombo3()
{
// TODO: Add your control notification handler code here
int b;
UpdateData(TRUE);
int m[3],n[7],e[1],s=0,f;
char y[4];
int d[3][7]={{0,0,0,1,1,1,1},{0,1,1,0,0,1,1},{1,0,1,0,1,0,1}};
char character[MAXLEN];
strcpy(character,m_t);
while(character[s]!='\0') //统计字符
s++;
for(int k=1;7*k<s+1; k++)
{
for(int i=0;i<7;i++)
{
n[i]=character[i]-48;
}
m[0]=d[0][0]*n[0]^d[0][1]*n[1]^d[0][2]*n[2]^d[0][3]*n[3]^d[0][4]*n[4]^d[0][5]*n[5]^d[0][6]*n[6];
m[1]=d[1][0]*n[0]^d[1][1]*n[1]^d[1][2]*n[2]^d[1][3]*n[3]^d[1][4]*n[4]^d[1][5]*n[5]^d[1][6]*n[6];
m[2]=d[2][0]*n[0]^d[2][1]*n[1]^d[2][2]*n[2]^d[2][3]*n[3]^d[2][4]*n[4]^d[2][5]*n[5]^d[2][6]*n[6];
if(!m[0]&&!m[1]&&!m[2])
{
f=0;
for( i=0;i<4;i++)
{
y[i]=char(n[i]+48);
}
strncat(code5,y,4);
// m_o=f+7*(k-1);
}
else if(m[0]&&m[1]&&m[2])
{
e[0]=n[6];
n[6]=!e[0];
f=7;
for( i=0;i<4;i++)
{
y[i]=char(n[i]+48);
}
strncat(code5,y,4);
m_o=f+7*(k-1);
}
else if(m[0]&&m[1]&&!m[2])
{
e[0]=n[5];
n[5]=!e[0];
f=6;
for(int i=0;i<4;i++)
{
y[i]=char(n[i]+48);
}
strncat(code5,y,4);
m_o=f+7*(k-1);
}
else if(m[0]&&!m[1]&&m[2])
{
e[0]=n[4];
n[4]=!e[0];
f=5;
for( i=0;i<4;i++)
{
y[i]=char(n[i]+48);
}
strncat(code5,y,4);
m_o=f+7*(k-1);
}
else if(m[0]&&!m[1]&&!m[2])
{
e[0]=n[3];
n[3]=!e[0];
f=4;
for( i=0;i<4;i++)
{
y[i]=char(n[i]+48);
}
strncat(code5,y,4);
m_o=f+7*(k-1);
}
else if(!m[0]&&m[1]&&m[2])
{
e[0]=n[2];
n[2]=!e[0];
f=3;
for( i=0;i<4;i++)
{
y[i]=char(n[i]+48);
}
strncat(code5,y,4);
m_o=f+7*(k-1);
}
else if(!m[0]&&m[1]&&!m[2])
{
e[0]=n[1];
n[1]=!e[0];
f=2;
for( i=0;i<4;i++)
{
y[i]=char(n[i]+48);
}
strncat(code5,y,4);
m_o=f+7*(k-1);
}
else if(!m[0]&&!m[1]&&m[2])
{
e[0]=n[0];
n[0]=!e[0];
f=1;
for( i=0;i<4;i++)
{
y[i]=char(n[i]+48);
}
strncat(code5,y,4);
m_o=f+7*(k-1);
}
int j=1;
for( i=0;i<s-7*j;i++)
{
character[i]=character[i+7*j];
}
}
if(r!=0)
{ strcpy(code6,code5);
b=strlen(code6)-3;
strncpy(code10,code6,b);
m_p=code10;
}
else if(g!=0)
{
strcpy(code6,code5);
b=strlen(code6)-2;
strncpy(code10,code6,b);
m_p=code10;
}
else if(t!=0)
{
strcpy(code6,code5);
b=strlen(code6)-1;
strncpy(code10,code6,b);
m_p=code10;
}
else
{
strcpy(code10,code5);
m_p=code10;
}
UpdateData(FALSE);
}
void CHDlg::OnSelendcancelCombo4()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
char character[MAXLEN];
strcpy(character,m_x);
strcpy(code7,m_s);
linktree temp,ht,htree,ptr=NULL;
temp=initialize(character); //初始化链表
ht=Order(temp); //对链表进行排序
htree=createHftree(ht); //对排序好的链表建立哈夫曼表
m_q=decode(htree,code7); //解码函数
UpdateData(FALSE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -