我用CB6编的一个HAFFMAN编码的演示程序,始终通不过。希望教各位高手不吝赐教!

bluespears 2003-06-03 10:35:25
我的目的是要做一个HAFFMAN编码的演示。就是在edit中输入节点权值,然后按执行,在memo中把生成的haffman编码显示出来。(我希望是左边显示权值,右边显示其对应的编码。但是不知道怎么做)。我在网上找了一个可通过的C代码,然后把它加到CB中去。但是始终没有通过。请各位高手帮小弟改改,千恩万谢,感激不尽!!
我把代码放在下面:
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
#include <dos.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -2
#define STACKSIZE 10
#define INCREASEMENT 5
typedef struct{
char *top;
char *base;
int size;
}STACK;
typedef struct{
int weight;
int parent,lchild,rchild;
}HTNODE,*HUFFMANTREE;
typedef char ** HUFFMANCODE;
int b=0;
HUFFMANTREE ht;
HUFFMANCODE hc;
int n,i;
int *wt;
wt=(int *)malloc(20*sizeof(int));//这里有问题
void huffmancoding(HUFFMANTREE *,HUFFMANCODE *,int *,int);
void gethcode(HUFFMANTREE,HUFFMANCODE *,int);
void select(HUFFMANTREE *,int,int *,int * );//这几个函数实现haffman编码的功能

void select(HUFFMANTREE *ht,int n,int *s1,int *s2)
{
int less,least,i,j,temp;
for(i=1;i<=n;i++)
if((*ht)[i].parent==0){ least=i; break;}
for(i++;i<=n;i++)
if((*ht)[i].parent==0){ less=i; break;}
if((*ht)[least].weight>(*ht)[less].weight){
temp=less;
less=least;
least=temp;
}
for(i++;i<=n;i++){
if((*ht)[i].parent==0){
if((*ht)[i].weight<=(*ht)[least].weight){less=least;least=i;}
else if((*ht)[i].weight<(*ht)[less].weight&&(*ht)[i].weight>=(*ht)[least].weight) less=i;
}
}
*s1=least;
*s2=less;
}
void huffmancoding(HUFFMANTREE *ht,HUFFMANCODE *hc,int *w,int n)
{
int i,m,s1,s2,c,f,start;
HUFFMANTREE p;
char *cd;
if(n<=1) return;
m=2*n-1;
(*ht)=(HUFFMANTREE)malloc((m+1)*sizeof(HTNODE));
for(p=*ht+1,i=1;i<=n;i++,p++,w++){
p->weight=*w;
p->lchild=p->rchild=p->parent=0;
}
for(;i<=m;i++,p++){
p->weight=0;
p->lchild=p->rchild=p->parent=0;
}
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;
}
(*hc)=(HUFFMANCODE)malloc((n+1)*sizeof(char *));
cd=(char *)malloc(sizeof(char)*n);
cd[n-1]='\0';
for(i=1;i<=n;i++){
start=n-1;
for(c=i,f=(*ht)[i].parent;f;c=f,f=(*ht)[f].parent)
if((*ht)[f].lchild==c) cd[--start]='0';
else cd[--start]='1';
(*hc)[i]=(char *)malloc(sizeof(char)*(n-start));
strcpy((*hc)[i],&cd[start]);
}
*ht=p;
free(cd);
}
void gethcode(HUFFMANTREE ht,HUFFMANCODE *hc,int n)
{
int start,c,i,j,f;
char *cd;
(*hc)=(HUFFMANCODE)malloc((n+1)*sizeof(char *));
cd=(char *)malloc(sizeof(char)*n);
cd[n-1]='\0';
for(i=1;i<=n;i++){
start=n-1;
for(c=i,f=(ht)[i].parent;f;c=f,f=(ht)[f].parent)
if((ht)[f].lchild==c) cd[--start]='0';
else cd[--start]='1';
(*hc)[i]=(char *)malloc(sizeof(char)*(n-start));
strcpy((*hc)[i],&cd[start]);
}
free(cd);
}
/////////////////////////////////// ///////////////////////////////
//以下是界面
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//-----------------------------------------------------------------------void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
Form2->Show();
}//这个按钮调出一个已经画好的图出来显示。没有问题
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
this->Edit1->Text="";
Button5->Enabled=true;
}
//上面这个按钮起清空作用---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
Close();
}
//上面是退出按钮---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{ int i;
huffmancoding(&ht,&hc,wt,n);
for(i=1;i<=n;i++)
{
Memo1->Lines->Add((IntToStr("hc[i]")));//我不知道怎么把字符串数组里的字符串显示出来
}
}
//上面这个按钮是执行按钮。要完成在MEMO上显示出HAFFMAN编码的功能。但是始终没有能够实现!---------------------------------------------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
{
wt[b]=Form1->Edit1->Text.ToInt();
b++;
if(wt[b]==-1)
{
Button5->Enabled=false;
b--;}
n=b+1;

}//上面这个按钮是把Edit1中输入的权值一个一个的加到wt数组中。(wt中放节点权值)
//---------------------------------------------------------------------------

以下是c语言的代码,上面的算法是来自于下面这个程序的。(这个代码在tc下可以完全通过)
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -2
#define STACKSIZE 10
#define INCREASEMENT 5
typedef struct{
char *top;
char *base;
int size;
}STACK;
typedef struct{
int weight;
int parent,lchild,rchild;
}HTNODE,*HUFFMANTREE;
typedef char ** HUFFMANCODE;
void huffmancoding(HUFFMANTREE *,HUFFMANCODE *,int *,int);
void gethcode(HUFFMANTREE,HUFFMANCODE *,int);
void select(HUFFMANTREE *,int,int *,int * );

void main()
{
HUFFMANTREE ht;
HUFFMANCODE hc;
/*int w[]={5,29,7,8,14,23,3,11};
int n=8,i; */
int n,i,*w;
printf("\n\npress any key to start!");
getch();
printf("\n\ninput the number:(-1 to end)");
printf("\n");
for(i=0;1;i++){
scanf("%d",&w[i]);
if(w[i]==-1){
i--;
break;
}
}
n=i+1;
huffmancoding(&ht,&hc,w,n);
for(i=1;i<=n;i++) printf("\n\n%s",hc[i]);
}

void select(HUFFMANTREE *ht,int n,int *s1,int *s2)
{
int less,least,i,j,temp;
for(i=1;i<=n;i++)
if((*ht)[i].parent==0){ least=i; break;}
for(i++;i<=n;i++)
if((*ht)[i].parent==0){ less=i; break;}
if((*ht)[least].weight>(*ht)[less].weight){
temp=less;
less=least;
least=temp;
}
for(i++;i<=n;i++){
if((*ht)[i].parent==0){
if((*ht)[i].weight<=(*ht)[least].weight){less=least;least=i;}
else if((*ht)[i].weight<(*ht)[less].weight&&(*ht)[i].weight>=(*ht)[least].weight) less=i;
}
}
*s1=least;
*s2=less;
}
void huffmancoding(HUFFMANTREE *ht,HUFFMANCODE *hc,int *w,int n)
{
int i,m,s1,s2,c,f,start;
HUFFMANTREE p;
char *cd;
if(n<=1) return;
m=2*n-1;
(*ht)=(HUFFMANTREE)malloc((m+1)*sizeof(HTNODE));
for(p=*ht+1,i=1;i<=n;i++,p++,w++){
p->weight=*w;
p->lchild=p->rchild=p->parent=0;
}
for(;i<=m;i++,p++){
p->weight=0;
p->lchild=p->rchild=p->parent=0;
}
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;
}
(*hc)=(HUFFMANCODE)malloc((n+1)*sizeof(char *));
cd=(char *)malloc(sizeof(char)*n);
cd[n-1]='\0';
for(i=1;i<=n;i++){
start=n-1;
for(c=i,f=(*ht)[i].parent;f;c=f,f=(*ht)[f].parent)
if((*ht)[f].lchild==c) cd[--start]='0';
else cd[--start]='1';
(*hc)[i]=(char *)malloc(sizeof(char)*(n-start));
strcpy((*hc)[i],&cd[start]);
}
*ht=p;
free(cd);
}
void gethcode(HUFFMANTREE ht,HUFFMANCODE *hc,int n)
{
int start,c,i,j,f;
char *cd;
(*hc)=(HUFFMANCODE)malloc((n+1)*sizeof(char *));
cd=(char *)malloc(sizeof(char)*n);
cd[n-1]='\0';
for(i=1;i<=n;i++){
start=n-1;
for(c=i,f=(ht)[i].parent;f;c=f,f=(ht)[f].parent)
if((ht)[f].lchild==c) cd[--start]='0';
else cd[--start]='1';
(*hc)[i]=(char *)malloc(sizeof(char)*(n-start));
strcpy((*hc)[i],&cd[start]);
}
free(cd);
}


...全文
15 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧