急求lzw算法的英文文本压缩C语言源代码!
发布网友
发布时间:2022-05-06 12:12
我来回答
共4个回答
热心网友
时间:2022-06-30 12:07
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>//用来计算压缩的时间
using namespace std;
//定义常数
const int MAX = 1000003;//最大code数,是一个素数,求模是速度比较快
const int ascii = 256; //ascii代码的数量
const int ByteSize = 8; //8个字节
struct Element//hash表中的元素
{
int key;
int code;
Element *next;
}*table[MAX];//hash表
int hashfunction(int key)//hash函数
{
return key%MAX;
}
void hashinit(void)//hash表初始化
{
memset(table,0,sizeof(table));
}
void hashinsert(Element element)//hash表的插入
{
int k = hashfunction(element.key);
if(table[k]!=NULL)
{
Element *e=table[k];
while(e->next!=NULL)
{
e=e->next;
}
e->next=new Element;
e=e->next;
e->key = element.key;
e->code = element.code;
e->next = NULL;
}
else
{
table[k]=new Element;
table[k]->key = element.key;
table[k]->code = element.code;
table[k]->next = NULL;
}
}
bool hashfind(int key,Element &element)//hash表的查找
{
int k = hashfunction(key);
if(table[k]!=NULL)
{
Element *e=table[k];
while(e!=NULL)
{
if(e->key == key)
{
element.key = e->key;
element.code = e->code;
return true;
}
e=e->next;
}
return false;
}
else
{
return false;
}
}
void compress(void)//压缩程序
{
//打开一个流供写入
FILE *fp;
fp = fopen("result.dat", "wb");
Element element;
int used;
char c;
int pcode, k;
for(int i=0;i<ascii;i++)
{
element.key = i;
element.code = i;
hashinsert(element);
}
used = ascii;
c = getchar();
pcode = c;
while((c = getchar()) != EOF)
{
k = (pcode << ByteSize) + c;
if(hashfind(k, element))
pcode = element.code;
else
{
//cout<<pcode<<' ';
fwrite(&pcode, sizeof(pcode), 1, fp);
element.code = used++;
element.key = (pcode << ByteSize) | c;
hashinsert(element);
pcode = c;
}
}
//cout<<pcode<<endl;
fwrite(&pcode, sizeof(pcode), 1, fp);
}
int main(void)
{
int t1,t2;
//欲压缩的文本文件
//freopen("input.txt","r",stdin);
freopen("book5.txt","r",stdin);
t1=time(NULL);
hashinit();
compress();
t2=time(NULL);
cout<<"Compress complete! See result.dat."<<endl;
cout<<endl<<"Total use "<<t2-t1<<" seconds."<<endl;
热心网友
时间:2022-06-30 12:07
我也正需要一个,参考楼上的
热心网友
时间:2022-06-30 12:08
/*LZW就是通过建立一个字符串表,用较短的代码来表示较长的字符串来实现压缩*/#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>//用来计算压缩的时间
using
namespace
std;//定义常数
const
int
MAX
=
1000003;//最大code数,是一个素数,求模是速度比较快
const
int
ascii
=
256;
//ascii代码的数量
const
int
ByteSize
=
8;
//8个字节struct
Element//hash表中的元素
{
int
key;
int
code;
Element
*next;
}*table[MAX];//hash表int
hashfunction(int
key)//hash函数
{
return
key%MAX;
}
void
hashinit(void)//hash表初始化
{
memset(table,0,sizeof(table));
}
void
hashinsert(Element
element)//hash表的插入
{
int
k
=
hashfunction(element.key);
if(table[k]!=NULL)
{
Element
*e=table[k];
while(e->next!=NULL)
{
e=e->next;
}
e->next=new
Element;
e=e->next;
e->key
=
element.key;
e->code
=
element.code;
e->next
=
NULL;
}
else
{
table[k]=new
Element;
table[k]->key
=
element.key;
table[k]->code
=
element.code;
table[k]->next
=
NULL;
}
}
bool
hashfind(int
key,Element
&element)//hash表的查找
{
int
k
=
hashfunction(key);
if(table[k]!=NULL)
{
Element
*e=table[k];
while(e!=NULL)
{
if(e->key
==
key)
{
element.key
=
e->key;
element.code
=
e->code;
return
true;
}
e=e->next;
}
return
false;
}
else
{
return
false;
}
}
void
compress(void)//压缩程序
{
//打开一个流供写入
FILE
*fp;
fp
=
fopen("result.dat",
"wb");
Element
element;
int
used;
char
c;
int
pcode,
k;
for(int
i=0;i<ascii;i++)
{
element.key
=
i;
element.code
=
i;
hashinsert(element);
}
used
=
ascii;
c
=
getchar();
pcode
=
c;
while((c
=
getchar())
!=
EOF)
{
k
=
(pcode
<<
ByteSize)
+
c;
if(hashfind(k,
element))
pcode
=
element.code;
else
{
//cout<<pcode<<'
';
fwrite(&pcode,
sizeof(pcode),
1,
fp);
element.code
=
used++;
element.key
=
(pcode
<<
ByteSize)
|
c;
hashinsert(element);
pcode
=
c;
}
}
//cout<<pcode<<endl;
fwrite(&pcode,
sizeof(pcode),
1,
fp);
}
int
main(void)
{
int
t1,t2;
//欲压缩的文本文件
char
xx[100]={0};
printf("请输入要压缩的文件:");
gets(xx);
freopen(xx,"r",stdin);
t1=time(NULL);
hashinit();
compress();
t2=time(NULL);
cout<<"Compress
complete!
See
result.dat."<<endl;
cout<<endl<<"Total
use
"<<t2-t1<<"
seconds."<<endl;
}
热心网友
时间:2022-06-30 12:08
/*LZW就是通过建立一个字符串表,用较短的代码来表示较长的字符串来实现压缩*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>//用来计算压缩的时间
using
namespace
std;
//定义常数
const
int
MAX
=
1000003;//最大code数,是一个素数,求模是速度比较快
const
int
ascii
=
256;
//ascii代码的数量
const
int
ByteSize
=
8;
//8个字节
struct
Element//hash表中的元素
{
int
key;
int
code;
Element
*next;
}*table[MAX];//hash表
int
hashfunction(int
key)//hash函数
{
return
key%MAX;
}
void
hashinit(void)//hash表初始化
{
memset(table,0,sizeof(table));
}
void
hashinsert(Element
element)//hash表的插入
{
int
k
=
hashfunction(element.key);
if(table[k]!=NULL)
{
Element
*e=table[k];
while(e->next!=NULL)
{
e=e->next;
}
e->next=new
Element;
e=e->next;
e->key
=
element.key;
e->code
=
element.code;
e->next
=
NULL;
}
else
{
table[k]=new
Element;
table[k]->key
=
element.key;
table[k]->code
=
element.code;
table[k]->next
=
NULL;
}
}
bool
hashfind(int
key,Element
&element)//hash表的查找
{
int
k
=
hashfunction(key);
if(table[k]!=NULL)
{
Element
*e=table[k];
while(e!=NULL)
{
if(e->key
==
key)
{
element.key
=
e->key;
element.code
=
e->code;
return
true;
}
e=e->next;
}
return
false;
}
else
{
return
false;
}
}
void
compress(void)//压缩程序
{
//打开一个流供写入
FILE
*fp;
fp
=
fopen("result.dat",
"wb");
Element
element;
int
used;
char
c;
int
pcode,
k;
for(int
i=0;i<ascii;i++)
{
element.key
=
i;
element.code
=
i;
hashinsert(element);
}
used
=
ascii;
c
=
getchar();
pcode
=
c;
while((c
=
getchar())
!=
EOF)
{
k
=
(pcode
<<
ByteSize)
+
c;
if(hashfind(k,
element))
pcode
=
element.code;
else
{
//cout<<pcode<<'
';
fwrite(&pcode,
sizeof(pcode),
1,
fp);
element.code
=
used++;
element.key
=
(pcode
<<
ByteSize)
|
c;
hashinsert(element);
pcode
=
c;
}
}
//cout<<pcode<<endl;
fwrite(&pcode,
sizeof(pcode),
1,
fp);
}
int
main(void)
{
int
t1,t2;
//欲压缩的文本文件
char
xx[100]={0};
printf("请输入要压缩的文件:");
gets(xx);
freopen(xx,"r",stdin);
t1=time(NULL);
hashinit();
compress();
t2=time(NULL);
cout<<"Compress
complete!
See
result.dat."<<endl;
cout<<endl<<"Total
use
"<<t2-t1<<"
seconds."<<endl;
}