Académique Documents
Professionnel Documents
Culture Documents
FileIn.get(ch);
if (FileIn.eof())
break;
freq[ch]++;
}
FileIn.close();
return n;
}
void Compress::heapify(PQ *p, int i)
{
int l, r, smallest;
treenode *t;
l = left_child (i);
r = right_child (i);
if (l < p->heap_size && p->A[l]->freq < p->A[i]->freq)
smallest = l;
else
smallest = i;
if (r < p->heap_size && p->A[r]->freq < p->A[smallest]->freq)
smallest = r;
if (smallest != i)
{
t = p->A[i];
p->A[i] = p->A[smallest];
p->A[smallest] = t;
this->heapify (p, smallest);
}
}
void Compress::insert_pq(PQ *p, treenode *r)
{
int i;
p->heap_size++;
i = p->heap_size - 1;
while ((i > 0) && (p->A[parent(i)]->freq > r->freq))
{
p->A[i] = p->A[parent(i)];
i = parent (i);
}
p->A[i] = r;
}
fstream FileIn(NameFileIn,ios::in|ios::binary);
fstream FileOut(NameFileOut,ios::out|ios::binary);
FileOut.write((char*)(&n),sizeof(n));
for(int j=0;j<NUM_CHAR;++j)
if(freq[j])
{
FileOut.put((unsigned char)j);
FileOut.write((char *)(&freq[j]),sizeof(int));
}
for(;;)
{
FileIn.get(ch);
if(FileIn.eof())
break;
for (s=codes[ch]; *s; s++)
this->bitout (FileOut, *s);
}
n = nbits;
while(nbits)
this->bitout(FileOut,'0');
FileOut.put(char(n));
FileIn.close();
FileOut.close();
}
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)
{
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(int 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();
}
void main()
{
clrscr();
textcolor(LIGHTMAGENTA);
textbackground(GREEN);
/
*######################################################################
################*/
clrscr();
printf("\n");
printf("
TRUONG DAI HOC TAY DO
\n");
printf("
KHOA KY THUAT CONG NGHE
\n");
printf("=====================NIEN LUAN 1 TIN
HOC======================\n");
printf("
\n");
printf("**************************************************************\n"
);
printf("
|
\n");
printf(" De Tai |
NEN VA GIAI NEN BANG HUFFMAN
\n");
printf("
|
\n");
printf("**************************************************************\n"
);
printf("
\n");
printf("
\n");
printf("
\n");
printf(" Giao Vien Huong Dan
|
Sinh Vien Thuc Hien \n");
printf("
|
\n");
printf(" NGUYEN CHI CUONG
|
BUI THANH DIEN
\n");
printf(" MSCB:
|
MSSV:0851010013
\n");
printf("
|
NGUYEN THI DA THAO \n");
printf("
|
MSSV:0851010062
\n");
printf("
|
LOP: DH-TinHoc-K3 \n");
printf("
\n");
printf("
\n");
printf(" Can Tho, thang 03 nam 2011
\n");
printf("
\n");
printf("\n");
printf("
\n");
printf("
Bam Enter de tiep tuc!
\n");
printf("
\n");
printf("\n");
/
*######################################################################
#######################*/
getch();
//Dinh nghia cac bien can thiet
Compress C;
Decompress D;
char * s;
char *s1 = "D:\\Compress.huf";
char *s2 = "D:\\Decompress.doc";
char *codes[NUM_CHAR];
char code[NUM_CHAR];
system("cls");
//Nhap vao ten file can nen
cout<<"\n======ALGORITHM HUFFMAN======\n";
cout<<"\n BAN HAY NHAP CHINH XAC DUONG DAN DAN TOI TEN TEP:
";
cin>>s;
//Nen va giai nen
C.get_frequencies(s);
treenode *r= C.build_huffman();
C.traverse(r,0,code,codes);
C.encode_file(s,s1,codes);
D.decode_file(s1,s2);
cout<<"\nCAC FILE NEN VA GIAI NEN ";
cout<<"\n TEN FILE NEN VA VI TRI LUU TRU CUA PHAI NEN ";cin>>s1;
cout<<"TEN FILE GIAI NEN VA VI TRI LUU TRU CUA FILE GIA NEN ";cin>>s2;
cout<<"\n BAM ENTER DE THOAT KHOI CHUONG TRINH";
getch();
}