• 大小: 2KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-09
  • 语言: 其他
  • 标签: LZW  编码  

资源简介

压缩文件有两个源码文件LZWCode.cpp和LZWDecode.cpp可以实现LZW的编码与解码,并有简要的使用说明文档。

资源截图

代码片段和文件信息

#include
#include
#include
using namespace std;
class LZW{
public:
LZW();
void Code();
bool Find(string);
void Output(string);
void AddS(string);
void Print();
private:
int* code;//生成的字符串表中的编码
string* Str;//生成的字符串表中的字符
int* output;//原始字符串经压缩编码后生成的编码序列;
    string K;//需要压缩的字符串
int N;//需要压缩的字符串的长度
int L;//当前字符串表中的记录数目
int L1;//当前压缩的编码长度
string s;
    char c;
};
LZW::LZW ( )
{
ifstream fin;
fin.open (“code.txt“);
if(!fin)
cerr<<“文件打开失败!“< char temp;
fin.get(temp);
while(temp!=EOF)
{
K+=temp;
temp=EOF;
fin.get (temp);
}
fin.close ();
N=K.length ();

code=new int[N];
Str=new string[N];
output=new int[N];
L=0;
L1=0;
fin.clear ();

    fin.open (“init.txt“);
if(!fin)
cerr<<“文件打开失败!“< fin.get (temp);
while(temp!=EOF)
{
Str[L]=temp;
fin>>code[L];
fin.get(temp);
if(temp!=EOF)
{
while(temp==‘ ‘&&!fin.eof ())
{
fin.get (temp);
}
if(temp!=EOF)
{
temp=EOF;
fin.get (temp);
}
}
L++;
}

fin.close ();

Code();
Print();
cout<<“被压缩的字符个数为“< cout<<“压缩比为“<}
void LZW::Code ( )
{
cout<<“正在进行压缩........“< cout<<“被压缩的字符串为“< cout< int m=0;
s=K[m];
while(m+1 {
m++;
c=K[m];
if(Find(s+c))
{
s=s+c;
}
else
{
Output(s);
AddS(s+c);
s=c;
}
}
Output(s);
}

bool LZW::Find(string t)
{
int i;
for(i=0;i {
if(t==Str[i])
break;
}
if(i==L)
return false;
else
return true;
}
void LZW::Output (string t)
{
int i;
for(i=0;i {
if(t==Str[i])
break;
}

output[L1]=code[i];

L1++;
}
void LZW::AddS (string t)
{
Str[L]=t;
code[L]=L+1;
L++;
}
void LZW::Print() 
{
cout<<“压缩结果为“< ofstream fout;
fout.open(“yasuo.txt“);
for(int i=0;i {
cout< fout< }
cout< fout< cout<<“压缩结束!“<}

int main( )
{
LZW lzw;
return 0;
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       2251  2009-12-30 17:28  LZW的编码与解码\LZWCode.cpp

     文件       1798  2009-12-30 17:35  LZW的编码与解码\LZWDecode.cpp

     文件        520  2009-12-30 17:37  LZW的编码与解码\说明文档.txt

     目录          0  2009-12-30 17:36  LZW的编码与解码

----------- ---------  ---------- -----  ----

                 4569                    4


评论

共有 条评论