哈夫曼编/译码器 出现好多错误
#include <iostream.h>#include <fstream.h>
#include <iomanip.h>
#include <string.h>
#include <malloc.h>
#include <stdio.h>
#include <iomanip.h>
const int UINT_MAX=10000;
typedef struct
{
int weight;
int parent,lchild,rchild;
}HTNode,* HuffmanTree; //动态分配数组存储赫夫曼树
typedef char **HuffmanCode; //动态分配数组存储赫夫曼编码表
//--------------------全局变量-----------------------
HuffmanTree HT;
HuffmanCode HC;
int *w,i,j;
const int n=26;
char *z;
int flag=0;
int numb=0;
// -----------------求赫夫曼编码---------------------
int min(HuffmanTree t,int i)
{ // 此函数将要被void select()调用
int j,flag;
int k=UINT_MAX; // 取k为不小于可能的值
for(j=1;j<=i;j++)
if(t[j].weight<k&&t[j].parent==0)
k=t[j].weight,flag=j;
t[flag].parent=1;
return flag;
}
//--------------------slect函数----------------------
void select(HuffmanTree t,int i,int &s1,int &s2)
{ // s1为最小的两个值中序号小的那个
int j;
s1=min(t,i);
s2=min(t,i);
if(s1>s2)
{
j=s1;
s1=s2;
s2=j;
}
}
// -------------------参考课本算法6.12-------------------
void HuffmanCoding(HuffmanTree &HT,HuffmanCode &HC,int *w,int n)
{ // w存放n个字符的权值(均>0),构造赫夫曼树HT,并求出n个字符的赫夫曼编码HC
int m,i,s1,s2,start;
int c,f;
HuffmanTree p;
char *cd;
if(n<=1)
return;//检测结点数是否可以构成树
m=2*n-1;
HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode)); // 0号单元未用
for(p=HT+1,i=1;i<=n;++i,++p,++w)
{
p->weight=*w;
p->parent=0;
p->lchild=0;
p->rchild=0;
}
for(;i<=m;++i,++p)
p->parent=0;
for(i=n+1;i<=m;++i) // 建赫夫曼树
{ //在HT[1~i-1]中选择parent=0且weight最小的两个结点,其序号分别为s1和s2
select(HT,i-1,s1,s2);
HT[s1].parent=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*));
// 分配n个字符编码的头指针向量([0]不用)
cd=(char*)malloc(n*sizeof(char)); // 分配求编码的工作空间
cd[n-1]='\0'; // 编码结束符
for(i=1;i<=n;i++)
{ // 逐个字符求赫夫曼编码
start=n-1; // 编码结束符位置
for(c=i,f=HT[i].parent;f!=0;c=f,f=HT[f].parent)
// 从叶子到根逆向求编码
if(HT[f].lchild==c)
cd[--start]='0';
else
cd[--start]='1';
HC[i]=(char*)malloc((n-start)*sizeof(char));
// 为第i个字符编码分配空间
strcpy(HC[i],&cd[start]); // 从cd复制编码(串)到HC
}
free(cd); // 释放工作空间
}
//----------------------初始化赫夫曼链表-------------------------
void Initialization()
{
flag=1;
int num2;
cout<<"下面初始化赫夫曼链表"<<endl;
w=(int*)malloc(n*sizeof(int)); // 为第26个字符权值分配空间
z=(char*)malloc(n*sizeof(char)); // 为第26个字符分配空间
cout<<"\n依次显示"<<n<<"个字符与其权值和编码\n"<<endl;
char base[2];//?
ifstream fin("abc.txt");
for(i=0;i<n;i++)
{
fin>>base;
*(z+i)=*base;//?
fin>>num2;//上面123行
*(w+i)=num2;
}
HuffmanCoding(HT,HC,w,n);
//----------------------------------打印编码---------------------------------------
cout<<"字符"<<setw(6)<<"权值"<<setw(11)<<"编码"<<endl;
for(i=1;i<=n;i++)
{
cout<<setw(3)<<*(z+i-1);
cout<<setw(6)<<*(w+i-1)<<setw(12)<<HC[i]<<endl;
}
//--------------------------将赫夫曼编码写入文件----------------------------
cout<<"下面将赫夫曼编码写入文件"<<endl<<"...................."<<endl;
FILE *htmTree;
char r[]={' ','\0'};
if((htmTree=fopen("htmTree.txt","w"))==NULL)
{
cout<<"不能打开文件 "<<endl;
return;
}
for(i=0;i<n;i++)
{
fputc(*(z+i),htmTree);
fputs(r,htmTree);
}
for(i=0;i<n;i++)
{
fprintf(htmTree,"%6d",*(w+i));
fputs(r,htmTree);
}
for(i=1;i<=n;i++)
{
fputs(HC[i],htmTree);
fputs(r,htmTree);
}
fclose(htmTree);
cout<<"已将字符与对应编码写入根目录下文件htmTree.txt中"<<endl<<endl;
}
//--------------------------获取报文并写入文件---------------------------
void InputCode()
{
FILE *tobetran;
char str[100];
if((tobetran=fopen("tobetran.txt","w"))==NULL)
{
cout<<"不能打开文件"<<endl;
return;
}
cout<<"请输入你想要编码的字符"<<endl; //字符个数应当小于100
gets(str);
fputs(str,tobetran);
cout<<"获取报文成功"<<endl;
fclose(tobetran);
cout<<"...................."<<endl<"报文存入根目录下的tobetran.txt文件中"<<endl;
}
//---------------------------------编码函数---------------------------------
void Encoding()
{
cout<<"下面对目录下文件tobetran.txt中的字符进行编码"<<endl;
FILE *tobetran,*codefile;
if((tobetran=fopen("tobetran.txt","rb"))==NULL)
{
cout<<"不能打开文件"<<endl;
}
if((codefile=fopen("codefile.txt","wb"))==NULL)
{
cout<<"不能打开文件"<<endl;
}
char *tran;
i=99;
tran=(char*)malloc(100*sizeof(char));
while(i==99)
{
if(fgets(tran,100,tobetran)==NULL)
{
cout<<"不能打开文件"<<endl;
break;
}
for(i=0;*(tran+i)!='\0';i++)
{
for(j=0;j<=n;j++)
{
if(*(z+j-1)==*(tran+i))
{
fputs(HC[j],codefile);
if(j>n)
{
cout<<"字符错误,无法编码!"<<endl;
break;
}
}
}
}
}
cout<<"…………编码完成…………"<<endl;
cout<<"编码写入目录下的codefile.txt中"<<endl<<endl;
fclose(tobetran);
fclose(codefile);
free(tran);
}
//-------------------------译码函数---------------------------
void Decoding()
{
cout<<"下面对根目录下文件codefile.txt中的字符进行译码"<<endl;
FILE *codef,*txtfile;
if((txtfile=fopen("\\Textfile.txt","w"))==NULL)
{
cout<<"不能打开文件"<<endl;
}
txtfile=fopen("Textfile.txt","w");
if ((codef=fopen("codefile.txt","r"))==NULL)
{
cout<<"不能打开文件"<<endl;
}
codef=fopen("codefile.txt","r");
char *work,*work2,i2;
int i4=0,i,i3;
unsigned long length=10000;
work=(char*)malloc(length*sizeof(char));
fgets(work,length,codef);
work2=(char*)malloc(length*sizeof(char));
i3=2*n-1;
for(i=0;*(work+i-1)!='\0';i++)
{
i2=*(work+i);
if(HT[i3].lchild==0)
{
*(work2+i4)=*(z+i3-1);
i4++;
i3=2*n-1;
i--;
}
else if(i2=='0') i3=HT[i3].lchild;
else if(i2=='1') i3=HT[i3].rchild;
}
*(work2+i4)='\0';
fputs(work2,txtfile);
cout<<"…………译码完成…………"<<endl;
cout<<"内容写入根目录下的文件textfile.txt中"<<endl<<endl;
free(work); //释放工作区
free(work2); //释放工作区
fclose(txtfile); //关闭文件txtfile.txt
fclose(codef); //关闭文件codef.txt
}
//-----------------------打印编码的函数----------------------
void Code_printing()
{
cout<<"下面打印根目录下文件CodePrin.txt中编码字符"<<endl;
FILE * CodePrin,* codefile;
if((CodePrin=fopen("CodePrin.txt","w"))==NULL)
{
cout<<"不能打开文件"<<endl;
return;
}
if((codefile=fopen("codefile.txt","r"))==NULL)
{
cout<<"不能打开文件"<<endl;
return;
}
char *work3;
work3=(char*)malloc(51*sizeof(char));
if(fgets(work3,51,codefile)==NULL)
{
cout<<"不能读取文件"<<endl;
}
else
do
{
fputs(work3,CodePrin);
puts(work3);
}while(strlen(work3)==50&&fgets(work3,51,codefile)!=NULL);
free(work3);
cout<<"打印结束"<<endl<<endl;
fclose(CodePrin);
fclose(codefile);
}
//------------------------打印赫夫曼树的函数-----------------------
void coprint(HuffmanTree start,HuffmanTree HT) //start=ht+26这是一个递归算法
{
if(start!=HT)
{
FILE * TreePrint;
if((TreePrint=fopen("TreePrint.txt","a"))==NULL)
{
cout<<"创建文件失败"<<endl;
return;
}
numb++; //number=0 该变量为已被声明为全局变量
coprint(HT+start->rchild,HT); //递归先序遍历
cout<<setw(5*numb)<<start->weight<<endl;
fprintf(TreePrint,"%d\n",start->weight);
coprint(HT+start->lchild,HT);
numb--;
fclose(TreePrint);
}
}
void Tree_printing(HuffmanTree HT,int w)
{
HuffmanTree p;
p=HT+w; //p=HT+26
cout<<"下面打印赫夫曼树"<<endl;
coprint(p,HT); //p=HT+26
cout<<"打印工作结束"<<endl;
}
//----------------------------------主函数-------------------------------------
void main()
{
cout<<endl;
cout<<" 此程序经晓光修改 "<<endl;
cout<<" 实现赫夫曼编码解码功能 "<<endl;
char choice;
while(choice!='q')
{ cout<<"\n******************************"<<endl;
cout<<" 赫夫曼编码解码 "<<endl;
cout<<"****************************** "<<endl;
cout<<"(i)初始化赫夫曼表 "<<endl;
cout<<"(w)输入待编码的字符 "<<endl;
cout<<"(e)进行编码、译码、打印编码 "<<endl;
cout<<"(t)打印赫夫曼树 "<<endl;
cout<<"(q)离开 "<<endl;
if(flag==0)
{
cout<<"\n请先初始化赫夫曼链表,输入'i'"<<endl;
cout<<"(程序将从根目录下的abc.txt文件中读出26个
字母及其权值并对字母进行编码)"<<endl;
}
cin>>choice;
switch(choice)
{
case 'i':
Initialization();//初始化赫夫曼表
break;
case 'w':
InputCode(); //输入待编码的字符
break;
case 'e':
Encoding();//进行编码
Decoding();//进行译码
Code_printing();//打印编码
break;
case 't':
Tree_printing(HT,2*n-1);//打印26个字母权值形成的哈夫曼树
break;
case 'q': //退出程序
break;
default:
cout<<"输入命令错误 "<<endl;
}
}
free(z);//释放字母所占内存空间
free(w);//释放权值所占内存空间
free(HT); //释放HT结构体所占内存空间
}
//代码结束code over
--------------------Configuration: luo - Win32 Debug--------------------
Compiling...
luo.cpp
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(174) : error C2296: '<<' : illegal, left operand has type 'char [37]'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(174) : error C2297: '<<' : illegal, right operand has type 'class ostream &(__cdecl *)(class ostream &)'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(220) : error C2018: unknown character '0xa3'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(220) : error C2018: unknown character '0xbb'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(221) : error C2146: syntax error : missing ';' before identifier 'cout'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(265) : error C2018: unknown character '0xa3'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(265) : error C2018: unknown character '0xbb'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(266) : error C2146: syntax error : missing ';' before identifier 'cout'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(357) : error C2001: newline in constant
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(358) : error C2018: unknown character '0xd7'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(358) : error C2018: unknown character '0xd6'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(358) : error C2018: unknown character '0xc4'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(358) : error C2018: unknown character '0xb8'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(358) : error C2018: unknown character '0xbc'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(358) : error C2018: unknown character '0xb0'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(358) : error C2018: unknown character '0xc6'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(358) : error C2018: unknown character '0xe4'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(358) : error C2018: unknown character '0xc8'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(358) : error C2018: unknown character '0xa8'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(358) : error C2018: unknown character '0xd6'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(358) : error C2018: unknown character '0xb5'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(358) : error C2018: unknown character '0xb2'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(358) : error C2018: unknown character '0xa2'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(358) : error C2018: unknown character '0xb6'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(358) : error C2018: unknown character '0xd4'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(358) : error C2018: unknown character '0xd7'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(358) : error C2018: unknown character '0xd6'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(358) : error C2018: unknown character '0xc4'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(358) : error C2018: unknown character '0xb8'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(358) : error C2018: unknown character '0xbd'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(358) : error C2018: unknown character '0xf8'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(358) : error C2018: unknown character '0xd0'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(358) : error C2018: unknown character '0xd0'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(358) : error C2018: unknown character '0xb1'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(358) : error C2018: unknown character '0xe0'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(358) : error C2018: unknown character '0xc2'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(358) : error C2018: unknown character '0xeb'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(358) : error C2059: syntax error : ')'
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(358) : error C2001: newline in constant
E:\Microsoft Visual Studio\MyProjects\luo\luo.cpp(359) : error C2143: syntax error : missing ';' before '}'
执行 cl.exe 时出错.
luo.exe - 1 error(s), 0 warning(s)