哈弗曼编译码问题,应该是出在指针问题,高手帮帮忙!!
问题应该出在第一个函数中:出现乱码,我很难找其它函数的错误;帮帮忙!!
//哈夫曼编译码
#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
using namespace std;
const int MAXINT=1000000;
struct www{ //记录字符和该字符数的结构
int wwl;
char chl;
};
typedef struct www**wtt;
struct htnode{//哈夫曼树结点的结构
int ww;
int parent ,llink,rlink;
char ch;
};
struct httree{//哈夫曼数结构
int m;
int root;
struct htnode *ht;
};
typedef struct httree *phttree;
//统计文件中各字符出现次数
int statistics(struct www**pt)
{
ifstream fin;
fin.open("a.txt");
if(!fin.is_open())
{
cout<<"can not open the file"<<endl;
exit(EXIT_FAILURE);
}
struct www wl[256];
for(int i=0; i<256;i++)
{
wl[i].wwl=0;wl[i].chl='w';
}
int ch;
while((ch=fin.get())!=EOF)//统计256个字符在文件a中分别出现的次数
{
wl[ch].wwl++;
wl[ch].chl=ch;
}
for(i=0;i<256;i++)
{cout<<wl[i].wwl<<wl[i].chl;}//显示256个字符在文件a中分别出现的次数
fin.close();
int j=0;
for(i=0;i<256;i++)
{
if(wl[i].wwl!=0)//筛选个数不为0的字符
{
pt[j]=new struct www; //为结构申请空间申请空间,可是没有为指针pt[j]申请空间,但下面还是显示了指针的地址
if(pt[j]==NULL)cout<<"out of space"<<endl;
(*pt[j]).wwl=wl[i].wwl;
(*pt[j]).chl=wl[i].chl;
j++;
}
}
for(i=0;i<j;i++)//第一次显示指针地址
{
cout<<&pt[i]<<endl;
}
for(i=0;i<j;i++)//显示个数不为0的字符
{
cout<<pt[i]->wwl<<' '<<pt[i]->chl<<' ';
}
return j;
}
//构建哈夫曼树
phttree huffman(int m,struct www*pt)
{ cout<<endl; cout<<m;cout<<"??";for(int k=0;k<m;k++){cout<<pt[k].wwl<<pt[k].chl<<endl;}
phttree pht;
int i, j,x1,x2,m1,m2;
pht=new struct httree;
if(pht==NULL){cout<<"out of space"<<endl;return pht;}
pht ->ht=new struct htnode[2*m-1];
if(pht->ht==NULL){cout<<"out of space"<<endl;return pht;}
for(i=0;i<2*m-1;i++)
{cout<<7;
pht->ht[i].llink=-1;
pht->ht[i].rlink=-1;
pht->ht[i].parent=-1;
if(i<m)
{ cout<<8;
pht->ht[i].ww=pt[i].wwl;
pht->ht[i].ch=pt[i].chl;
}
else
{cout<<9;
pht->ht[i].ww=-1;
pht->ht[i].ch='a';
}cout<<0;
}
for(i=0;i<m-1;i++)
{
m1=MAXINT ;m2=MAXINT;
x1=-1;x2=-1;
for(j=0;j<m;j++)
{
if(pht->ht[j].ww<m1&&pht->ht[j].parent==-1)
{
m2=m1;x2=x1;m1=pht->ht[j].ww;x1=j;
}
else if(pht->ht[j].ww<m2&&pht->ht[j].parent==-1)
{
m2=pht->ht[j].ww;x2=j;
}
}
pht->ht[x1].parent=m+i;
pht->ht[x2].parent=m+i;
pht->ht[m+i].ww=m1+m2;
pht->ht[m+i].llink=x1;
pht->ht[m+i].rlink=x2;
}
pht->root=2*m-2;
return pht;
}
//求个字符编码
void coding(htnode *Ht,int n,char***p)
{
int i,c,f,s; char cd[MAXINT];
for (i=0;i<n;++i)
{ cout<<1;
cd[n-1]='\0'; s=n-1;
for (c=i,f=Ht[i].parent;f!=-1;c=f,f=Ht[f].parent)
{ if (Ht[f].llink==c){ cd[--s]='0';cout<<5;}
else if (Ht[f].rlink==c){ cd[--s]='1';}
}cout<<4;
(*p)[i]=new char[n-s];
if((*p)[i]==NULL)//按编码实际长度申请字符串空间
strcpy((*p)[i],&cd[s]); //将编码复制到指定位置
}
}
//为文件编码
void codingfile(htnode*ht,char**p,int m)
{ ifstream fin;
fin.open("a.txt");
if(!fin.is_open()){cout<<"can not open the file"<<endl;exit(EXIT_FAILURE);}
ofstream fout;
fout.open("b.txt");
if(!fout.is_open()){cout<<"can not open the file"<<endl;exit(EXIT_FAILURE);}
int chl;
while((chl=fin.get())!=EOF)
{
for(int i=0;i<m;i++)
{
if( ht[i].ch==chl)
{
fout<<p[i];
break;
}
}
}
fin.close();
fout.close();
}
//译码
void translate(htnode *ht,int m)
{
ifstream fin;
fin.open("b.txt");
if(!fin.is_open()){cout<<"can not open the file"<<endl;exit(EXIT_FAILURE);}
ofstream fout;
fout.open("c.txt");
if(!fout.is_open()){cout<<"can not open the file"<<endl;exit(EXIT_FAILURE);}
char chl=fin.get();
int f=2*m-2;
int F;
while(chl!=EOF)
{
while(f!=-1)
{
F=f;
if(chl==0)f=ht[f].llink;
if(chl==1)f=ht[f].rlink;
chl=fin.get();
}
fout<<ht[F].ch;
}
}
int main(){
int i;
struct www*pl;
int m;
m=statistics(&pl);
cout<<"......................"<<endl;
for(i=0;i<m;i++)//再次显示指针地址,
{
cout<<&(&pl)[i]<<endl;
}
cout<<"....................."<<endl;
for(i=0;i<m;i++)//显示结构地址 这里能得到结构的地址,但下面却不可以访问到结构成员
{
cout<<(&pl)[i]<<endl;
}
for(i=0;i<m;i++)//但在这里没能得到指针指向的结构的成员?...如果去掉这for语句,在构建哈夫曼树就显示乱码?
{
cout<<(&pl)[i]->wwl<<' '<<(&pl)[i]->chl;
}
cout<<m;
phttree asd;
cout<<5;
asd=huffman(m,pl);
cout<<6;
htnode*HT;
HT=asd->ht;
char**pt;
for( i=0;i<2*m-1;i++)
{cout<<HT[i].ww<<HT[i].ch;}cout<<endl<<endl;
coding(HT,m,&pt);
codingfile(HT,pt,m);
translate(HT,m);
cout<<7;
system("pause");
return 0;
}