哈夫曼编码,给跪了,求大神,有错。#include<iostream> #include<string> #include<fstream> 100
usingnamespacestd;#defineN100typedefstruct{floatweight;intparent,lchild,rchild;}*HTNo...
using namespace std;
#define N 100
typedef struct{
float weight;
int parent,lchild,rchild;
}*HTNode,hufftree;
typedef struct{
char co[N];
int start;
}HCode;
int* frequent(string &str,int &n)//统计各个字符出现的频率
{ int i=0,j=0;int *weight=new int[n];string s;
char ch[256]={0};
ifstream in;
in.open("input.txt");
if(!in)
{ cout<<"cannot open input file.\n"; exit(1); }
while(!in.eof())
{getline(in,s); str+=s; }
while(i<str.size())
{ ch[str[i]]++;i++; }
for(i=0;i<256;i++)
{if(ch[i]!=0)//记录过的所有字符
n++;//结点个数n}
for(i=0;i<n;i++)
{ if(ch[i]!=0)
weight[j++]=ch[i];}
in.close();
return weight;
}
void createHT(int *w,HTNode &T,int &n)//建立哈夫曼树
{
int i; T=new hufftree[2*n-1];
for(i=0;i<n;i++)
T[i].weight=w[i];
for(i=0;i<2*n-1;i++)
{T[i].parent=T[i].lchild=T[i].rchild=-1;}
for(i=n;i<2*n-1;i++)
{double min1,min2;
min1=min2=32767;
int p1,p2;p1=p2=-1;
for(int j=0;j<i;j++)
{if(T[j].parent==-1)
{if(T[j].weight<min1)
{min2=min1;p2=p1;min1=T[j].weight;p1=j;}
else if(T[j].weight<min2)
{min2=T[i].weight;p2=j;}}}
T[p1].parent=T[p2].parent=i;
T[i].lchild=p1;
T[i].rchild=p2;
T[i].weight=T[p1].weight+T[p2].weight;
}}
void Huffmancoding(HTNode &T,int &n,char ch[],string &str)
{int c,p,i;
HCode C;string stri;int j,k;
ofstream out("output.txt",ios::trunc);
for (k=0;k<256;k++)
{if (ch[k]!=0)
{ out<<(char)k<<":";
while (T[i].weight!=ch[k])
i++;
T[i].weight=0;ch[k]=i;
C.start=n;c=i;
while((p=T[c].parent)!=-1)
{ if(T[p].lchild==c)
C.co[C.start--]='0';
else
C.co[C.start--]='1';
c=p;
for (j=C.start;j<n;j++)
out<<C.co[j];
out<<endl;
}}
for(int m=0;m<str.size();m++)
{c=ch[str[m]];
C.start=n;
while((p=T[c].parent)!=-1)
{ if(T[p].lchild==c)
C.co[C.start--]='0';
else
C.co[C.start--]='1';
c=p;}
for(j=C.start;j<n;j++)
stri+=C.co[j];
}
out<<stri;
out.close();
}
int main()
{string str;
int n=0,*w;
HTNode T;
char ch[256];
w=frequent(str,n);
createHT(w,T,n);
Huffmancoding(T,n,ch,str);
return 0;}
数据从文件读入,不能运行
因为字数限制本来括号占一行给缩写了,见谅 展开
#define N 100
typedef struct{
float weight;
int parent,lchild,rchild;
}*HTNode,hufftree;
typedef struct{
char co[N];
int start;
}HCode;
int* frequent(string &str,int &n)//统计各个字符出现的频率
{ int i=0,j=0;int *weight=new int[n];string s;
char ch[256]={0};
ifstream in;
in.open("input.txt");
if(!in)
{ cout<<"cannot open input file.\n"; exit(1); }
while(!in.eof())
{getline(in,s); str+=s; }
while(i<str.size())
{ ch[str[i]]++;i++; }
for(i=0;i<256;i++)
{if(ch[i]!=0)//记录过的所有字符
n++;//结点个数n}
for(i=0;i<n;i++)
{ if(ch[i]!=0)
weight[j++]=ch[i];}
in.close();
return weight;
}
void createHT(int *w,HTNode &T,int &n)//建立哈夫曼树
{
int i; T=new hufftree[2*n-1];
for(i=0;i<n;i++)
T[i].weight=w[i];
for(i=0;i<2*n-1;i++)
{T[i].parent=T[i].lchild=T[i].rchild=-1;}
for(i=n;i<2*n-1;i++)
{double min1,min2;
min1=min2=32767;
int p1,p2;p1=p2=-1;
for(int j=0;j<i;j++)
{if(T[j].parent==-1)
{if(T[j].weight<min1)
{min2=min1;p2=p1;min1=T[j].weight;p1=j;}
else if(T[j].weight<min2)
{min2=T[i].weight;p2=j;}}}
T[p1].parent=T[p2].parent=i;
T[i].lchild=p1;
T[i].rchild=p2;
T[i].weight=T[p1].weight+T[p2].weight;
}}
void Huffmancoding(HTNode &T,int &n,char ch[],string &str)
{int c,p,i;
HCode C;string stri;int j,k;
ofstream out("output.txt",ios::trunc);
for (k=0;k<256;k++)
{if (ch[k]!=0)
{ out<<(char)k<<":";
while (T[i].weight!=ch[k])
i++;
T[i].weight=0;ch[k]=i;
C.start=n;c=i;
while((p=T[c].parent)!=-1)
{ if(T[p].lchild==c)
C.co[C.start--]='0';
else
C.co[C.start--]='1';
c=p;
for (j=C.start;j<n;j++)
out<<C.co[j];
out<<endl;
}}
for(int m=0;m<str.size();m++)
{c=ch[str[m]];
C.start=n;
while((p=T[c].parent)!=-1)
{ if(T[p].lchild==c)
C.co[C.start--]='0';
else
C.co[C.start--]='1';
c=p;}
for(j=C.start;j<n;j++)
stri+=C.co[j];
}
out<<stri;
out.close();
}
int main()
{string str;
int n=0,*w;
HTNode T;
char ch[256];
w=frequent(str,n);
createHT(w,T,n);
Huffmancoding(T,n,ch,str);
return 0;}
数据从文件读入,不能运行
因为字数限制本来括号占一行给缩写了,见谅 展开
2个回答
2013-05-22 · 知道合伙人互联网行家
关注
展开全部
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
using namespace std;
#define N 100
typedef struct{
float weight;
int parent,lchild,rchild;
}*HTNode,hufftree;
typedef struct{
char co[N];
int start;
}HCode;
int* frequent(string &str,int &n)
{
int i=0,j=0;int *weight=new int[n];string s;
char ch[256]={0};
ifstream in;
in.open("input.txt");
if(!in)
{
cout<<"cannot open input file.\n"; exit(1);
}
while(!in.eof())
{
getline(in,s); str+=s;
}
while(i<str.size())
{
ch[str[i]]++;i++;
}
for(i=0;i<256;i++)
{
if(ch[i]!=0)
n++;
}
for(i=0;i<n;i++)
{
if(ch[i]!=0)
weight[j++]=ch[i];
}
in.close();
return weight;
}
void createHT(int *w,HTNode &T,int &n)
{
int i; T=new hufftree[2*n-1];
for(i=0;i<n;i++)
T[i].weight=w[i];
for(i=0;i<2*n-1;i++)
{T[i].parent=T[i].lchild=T[i].rchild=-1;}
for(i=n;i<2*n-1;i++)
{
double min1,min2;
min1=min2=32767;
int p1,p2;p1=p2=-1;
for(int j=0;j<i;j++)
{
if(T[j].parent==-1)
{
if(T[j].weight<min1)
{
min2=min1;p2=p1;min1=T[j].weight;p1=j;
}
else if(T[j].weight<min2)
{
min2=T[i].weight;p2=j;
}
}
}
T[p1].parent=T[p2].parent=i;
T[i].lchild=p1;
T[i].rchild=p2;
T[i].weight=T[p1].weight+T[p2].weight;
}
}
void Huffmancoding(HTNode &T,int &n,char ch[],string &str)
{
int c,p,i = 0;
HCode C;string stri;int j,k;
ofstream out("output.txt",ios::trunc);
for (k=0;k<256;k++)
{
if (ch[k]!=0)
{
out<<(char)k<<":";
while (T[i].weight!=ch[k])
i++;
T[i].weight=0;ch[k]=i;
C.start=n;c=i;
while((p=T[c].parent)!=-1)
{
if(T[p].lchild==c)
C.co[C.start--]='0';
else
C.co[C.start--]='1';
c=p;
for (j=C.start;j<n;j++)
out<<C.co[j];
out<<endl;
}
}
for(int m=0;m<str.size();m++)
{
c=ch[str[m]];
C.start=n;
while((p=T[c].parent)!=-1)
{
if(T[p].lchild==c)
C.co[C.start--]='0';
else
C.co[C.start--]='1';
c=p;
}
for(j=C.start;j<n;j++)
stri+=C.co[j];
}
out << "tst";
out<<stri;
out.close();
}
}
int main()
{
string str;
int n=0,*w;
HTNode T;
char ch[256];
w=frequent(str,n);
createHT(w,T,n);
Huffmancoding(T,n,ch,str);
return 0;
}
只修改了错误,没有语法错误了,但是没看能不能实现想要的功能
n++;//结点个数n} //你的这个注释误把一个大括号注释掉了
createHT函数最后也少一个大括号,
另外还需要包含string头文件
就这些吧
追问
放到网上来的时候字数不够,把一些东西误删了,运行了功能不能实现。不知道错在哪
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询