資源簡介
壓縮文件有兩個源碼文件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
- 上一篇:labview 用戶登錄程序
- 下一篇:IIC Linux 應用層的兩種操作方式
評論
共有 條評論