📄 lzw.cpp
字号:
#include "LZW.h"
#include <iostream>
using std::cout;
using std::endl;
#include <algorithm>
// 构造函数
LZW::LZW( string source ){
_source = source;
_code = "";
_dictionary = new vector< CEntry >;
CEntry entry;
char tmp;
string tmp_E;
for(int i=0;i<256;++i){
entry.SetCode( i );
tmp_E=tmp;
entry.SetEntry( tmp_E );
_dictionary->push_back( entry );
++tmp;
}
_initLength = _dictionary->size();
}
// 析构函数
LZW::~LZW(){
if( _dictionary ){
delete _dictionary;
}
}
// 压缩
void LZW::Compression(){
int code = _initLength;
std::vector< CEntry >::iterator begin = _dictionary->begin();
string s;
s.assign( _source, 0, 1 );
t=0;//my
for( int i = 1; i <= _source.size() - 1; i++ ){
string c;
c.assign( _source, i, 1 );
cout << "i: " << i << "; s: " << s << "; c: " << c
<< " s + c = " << s + c ;
std::vector< CEntry >::iterator iter;
CEntry entry;
entry.SetEntry( s + c );
iter = std::find( _dictionary->begin(), _dictionary->end(), entry );
if( iter != _dictionary->end() ){
// s + c在词典中
cout << "在词典中" ;
s += c;
}
else{
cout << "不在词典中,输出 ";
std::vector< CEntry >::iterator iterS;
CEntry entry;
entry.SetEntry( s );
iterS = std::find( _dictionary->begin(), _dictionary->end(), entry );
CEntry entryS = ( CEntry )( *iterS );
cout << entryS.GetCode() << " " ;
//保存编码值
Codes[t]=entryS.GetCode();
t++;
code++;
entry.SetCode( code );
entry.SetEntry( s + c );
_dictionary->push_back( entry );
s = c;
}
cout<<endl ;
}
CEntry entry;
entry.SetEntry( s );
std::vector< CEntry >::iterator iterS;
iterS = std::find( _dictionary->begin(), _dictionary->end(), entry );
CEntry entryS = ( CEntry )( *iterS );
cout << entryS.GetCode() << " ";
//保存编码值
Codes[t]=entryS.GetCode();
std::cout << std::endl;
}
void LZW::ShowCodes(){
cout<<"The Codes:";
for(int i=0;i<=t;i++)
cout<<Codes[i];
cout<<endl<<endl;
}
// 解压缩
void LZW::Depression(){
cout<<"Depression:"<<endl;
string text;
std::vector< CEntry >::iterator iter_end ;
std::vector< CEntry >::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<<endl;
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( entry );
}
s=entry.GetEntry();
}
cout<<endl;
cout<<"The Text:"<<text<<endl;
}
// 设置原始字符串
void LZW::SetSource( string source ){
_source = source;
}
string LZW::GetSource(){
return _source;
}
// 字典
void LZW::SetDictionary( vector< CEntry > dictionary ){
if( _dictionary->empty() == false ){
_dictionary->erase( _dictionary->begin(), _dictionary->end() );
}
std::vector< CEntry >::iterator iter;
std::vector< CEntry >::iterator begin = dictionary.begin();
std::vector< CEntry >::iterator end = dictionary.end();
for( iter = begin; iter <= end - 1; iter++ ){
_dictionary->push_back( ( CEntry )*iter );
}
}
vector< CEntry > * LZW::GetDictionary(){
return _dictionary;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -