📄 giainen.h
字号:
#include "nen.h"
class Decompress : public Compress
{
private:
bit_buffer buff;
public:
Decompress();
void convert_bin(unsigned char c);
void decode_file(char *FileIn, char *FileOut);
};
Decompress::Decompress()
{
for(int i=0;i<NUM_CHAR;++i)
freq[i] = 0;
}
void Decompress::convert_bin(unsigned char c)
{
int i;
for(i=0;i<NUM_BIT;++i)
buff.content[i] = '0';
buff.content[NUM_BIT]='\0';
i= NUM_BIT - 1;
while(i>=0)
{
buff.content[i]=(c%2)+'0';
c=c/2;
i--;
}
buff.post = 0;
}
void Decompress::decode_file(char *NameFileIn, char *NameFileOut)
{
int i;
char *codes[NUM_CHAR];
char code[NUM_CHAR];
unsigned char c; // luu tru byte cuoi cung
int n; // So ky tu co trong File truoc khi nen
fstream FileIn(NameFileIn,ios::in|ios::binary);
fstream FileOut(NameFileOut,ios::out|ios::binary);
FileIn.read((char*)(&n),sizeof(n));
for(int i=1;i<=n;++i)
{
FileIn.get(unsigned char(ch));
FileIn.read((char*)(&freq[ch]),sizeof(int));
}
int start = FileIn.tellg();
FileIn.seekp(-1,ios::end);
int full_bytes = FileIn.tellg() - start - 1;
FileIn.get(c);
FileIn.seekg(-full_bytes-2,ios::end);
treenode *r = this->build_huffman();
this->traverse (r, 0, code, codes);
treenode *t=r;
FileIn.get(ch);
this->convert_bin(ch);
for(i=1;i<=full_bytes;)
{
if(t->left==NULL&&t->right==NULL)
{
FileOut.put(t->ch);
t = r;
}
else
{
if(buff.post<NUM_BIT&&buff.content[buff.post]=='0')
t = t->left;
if(buff.post<NUM_BIT&&buff.content[buff.post]=='1')
t = t->right;
if(buff.post==NUM_BIT)
{
FileIn.get(ch);
this->convert_bin(ch);
++i;
buff.post = -1;
}
buff.post++;
}
}
int remain = (int(c)==0)?NUM_BIT:int(c);
for(;buff.post<=remain;)
{
if(t->left==NULL&&t->right==NULL)
{
FileOut.put(t->ch);
t = r;
}
else
{
if(buff.post<NUM_BIT&&buff.content[buff.post]=='0')
t = t->left;
if(buff.post<NUM_BIT&&buff.content[buff.post]=='1')
t = t->right;
buff.post++;
}
}
FileIn.close();
FileOut.close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -