📄 hafuman.cpp
字号:
{
min2=t->amount;
min2_pos=t;
min2_c=t->ch;
min2_son=t->son;
}
}
min_pri=l;
while(min_pri->next!=min2_pos)
min_pri=min_pri->next;
min_pri->next=min2_pos->next; //寻找第二个小结点完成
if(min1_son==NULL)
{
left=(bt)malloc(sizeof(tnode));
left->amount=min1;
left->ch=min1_c;
left->left=NULL;
left->right=NULL;
}
else
left=min1_son; //left指向生成的二叉子树根结点
if(min2_son==NULL) //结点子树指针为空 ,则分配新结点
{
right=(bt)malloc(sizeof(tnode)); //第二小结点往右
right->amount=min2;
right->ch=min2_c;
right->left=NULL;
right->right=NULL;
}
else
right=min2_son;
root=(bt)malloc(sizeof(tnode));
root->amount=min1+min2;
root->ch='\0';
root->left=left;
root->right=right; //生成一个对应上面已产生二叉子树地址的链表结点
made=(L)malloc(sizeof(Node));
made->amount=root->amount;
made->ch=root->ch;
made->next=NULL;
made->son=root; //将生成的链表结点插入链表中
temp_p=l;
while(temp_p->next!=NULL)
temp_p=temp_p->next; //退出循环时temp_p指向链表最后一个结点
temp_p->next=made; //将生成的结点插入链表
}
temp_p=l->next;
return temp_p->son;
}
void encoding() //根据建立的哈夫曼树编码
{
stack code,top,temp,readcode;
bt r;
huff temp_huff,hp;
char huff[100]=""; //用于存储当前字符
int i,j,k=0,n=0;
hlist=(hnode*)malloc(sizeof(hnode));
hlist->ch='\0';
for(i=0;i<100;i++)
hlist->code[i]='\0';
hlist->next=NULL;
hp=hlist;
//建立空栈
code=(stack)malloc(sizeof(snode));
code->amount=0;
code->ch='\0';
code->next=NULL;
code->son=NULL; //栈的头结点指向树的根结点
top=code;
r=root;
temp=(stack)malloc(sizeof(snode)); //给根结点分配栈结点
temp->amount=r->amount;
temp->ch='0';
temp->next=top;
temp->son=r;
top=temp;
while(r!=NULL)
{
if(r->left!=NULL&&r->left->amount!=-1) //当前结点有左孩子
{
r=r->left;
r->amount=-1;
temp=(stack)malloc(sizeof(snode));
temp->amount=r->amount;
temp->ch='0';
temp->next=top;
temp->son=r;
top=temp;
}
else if(r->right!=NULL&&r->right->amount!=-1) //当前结点有右孩子
{
r=r->right;
r->amount=-1;
temp=(stack)malloc(sizeof(snode)); //给右孩子分配栈结点
temp->amount=r->amount;
temp->ch='1';
temp->next=top;
temp->son=r;
top=temp;
}
else
{
if(r->left==NULL&&r->right==NULL)
{
for(i=0;i<100;i++) //哈夫曼编码数组初始化
huff[i]='\0';
printf("字符%c,编码为:",r->ch);
difrnt_amount[k].ch=r->ch ;
difrnt_amount[k].next=NULL;k++;
readcode=top;
i=0;
while(readcode!=code)
{
huff[i++]=readcode->ch;
readcode=readcode->next;
}
temp_huff=(hnode*)malloc(sizeof(hnode));
temp_huff->ch=r->ch;
for(j=0;j<100;j++)
temp_huff->code[j]='\0';
j=0;
for(i=i-2;i>=0;i--) //单个字符对应的编码
{
printf("%c",huff[i]);
temp_huff->code[j++]=huff[i];
difrnt_amount[k-1].code[j-1]=huff[i];
}
temp_huff->next=NULL;
hp->next=temp_huff;
hp=temp_huff;
printf("\t\t");
if(++n%2==0)
printf("\n");
}
if(top->next!=code) //栈非空
{
top=top->next;
r=top->son;
}
else
break;
}
}
}
void Search(char *str) //在difrnt_mount中查找字符ch,输出其编码
{
int j,i;
for(i=0;i<=int_amount;i++)
for(j=0;j<=int_num;j++)
if(str[i]==difrnt_amount[j].ch)
{
printf("%s",difrnt_amount[j].code);
strcat(tmpstring,difrnt_amount[j].code);//此句用于保存译码
break;
}
printf("\n");
}
void opencode(bt tmptree) //解码函数
{
if(!tmptree->left&&!tmptree->right)
{
printf("%c",tmptree->ch);
rongqi[abc++]=tmptree->ch;
}
else if(*zifu=='0'&&tmptree->left)
{
zifu++;
opencode(tmptree->left);
}
else if(*zifu=='1'&&tmptree->right)
{
zifu++;
opencode(tmptree->right);
}
else
{
zifu++;
printf("遇到一个未知字符【%c】",*zifu);
}
}
//*****************************************************以下为文件操作
void SavaData(hnode difrnt_amount[]) //保存数据及其编码
{
FILE *fp;
int i=0;
if((fp=fopen("d:\\data&code","wb"))==NULL)
{
puts("文件打开错误!");
getchar();
exit(0);
}
else
while(difrnt_amount[i].ch)
{
fputc('(',fp);
fputc(difrnt_amount[i].ch,fp);
fputs(difrnt_amount[i].code,fp);
fputc(')',fp);
i++;
}
fclose(fp);
}
void Readtxt() //读取要编译的文件
{
FILE *fp;
int i=0;
char ch;
l=(Node*)malloc(sizeof(Node));
l->ch='\0';
l->amount=0;
l->son=NULL;
l->next=NULL;
str=t;
if((fp=fopen("d:\\ToBeTrans.txt","r"))==NULL)
{
puts("文件打开错误!");
getchar();
exit(0);
}
while((ch=fgetc(fp))!=EOF)
str[i++]=ch;
str[i]='\0';
fclose(fp);
}
void Savacode(char *codestr) //保存译码
{
FILE *fp;
int i=0;
if((fp=fopen("d:\\CodeFile.txt","w"))==NULL)
{
puts("文件打开错误!");
getchar();
exit(0);
}
fputs(codestr,fp);
fclose(fp);
}
void SaveText() //保存解码后的文件
{
FILE *fp;
if((fp=fopen("d:\\TextFile.txt","w"))==NULL)
{
puts("文件打开错误!");
getchar();
exit(0);
}
fputs(rongqi,fp); //tmpstring为全局字符串指针变量
fclose(fp);
}
void ReadCode() //读取要解码的文件
{
FILE *fp;
int i=0;
char ch;
zifu=t; //初始化,即让字符串型指针可以当数组用
if((fp=fopen("d:\\CodeFile.txt","r"))==NULL)
{
puts("文件打开错误!");
getchar();
exit(0);
}
while((ch=fgetc(fp))!=EOF)
zifu[i++]=ch;
zifu[i]='\0';
fclose(fp);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -