اینم با تابع Decoder.
وجمله ای که میخواهید دیکد شود در textb.txt قرار داشته باشد.
اگر صفر و یک هایی که برای دیکد در فایل textb.txt میریزید نمادی از هیچ حرفی (با توجه به کد شدن در مرحله ی قبل) نباشند برنامه تک بوق میزند و دیکد کردن می ایستد.
همانطوری که قبلا گفتم این دو فایل در دایرکتوری خود برنامه ایجاد شوند.
کد:
//Written by Soheil setayeshi
//www.codecorona.com
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
//*******************************Datatype : Node
class node
{
public:
int num;
char ch;
node *right;
node *left;
node():num(0),ch(0),right(0),left(0){}
};
//*******************************Datatype : Code
class code
{
public:
char bcode[8];
char ch;
code(){for(int i=0;i<8;i++) bcode[i]=0;}
};
//*******************************Class : Huffman
class huffman
{
public:
huffman();
private:
void reader();
void lister();
void sort();
void create();
void binary(node*,char[],int);
void coder();
void calculate();
void display();
void decoder();
vector<char> sentence;//Sentence
vector<char> codes;//Coded letters
vector<node> list;//Letters with num
vector<node*> v;//Address of letters in list
code *dictionary;//Binary map
string btext;//Input binary
};
//*******************************Constractor
huffman :: huffman()
{
int i;
reader();
lister();
sort();
dictionary=new code[list.size()];
for (i=0;i<list.size();i++)
dictionary[i].ch=v[i]->ch;
calculate();
}
//*******************************Reader
void huffman :: reader ()
{
char ch;
ifstream in("textw.txt",ios::in);
while (in.get(ch))
sentence.push_back(ch);
in.close();
}
//*******************************Lister
void huffman :: lister ()
{
int i,j;
node temp;
temp.ch=sentence[0];
temp.num=1;
list.push_back(temp);
for (i=1;i<sentence.size();i++)
{
for (j=0;j<list.size();j++)
if (sentence[i]==list[j].ch)
{
list[j].num++;
break;
}
if (j==list.size())
{
temp.ch=sentence[i];
list.push_back(temp);
}
}
for (i=0;i<list.size();i++)
v.push_back(&list[i]);
}
//*******************************Sorter
void huffman :: sort()
{
for (int i=0;i<v.size()-1;i++)
for (int j=0;j<v.size()-i-1;j++)
if( v[j]->num > v[j+1]->num )
swap( v[j] , v[j+1]);
}
//*******************************Calculate
void huffman :: calculate()
{
char s[8]={0};
create();
binary(v[0],s,0);
coder();
display();
}
//*******************************Create
void huffman :: create()
{
if (v.size() > 1 )
{
node *p1=v[0],*p2=v[1],*p=new node;
p->right=p2;
p->left=p1;
p->num=p1->num + p2->num;
v.erase(v.begin());
v.erase(v.begin());
v.insert(v.begin(),p);
sort();
create();
}
else return;
}
//*******************************Binary
void huffman :: binary(node* n,char byte[],int i)//n: First address
{ //byte[]: Binary code place
//i: Present place of pointer
if ( ! n->ch )
{
char byte1[8]={0},byte2[8]={0};//1:right 2:left
strcpy(byte1,byte);
strcpy(byte2,byte);
byte1[i]='1';
byte2[i]='0';
binary(n->right,byte1,i+1);
binary(n->left,byte2,i+1);
}
else
{
for (int j=0;j<list.size();j++)
if (dictionary[j].ch == n->ch)
{
strcpy(dictionary[j].bcode,byte);
return;
}
}
}
//*******************************Display
void huffman :: display()
{
int i;
cout<<"Input sentence is: ";
for (i=0;i<sentence.size();i++)
cout<<sentence[i];
cout<<"\n\n";
for (i=0;i<list.size();i++)
cout<<list[i].ch<<':'<<list[i].num<<' ';
cout<<"\n\n";
for (i=0;i<list.size();i++)
cout<<dictionary[i].ch<<" -> "<<dictionary[i].bcode<<endl;
cout<<endl;
cout<<"\nThe Huffman code of input words is(from textw.txt):\n";
for (i=0;i<codes.size();i++)
cout<<codes[i];
cout<<"\n\n";
decoder();
cout<<endl;
}
//*******************************Coder
void huffman :: coder()
{
for (int i=0;i<sentence.size();i++)
for (int j=0;j<list.size();j++)
if (dictionary[j].ch == sentence[i])
{
for (int k=0 ; dictionary[j].bcode[k] ; k++)
codes.push_back(dictionary[j].bcode[k]);
break;
}
}
//*******************************Decoder
void huffman :: decoder()
{
ifstream in("textb.txt",ios::in);
in>>btext;
cout<<"Input binaries is(from textb.txt):\n";
cout<<btext<<endl;
cout<<"\nThe Huffman decode of input binaries is: ";
int l,i,j,k,length;
for (l=0;l<btext.size();)
{
for (i=list.size()-1 ; i>=0 ;i--)
{
length=strlen(dictionary[i].bcode);
char *temp=new char[length];
for (k=l,j=0;k<l+length;k++,j++)
temp[j]=btext[k];
if (strstr(temp,dictionary[i].bcode))
{
cout<<dictionary[i].ch;
l+=length;
break;
}
if (i == 0)//end of dictionanry
{
cout<<"\n\aOut of dictionaty!!\n";
system("pause");
return;
}
}
}
cout<<endl;
}
//*******************************Main
int main()
{
huffman ob;
system("pause");
return 0;
}
//*******************************End