【估值空间源码】【2016支付宝秒余额源码】【大话企业级android应用开发实战源码】lzw源码
1.急求LZW算法源代码!!!
2.请问有没有LZW压缩算法的源代码?
3.跪求C语言进行哈夫曼编码、算术编码和LZW编码,要求源程序要有注释。估值空间源码
急求LZW算法源代码!!!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>//用来计算压缩的时间
using namespace std;
//定义常数
const int MAX = ;//最大code数,是2016支付宝秒余额源码一个素数,求模是大话企业级android应用开发实战源码速度比较快
const int ascii = ; //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;
}
请问有没有LZW压缩算法的源代码?
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define PATHSIZE
#define BITS
#define HashShift BITS-8
#define MAX_VALUE ((1<<BITS)-1)
#define MAX_CODE (MAX_VALUE-1)
#define HashTableLen
#define process
/*压缩结构*/
typedef struct
{
int *code;
unsigned int *prenum;
unsigned char *baknum;
}LZW_DATA;
unsigned char decode_stack[HashTableLen];
LZW_DATA lzwt,*lzw;
void dataout(FILE *output,unsigned int code);/*输出压缩后的流*/
unsigned int hashfind(unsigned int hash_prenum,unsigned int hash_character);/*压缩算法*/
char *decode(unsigned char *buffer,unsigned int code);
unsigned int incode(FILE *input);
void compress(FILE *input,FILE *output);
void decompress(FILE *input,FILE *output);
int main()
{
FILE *fp1,*fp2;
char path[PATHSIZE];
S1:puts("Input function.(h:hoop,u:uncompress)...");
fflush(stdin);
gets(path);
if(!strcmp(path,"h")||!strcmp(path,"hoop"))
{
printf("Input source file path:");
fflush(stdin);
gets(path);
if((fp1=fopen(path,"rb"))==NULL)
{
puts("Can not open source file!");
return 1;
}
printf("Input objective file path:");
fflush(stdin);
gets(path);
if((fp2=fopen(path,"wb"))==NULL)
{
puts("Can not open objective file!");
return 1;
}
compress(fp1,fp2);
}
else if(!strcmp(path,"u")||!strcmp(path,"uncompress"))
{
printf("Input source file path:");
fflush(stdin);
gets(path);
if((fp1=fopen(path,"rb"))==NULL)
{
puts("Can not open source file!");
return 1;
}
printf("Input objective file path:");
fflush(stdin);
gets(path);
if((fp2=fopen(path,"wb"))==NULL)
{
puts("Can not open objective file!");
return 1;
}
decompress(fp1,fp2);
}
else
{
puts("Input error,please input again!");
goto S1;
}
fclose(fp1);
fclose(fp2);
S2:puts("If continue or not(y:yes/n:no)?");
fflush(stdin);
gets(path);
if(!strcmp(path,"y")||!strcmp(path,"yes"))
{
goto S1;
}
else if(!strcmp(path,"n")||!strcmp(path,"no"))
{
goto S3;
}
else
{
puts("Input error,please input again!");
goto S2;
}
S3:return 0;
}
void compress(FILE *input,FILE *output)
{
int i,index,len1,len2;
int curr_code;
int baknum;
int prenum;
len1=HashTableLen*sizeof(unsigned int);
len2=HashTableLen*sizeof(unsigned char);
if(!(lzwt.code=(int*)malloc(len1)))
{
puts("Internal memory distribution error!");
exit(0);
}
if(!(lzwt.prenum=(unsigned int*)malloc(len1)))
{
puts("Internal memory distribution error!");
exit(0);
}
if(!(lzwt.baknum=(unsigned char*)malloc(len2)))
{
puts("Internal memory distribution error!");
exit(0);
}
lzw=&lzwt;
curr_code=;
for(i=0;i<HashTableLen;i++)
{
lzw->code[i]=-1;
}
i=0;
puts("Hoop begin.");
prenum=getc(input);
while((baknum=getc(input))!=EOF)
{
index=hashfind(prenum,baknum);
if(lzw->code[index]!=-1)
{
prenum=lzw->code[index];
}
else
{
if(curr_code<=(MAX_CODE))
{
lzw->code[index]=curr_code++;
lzw->prenum[index]=prenum;
lzw->baknum[index]=baknum;
}
dataout(output,prenum);
prenum=baknum;
}
if(i==prenum)
{
i=0;
putchar('.');
}
else
{
i++;
}
}
dataout(output,prenum);
dataout(output,MAX_VALUE);
dataout(output,0);
free(lzw->code);
free(lzw->prenum);
free(lzw->baknum);
}
unsigned int hashfind(unsigned int prenum,unsigned int baknum)
{
int index;
int offset;
index=(baknum<<HashShift)^prenum;
if(index==0)
{
offset=1;
}
else
{
offset=HashTableLen-index;
}
while(1)
{
if(lzw->code[index]==-1)
{
return index;
}
if(lzw->prenum[index]==prenum&&lzw->baknum[index]==baknum)
{
return index;
}
index-=offset;
if(index<0)
{
index+=HashTableLen;
}
}
}
void dataout(FILE *output,unsigned int code)
{
static int outbinary=0;
static unsigned long nob=0L;
nob|=(unsigned long)code<<(-BITS-outbinary);
outbinary+=BITS;
while(outbinary>=8)
{
putc(nob>>,output);
nob<<=8;
outbinary=outbinary-8;
}
}
void decompress(FILE *input,FILE *output)
{
unsigned int curr_code;
unsigned int baknum;
unsigned int prenum;
int i,ch,len1,len2;
char *ps;
len1=HashTableLen*sizeof(unsigned int);
len2=HashTableLen*sizeof(unsigned char);
if(!(lzwt.code=(int*)malloc(len1)))
{
puts("Internal memory distribution error!");
exit(0);
}
if(!(lzwt.prenum=(unsigned int*)malloc(len1)))
{
puts("Internal memory distribution error!");
exit(0);
}
if(!(lzwt.baknum=(unsigned char*)malloc(len2)))
{
puts("Internal memory distribution error!");
exit(0);
}
lzw=&lzwt;
curr_code=;
i=0;
puts("Uncompress begin.");
ch=prenum=incode(input);
putc(prenum,output);
while((baknum=incode(input))!=MAX_VALUE)
{
if(baknum>=curr_code)
{
*decode_stack=ch;
ps=decode(decode_stack+1,prenum);
}
else
{
ps=decode(decode_stack,prenum);
}
ch=*ps;
while(ps>=decode_stack)
{
putc(*(ps--),output);
}
if(curr_code<=MAX_CODE)
{
lzw->prenum[curr_code]=prenum;
lzw->baknum[curr_code]=ch;
curr_code++;
}
prenum=baknum;
if(i==process)
{
i=0;
putchar('.');
}
else
{
i++;
}
}
free(lzw->code);
free(lzw->prenum);
free(lzw->baknum);
}
char *decode(unsigned char *buffer,unsigned int code)
{
int len=0;
while(code>)
{
*buffer++=lzw->baknum[code];
code=lzw->prenum[code];
len++;
if(len>=HashTableLen)
{
puts("Internal memory run over!");
exit(1);
}
}
*buffer=code;
return buffer;
}
unsigned int incode(FILE *input)
{
unsigned int ret;
static int inputbinary=0;
static unsigned long nib=0L;
while(inputbinary<=)
{
nib|=(unsigned long)getc(input)<<(-inputbinary);
inputbinary=inputbinary+8;
}
ret=nib>>(-BITS);
nib<<=BITS;
inputbinary=inputbinary-BITS;
return ret;
}
这是我以前写的LZW算法,压缩和解压缩文本文件都没问题,就是解压后可能字符的顺序有点问题,呵呵用作学习的
我在这个地址回答过了,这样的复杂算法一般不会有个人去写的,我写了就觉得晕,如果提问的是同一个人,加我QQ我抽空教你原理。分布式java应用基础与实践源码
/question/.html?fr=im
跪求C语言进行哈夫曼编码、算术编码和LZW编码,二手车网站源码 php要求源程序要有注释。
以下是哈夫曼编码
#include<iostream>
#include<math.h>
#include<string>
#include<iomanip>
using namespace std;
int n;
int isin(string str,char a)
{
int temp=0;
for(int i=0;i<str.length();i++)
{
if(str[i]==a) temp=1;
}
return temp;
}
void bubble(double p[],string sign[])//排序
{
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(p[i]<p[j])
{
double temp=p[i];
p[i]=p[j];
p[j]=temp;
string m=sign[i];
sign[i]=sign[j];
sign[j]=m;
}
}
}
}
void huff(double tempp[],string tempstr[])
{
double p[][];
string sign[][];
sign[0][i]=tempstr[i]; //符号放在sign数组中
for(int i=0;i<n;i++)
{
p[0][i]=tempp[i]; //p数组放对应的概率(第1列中)
}
for(i=0;i<n-1;i++)
{
bubble(p[i],sign[i]); //第一次排序
for(int j=0;j<n-2-i;j++)
{
p[i+1][j]=p[i][j]; //前n-2-i个概率重新放在p数组中(是数组的第2列中)
sign[i+1][j]=sign[i][j];
}
p[i+1][j]=p[i][j]+p[i][j+1];//第一次两个最小概率求和
sign[i+1][j]=sign[i][j]+sign[i][j+1];//符号跟随
for(j=n-1-i;j<n;j++)
{
p[i+1][j]=0;
}
}
string final[];
for(i=n-2;i>=0;i--)
{
for(int k=0;k<n;k++)
{
if(isin(sign[i][n-2-i],sign[0][k][0])) final[k]+="0";
if(isin(sign[i][n-1-i],sign[0][k][0])) final[k]+="1";
}
}
cout<<setw(9)<<"哈弗曼编码如下:"<<endl;
for(i=0;i<n;i++)
{
cout<<setw(7)<<sign[0][i]<<setw(7)<<p[0][i]<<setw()<<final[i]<<
setw(7)<<final[i].length()<<endl;
}
}
void main()
{
char a[];
cout<<"该字符串符号为:";
cin>>a;
string s=a;
n=s.length();
char b[][2];
for(int i=0;i<n;i++)
{
b[i][0]=a[i];
b[i][1]='\0';
}
string str[];
for(i=0;i<n;i++)
{
str[i]=b[i];
}
double tempp[];
cout<<"字符概率依次为:";
for(i=0;i<n;i++)
{
cin>>tempp[i];
}
huff(tempp,str);
}