metode kompresi data huffman ( huffman compression method ) C++
#include <iostream.h>
#include <conio.h>
#include <string>
struct digits
{
char chrs;
int freq;
}nodex[50];
int itungfreq (char* pstrBuf, char chAscii)
{
int iCount = 0;
int iLength = strlen (pstrBuf);
for (int i=0; i<iLength; i++) {
if (pstrBuf [i] == chAscii) {
iCount++;
}
}
return iCount;
}
void encode(int jumlahdatapreorder, int jumlahdatainorder)
{
int jumdt;
string kanan;
jumdt=jumlahdatainorder;
int jumdtx=jumdt-2;
string *bin = new string[jumlahdatainorder];
string *binx = new string[jumlahdatainorder];
for(int i=0;i<jumdtx;i++)
{
kanan=kanan+"1";
bin[(jumdtx-1)-i]=kanan;
}
for(int i=0;i<jumlahdatainorder;i++)
{
if(i==0)
{
binx[i]=bin[i]+"0";
}
else if(i==1)
{
binx[i]=bin[0]+"1";
}
else
{
binx[i]=bin[i-1]+"0";
}
cout<<nodex[i+1].chrs<<" = "<<binx[i]<<" ["<<binx[i].length()<<" bit]"<<"\n";
}
cout<<endl;
delete[] bin;
delete[] binx;
}
void tabelfrekwensi(char kalimat[50])
{
int panjang,jumlahchar,sama,jumlahtidaksama=0;
panjang=strlen(kalimat);
cout<<"TABEL FREKWENSI "<<kalimat<<"\nTotal jumlah karakter : " <<panjang <<"\n\n";
char a1;
for (int i=0;i<panjang;i++)
{
sama=0;
a1=kalimat[i];
jumlahchar=itungfreq(kalimat,a1); //menghitung frekwensi
if(i>0)
{
sama=0;
for(int a=0;a<i;a++) //loop periksa duplikasi
{
if(a1==kalimat[a])
{
sama=1;
break;
}
}
}
if (sama==0)
{
nodex[jumlahtidaksama].chrs=a1;
nodex[jumlahtidaksama].freq=jumlahchar;
jumlahtidaksama++;
}
}
cout<<endl;
if(jumlahtidaksama>1)
{
int temp;
char tempc;
int b;
for(b=0;b<jumlahtidaksama;b++) //sort by asc
for(int c=0;c<(jumlahtidaksama-b);c++)
if(nodex[c].chrs>nodex[c+1].chrs)
{
temp=nodex[c].freq;
tempc=nodex[c].chrs;
nodex[c].freq=nodex[c+1].freq;
nodex[c].chrs=nodex[c+1].chrs;
nodex[c+1].freq=temp;
nodex[c+1].chrs=tempc;
}
for(int b=0;b<jumlahtidaksama;b++) //sort by freq
for(int c=0;c<(jumlahtidaksama-b);c++)
if(nodex[c].freq>nodex[c+1].freq)
{
temp=nodex[c].freq;
tempc=nodex[c].chrs;
nodex[c].freq=nodex[c+1].freq;
nodex[c].chrs=nodex[c+1].chrs;
nodex[c+1].freq=temp;
nodex[c+1].chrs=tempc;
}
for(int d=1;d<(jumlahtidaksama+1);d++)
{
cout<<"jumlah " <<nodex[d].chrs<<" = "<<nodex[d].freq<<"/" <<panjang<<"\n";
}
cout<<endl;
}
cout<<"Tekan sembarang tombol untuk mencetak Tabel Encode...";
getch();
cout<<"\n\n";
encode(panjang,jumlahtidaksama);
}
int main()
{
int a;
char b;
char kata[50];
awal:
clrscr();
cout<<"=======================================\n";
cout<<" PROGRAM POHON BINER HUFFMAN\n";
cout<<" dibuat oleh : R.Radian Baratasena\n";
cout<<" NRP : 14511069\n";
cout<<"=======================================\n\n";
cout<<"Masukkan sembarang kalimat [2 - 40 characters] : ";
cin.sync();
cin.getline(kata,50);
if(strlen(kata)<2|| strlen(kata)>40)
goto awal;
char *kata1=strupr(kata);
cout<<"\n\n";
clrscr();
cout<<"Pohon Biner Huffman dari kalimat " << kata1 << "\n\n";
cout<<"Tekan sembarang tombol untuk mencetak Tabel Frekwensi...";
getch();
clrscr();
cin.clear();
tabelfrekwensi(kata1);
cout<<"Hitung lagi? [y/n]: ";
cin>>b;
if (b!='n')
goto awal;
return 0;
}
Comments
Post a Comment