LZW算法的实现(part1)
使用语言:C++以及STL模板
LZW压缩算法的基本原理:提取原始文本文件数据中的不同字符,基于这些字符创建一个编译表,然后用编译表中的字符的索引来替代原始文本文件数据中的相应字符,减少原始数据大小。
// 解压缩
void LZW::Depression()
{
cout<<"Depression:"< string text;
std::vector<>::iterator iter_end ;
std::vector<>::iterator iter;
string s; CEntry entry;
for(int i=0;i<=t;i++)
{ int k=Codes[i];
//从字典中取出等于K的字符
iter = _dictionary->begin();
iter_end = _dictionary->end();
for(iter;iter!=iter_end;++iter)
{ if((*iter).GetCode()==k)
{ entry.SetEntry((*iter).GetEntry());
entry.SetCode((*iter).GetCode()); }
}
/*exception handler*/
if(entry.GetEntry().empty()==true) entry.SetEntry(s+s[0]);
cout << "i: " << i << "; s: " << s << "; k: " << k
<< " output = " << entry.GetEntry() ; cout< text=text+entry.GetEntry();
if(entry.GetEntry().empty()==false)
{ CEntry tmp_entry;
tmp_entry.SetCode( _dictionary->size()+1 );
tmp_entry.SetEntry(s+ entry.GetEntry()[0] );
_dictionary->push_back( tmp_entry );
}
s=entry.GetEntry();
}
cout< cout<<"The Text:"< }
....待续
LZW压缩算法的基本原理:提取原始文本文件数据中的不同字符,基于这些字符创建一个编译表,然后用编译表中的字符的索引来替代原始文本文件数据中的相应字符,减少原始数据大小。
// 解压缩
void LZW::Depression()
{
cout<<"Depression:"<
std::vector<>::iterator iter_end ;
std::vector<>::iterator iter;
string s; CEntry entry;
for(int i=0;i<=t;i++)
{ int k=Codes[i];
//从字典中取出等于K的字符
iter = _dictionary->begin();
iter_end = _dictionary->end();
for(iter;iter!=iter_end;++iter)
{ if((*iter).GetCode()==k)
{ entry.SetEntry((*iter).GetEntry());
entry.SetCode((*iter).GetCode()); }
}
/*exception handler*/
if(entry.GetEntry().empty()==true) entry.SetEntry(s+s[0]);
cout << "i: " << i << "; s: " << s << "; k: " << k
<< " output = " << entry.GetEntry() ; cout<
if(entry.GetEntry().empty()==false)
{ CEntry tmp_entry;
tmp_entry.SetCode( _dictionary->size()+1 );
tmp_entry.SetEntry(s+ entry.GetEntry()[0] );
_dictionary->push_back( tmp_entry );
}
s=entry.GetEntry();
}
cout<
....待续
Labels: 算法
Post a Comment

