| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 944 人关注过本帖
标题:是一个有关LZW压缩的算法 编译通过但是运行出错。。高手看下
只看楼主 加入收藏
cryjack
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2007-12-30
收藏
 问题点数:0 回复次数:1 
是一个有关LZW压缩的算法 编译通过但是运行出错。。高手看下
#include<fstream.h>
#include<iostream.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
class element{
friend void Compress();
public:
    operator unsigned long()const{return key;}
    element& operator=(unsigned long y){key=y;return *this;}
public:
    int code;
    unsigned long key;
};

const D=4099,
      codes=4096,
      ByteSize=8,
      excess=4,
      alpha=256,
      mask1=255,
      mask2=15;
int LeftOver,status=0;

ifstream in;
ofstream out;
void Compress();
template <class T,class K>
class SortedChainNode{
//friend SortedChain<T>;
public:
    T data;
    SortedChainNode<T,K> *link;

};
template<class E, class K>
class SortedChain{
public:
    SortedChain(){first=0;}
    ~SortedChain(){};
    bool IsEmpty() const {return first == 0;}
    int Length() const;
    bool Search(const K& k,E& e)const;
    SortedChain<E,K>&Insert(const E& e);
    SortedChain<E,K>& DistinctInsert(const E& e);
        void Output(ostream& out) const;
private:
    SortedChainNode <E,K> *first;
};

template<class E, class K>
bool SortedChain<E,K>::Search(const K& k,E& e)const{
SortedChainNode<E,K> *p=first;
while(p&&p->data<k);
p = p->link;
if(p&&p->data==k){e=p->data;return 1;}
return false;
}


template<class E, class K>
SortedChain<E,K>& SortedChain<E,K>::Insert (const E& e)
{
 SortedChainNode<E,K> *p = first,
 *tp = 0;
 while (p && p->data < e) {
  tp = p;
  p = p->link;
 }

 SortedChainNode<E,K> *q = new SortedChainNode<E,K>;
 q->data = e;

 q->link = p;
 if (tp) tp->link = q;
 else first = q;
 return *this;
}


template<class E, class K>
SortedChain<E,K>& SortedChain<E,K>::DistinctInsert(const E& e){
SortedChainNode<E,K> *p=first,*tp=0;
while(p&&p->data<e);{tp=p;p=p->link;}
if(p&&p->data==e) return *this;
SortedChainNode<E,K> *q=new SortedChainNode<E,K>;
q->data=e;
q->link=p;
if(tp)tp->link=q;
else first=q;
return *this;
}

template<class E, class K>
class ChainHashTable{
public:
ChainHashTable(int divisor=11){D=divisor;
ht=new SortedChain<E,K> [D];}
~ChainHashTable(){delete []ht;}
bool Search(const K& k,E& e)const{
return ht[k%D].Search(k,e);}
ChainHashTable<E,K>& Insert(const E& e){
ht[e%D].DistinctInsert(e);
return *this;
}
void Output()const;
public:
    int D;
    SortedChain<E,K> *ht;

};

void SetFiles(int argc,char* argv[])
{//创建输入输出流
char OutputFile[50],InputFile[50];
//检查是否提供文件名
if(argc>=2)strcpy(InputFile,argv[1]);
else{//没有提供文件名:提示用户输入
cout<<"Enter name of file to compass"<<endl;
cout<<"File name should have no extension"<<endl;
cin>>InputFile;}

//文件名不应有扩展名
if(strchr(InputFile,'.')){
cerr<<"File name has extension"<<endl;
exit(1);}

//以二进制方式打开文件
in.open(InputFile,ios::binary);
if(in.fail()){cerr<<"Cannot open"<<InputFile<<endl;
exit(1);}
strcpy(OutputFile,InputFile);
strcat(OutputFile,".nz");
out.open(OutputFile,ios::binary);
}

void Output(ofstream out,unsigned long pcode)
{//输出8位,余下的位保存在LeftOver中
unsigned char c,d;
if(status){//余下4位
d=(unsigned char)pcode & mask1;
c=(unsigned char)((LeftOver<<excess)|(pcode>>ByteSize));
out.put(c);
out.put(d);
status=0;}
else{
LeftOver=pcode & mask2;
c=(unsigned char)(pcode>>excess);
out.put(c);
status=1;}
}

void Compress(){
ChainHashTable<element,unsigned long> h(D);
element e;
for(int i=0;i<alpha;i++){
e.key=i;
e.code=i;
h.Insert(e);}
int used=alpha;

unsigned char c;
in.get(c);
unsigned long pcode=c;
if(!in.eof()){
do{
in.get(c);
if(in.eof())break;
unsigned long k=(pcode<<ByteSize)+c;

if(h.Search(k,e))pcode=e.code;
else{
Output(out,pcode);
if(used<codes)
{
e.code=used++;
e.key=(pcode<<ByteSize)|c;
h.Insert(e);}
pcode=c;}
}while(1);
Output(out,pcode);
if(status){c=LeftOver<<excess;out.put(c);}
}
out.close();
in.close();
}



void main(int argc,char* argv[])
{
   SetFiles(argc,argv);
   Compress();
}
搜索更多相关主题的帖子: LZW 算法 编译 压缩 
2008-06-02 17:27
cdj_cjf
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2008-7-16
收藏
得分:0 
请看http://bbs.
2008-07-16 14:47
快速回复:是一个有关LZW压缩的算法 编译通过但是运行出错。。高手看下
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015011 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved