主题:[讨论]求助:哈夫曼编码压缩文件问题
哈夫曼编码压缩和解压文件,已经把txt文件放在根目录下,编译没错但运行时会跳出“debug assertion failed”的错误提示框,程序如下,请高手指正。(虽然稍长,但错误应该好找吧。。。似乎是指针出错?)
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
string file[600000];
int ele_count=0;
float bite_count=0;
struct huffchar{
int count;
char data;
};
int count=1;
huffchar huff[1000];
bool char_judge(char c)
{
for(int i=1;i<=count;i++)
if(huff[i].data=c)
{
huff[i].count++;
return true;
}
return false;
}
void char_count(char c)
{
huff[count].data=c;
huff[count++].count++;
}
void read_file()
{
char c;
ifstream infile;
infile.open("d:\\daiyasuo.txt");
if(!infile)
{
cout<<"不能打开文件"<<endl;
exit(0);
}
cout<<"读入的文件中的内容为:"<<endl;
while((c=infile.get())!=EOF)
{
file[++ele_count]=c;
if(!char_judge(c))
char_count(c);
}
cout<<endl;
}
struct huff_tree{
int parent;
int lchild;
int rchild;
int weight;
};
int sum;//结点个数
huff_tree huffman[1000];
void create()
{
int min1,min2;
int loc1,loc2;//指向权值最小数的位置
for(int i=1;i<=sum;i++)
{
huffman[i].parent=0;
huffman[i].lchild=0;
huffman[i].rchild=0;
huffman[i].weight=0;
}
for(int m=1;m<count;m++)
huffman[m].weight=huff[m].count;
sum=2*count-1;
for(int j=count;j<=sum;j++)
{
loc1=loc2=0;
min1=min2=20000;
for(int k=1;k<j;k++)
if(huffman[k].parent==0)
if(huffman[k].weight<=min1)
{
min2=min1;
min1=huffman[k].weight;
loc2=loc1;
loc1=k;
}
else
if(huffman[k].weight<=min2)
{
min2=huffman[k].weight;
loc2=k;
}
huffman[loc1].parent=j;
huffman[loc2].parent=j;
huffman[j].lchild=loc1;
huffman[j].weight=huffman[loc1].weight+huffman[loc2].weight;
}
}
struct huff_code
{
string bits[100];
int count;
string c;
};
huff_code code[100];
void huffmancode()
{
int r,p;
int count1=0;
for(int y=1;y<=count;y++)
{
r=y;
code[y].c=huff[y].data;
p=huffman[y].parent;
while(p!=0)
{
if(huffman[p].lchild==r)
code[y].bits[++count1]='0';
else code[y].bits[++count1]='1';
r=p;
p=huffman[p].parent;
}
code[y].count=count1;
count1=0;
}
for(int t=1;t<=count;t++)
{
cout<<"字符"<<code[t].c<<" 编码: ";
int r=code[t].count;
while(r)
cout<<code[t].bits[r--];
cout<<endl;
}
}
string str;
void code_huffman_file()
{
ofstream fp;
cout<<"请输入文件名:"<<endl<<"例如:daiyasuo.txt"<<endl;
cout<<"该文件的压缩文件:"<<endl;
cin>>str;
fp.open(str.c_str());
if(!fp)
{
cout<<"不能打开"<<str<<"文件"<<endl;
exit(0);
}
for(int j=1;j<=ele_count;j++)
{
for(int i=1;i<=count;i++)
if(file[j]==code[i].c)
{
for(int k=code[i].count;k>0;k--)
{
fp<<code[i].bits[k];bite_count++;
}
break;
}
}
fp.close();
}
void code_file_out()
{
ifstream fp1;
ofstream fp2;
fp1.open(str.c_str());
if(!fp1)
{
cout<<"不能打开"<<str<<"文件"<<endl;
exit(0);
}
char inchar;
cout<<"请输入文件名:"<<endl<<"例如:huffman2.txt"<<endl;
cout<<"该文件解压后的文件:"<<endl;
string s1;
cin>>s1;
fp2.open(s1.c_str());
if(!fp2)
{
cout<<"不能打开"<<s1<<"文件"<<endl;
exit(0);
}
for(int ptr=sum;!fp1.eof();)
{
fp1>>inchar;
if(inchar=='1')
ptr=huffman[ptr].rchild;
else ptr=huffman[ptr].lchild;
if(huffman[ptr].lchild==0&&huffman[ptr].rchild==0)
{
fp2<<huff[ptr].data;
ptr=sum;
}
}
cout<<endl<<"请检查原文件"<<"daiyasuo.txt"<<"与解压文件"<<s1<<endl<<endl<<endl;
cout<<"--------------------------------请检查---------------------"<<endl;
}
void rate()
{
float v;
v=bite_count/8/ele_count*100;
cout<<"压缩文件的压缩比例为:"<<v<<"%"<<endl;
}
void main()
{
cout<<"*************数据结构课程设计**************"<<endl;
cout<<"------------------------〖Huffman树文件压缩与解压〗------------------------"<<endl;
system("pause");
read_file();
create();
huff_code();
code_file_out();
rate();
cout<<endl<<"文件的压缩与解压完成!"<<endl;
}
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
string file[600000];
int ele_count=0;
float bite_count=0;
struct huffchar{
int count;
char data;
};
int count=1;
huffchar huff[1000];
bool char_judge(char c)
{
for(int i=1;i<=count;i++)
if(huff[i].data=c)
{
huff[i].count++;
return true;
}
return false;
}
void char_count(char c)
{
huff[count].data=c;
huff[count++].count++;
}
void read_file()
{
char c;
ifstream infile;
infile.open("d:\\daiyasuo.txt");
if(!infile)
{
cout<<"不能打开文件"<<endl;
exit(0);
}
cout<<"读入的文件中的内容为:"<<endl;
while((c=infile.get())!=EOF)
{
file[++ele_count]=c;
if(!char_judge(c))
char_count(c);
}
cout<<endl;
}
struct huff_tree{
int parent;
int lchild;
int rchild;
int weight;
};
int sum;//结点个数
huff_tree huffman[1000];
void create()
{
int min1,min2;
int loc1,loc2;//指向权值最小数的位置
for(int i=1;i<=sum;i++)
{
huffman[i].parent=0;
huffman[i].lchild=0;
huffman[i].rchild=0;
huffman[i].weight=0;
}
for(int m=1;m<count;m++)
huffman[m].weight=huff[m].count;
sum=2*count-1;
for(int j=count;j<=sum;j++)
{
loc1=loc2=0;
min1=min2=20000;
for(int k=1;k<j;k++)
if(huffman[k].parent==0)
if(huffman[k].weight<=min1)
{
min2=min1;
min1=huffman[k].weight;
loc2=loc1;
loc1=k;
}
else
if(huffman[k].weight<=min2)
{
min2=huffman[k].weight;
loc2=k;
}
huffman[loc1].parent=j;
huffman[loc2].parent=j;
huffman[j].lchild=loc1;
huffman[j].weight=huffman[loc1].weight+huffman[loc2].weight;
}
}
struct huff_code
{
string bits[100];
int count;
string c;
};
huff_code code[100];
void huffmancode()
{
int r,p;
int count1=0;
for(int y=1;y<=count;y++)
{
r=y;
code[y].c=huff[y].data;
p=huffman[y].parent;
while(p!=0)
{
if(huffman[p].lchild==r)
code[y].bits[++count1]='0';
else code[y].bits[++count1]='1';
r=p;
p=huffman[p].parent;
}
code[y].count=count1;
count1=0;
}
for(int t=1;t<=count;t++)
{
cout<<"字符"<<code[t].c<<" 编码: ";
int r=code[t].count;
while(r)
cout<<code[t].bits[r--];
cout<<endl;
}
}
string str;
void code_huffman_file()
{
ofstream fp;
cout<<"请输入文件名:"<<endl<<"例如:daiyasuo.txt"<<endl;
cout<<"该文件的压缩文件:"<<endl;
cin>>str;
fp.open(str.c_str());
if(!fp)
{
cout<<"不能打开"<<str<<"文件"<<endl;
exit(0);
}
for(int j=1;j<=ele_count;j++)
{
for(int i=1;i<=count;i++)
if(file[j]==code[i].c)
{
for(int k=code[i].count;k>0;k--)
{
fp<<code[i].bits[k];bite_count++;
}
break;
}
}
fp.close();
}
void code_file_out()
{
ifstream fp1;
ofstream fp2;
fp1.open(str.c_str());
if(!fp1)
{
cout<<"不能打开"<<str<<"文件"<<endl;
exit(0);
}
char inchar;
cout<<"请输入文件名:"<<endl<<"例如:huffman2.txt"<<endl;
cout<<"该文件解压后的文件:"<<endl;
string s1;
cin>>s1;
fp2.open(s1.c_str());
if(!fp2)
{
cout<<"不能打开"<<s1<<"文件"<<endl;
exit(0);
}
for(int ptr=sum;!fp1.eof();)
{
fp1>>inchar;
if(inchar=='1')
ptr=huffman[ptr].rchild;
else ptr=huffman[ptr].lchild;
if(huffman[ptr].lchild==0&&huffman[ptr].rchild==0)
{
fp2<<huff[ptr].data;
ptr=sum;
}
}
cout<<endl<<"请检查原文件"<<"daiyasuo.txt"<<"与解压文件"<<s1<<endl<<endl<<endl;
cout<<"--------------------------------请检查---------------------"<<endl;
}
void rate()
{
float v;
v=bite_count/8/ele_count*100;
cout<<"压缩文件的压缩比例为:"<<v<<"%"<<endl;
}
void main()
{
cout<<"*************数据结构课程设计**************"<<endl;
cout<<"------------------------〖Huffman树文件压缩与解压〗------------------------"<<endl;
system("pause");
read_file();
create();
huff_code();
code_file_out();
rate();
cout<<endl<<"文件的压缩与解压完成!"<<endl;
}