-
الگوریتم هافمن ++c
سورس کد الگوریتم هافمن فایل متنی را فشرده میکنه ولی مشکل در بازگرداند فایل فشرده متنی (کد گشای)که درست عمل نمیکنه
کد:
//*************************search******************************
int search(int i)
{
struct treenode *h;
h=start;
while(h)
{
if (h->data==i)
{
h->num=h->num+1;
return 0;
}
h=h->next;
}
return -1;
}
//**************************insert******************************
void insert(int i)
{
p=new treenode;
p->data=i;
p->num=1;
p->right=p->left=p->next=NULL;
if (start==NULL)
{
start=p;
end=p;
}
else
{
end->next=p;
end=p;
} }
//******************************Sort*******************
void sort()
{
struct treenode *h1,*h2,*n2,*right,*left;
int d,n;
n2=start;
while(n2)
{ h1=start;
h2=start->next;
while(h2)
{
if (h1->num > h2->num)
{
d=h1->data;
n=h1->num;
right=h1->right;
left=h1->left;
h1->data=h2->data;
h1->num=h2->num;
h1->right=h2->right;
h1->left=h2->left;
h2->data=d;
h2->num=n;
h2->right=right;
h2->left=left;
}
h1=h2;
h2=h2->next;
}
n2=n2->next;
} }
//****************Create*******************************
void create()
{
struct treenode *h;
while (start->next)
{
int i1,i2;
p=new treenode;
p->left=start;
p->right=start->next;
p->num=start->num+start->next->num;
p->data=-1;
p->next=NULL;
h=start;
start=start->next->next;
h->next->next=NULL;
h->next=NULL;
insertm(p);
sort();
}}
//********************Insertm****************************
void insertm(struct treenode *l)
{
if (start==NULL)
{
start=l;
end=l;
}
else
{
end->next=l;
end=l;
} }
//********************Binary****************************
void binary(struct treenode *n,char byte[],int i)
{
if (n->data==-1)
{
char b1[20]={0},b2[20]={0};
strcpy(b1,byte);
strcpy(b2,byte);
b1[i]='1';
binary(n->right,b1,i+1);
b2[i]='0';
binary(n->left,b2,i+1);
}
else
{
cout<<"Char :"<<(char)n->data<<" Byte : "<<byte<<"\n";
strcpy(coding[o].bcode,byte);
coding[o++].ch=n->data;
}
}
//*********************Coding*******************************
void write()
{
FILE *fp1,*fp2;
fp1=fopen(filename,"r+b");
fp2=fopen("Code.txt","w+b");
//**************************************************************
int i=1;
while (i<=numnode)
{
fprintf(fp2,"%d",atree[i].data);
i++;
}
//*****************************************************************
i=getc(fp1);
int j=0,place=1;
unsigned char bytebuf=0;
while (i!=-1)
{
j=searchc(i);
int y=0;
while (coding[j].bcode[y]) {
if (coding[j].bcode[y]=='1')
bytebuf |= place;
if (place==128)
{
putc(bytebuf,fp2);
place=1;
bytebuf=0;
}
else
place<<=1;
y++;
}
i=getc(fp1);
}
if (place<=128 && place !=1) putc(bytebuf,fp2);
fclose(fp1);
fclose(fp2);
}
//********************searchcode***************************
int searchc(int i)
{
int j=0;
while (j<o)
if (coding[j].ch==i)
return j;
else
j++;
}
//**********************Encode*****************************
void read()
{
FILE *fp1,*fp2;
fp1=fopen("Decode.txt","w+b");
fp2=fopen("Code.txt","r+b");
//************************************************
int i=1,a;
while (i<=numnode)
{
fscanf(fp2,"%d",&a);
i++;
}
//************************************************
i=getc(fp2);
int j=0,place=1;
struct treenode *n;
unsigned char bytebuf=0;
while (i!=-1)
{
n=start;
while (n->data==-1)
{
int x=(place & i)?1:0;
if (x==1)
n=n->right;
else
n=n->left;
if (place==128)
{i=getc(fp2);
place=1; }
else
place<<=1;
}
if (j++<numchar)
putc(n->data,fp1);
}
fclose(fp1);
fclose(fp2);
}
//*********************************************************
void tree(struct treenode *n,int i)
{
atree[i].data=n->data;
atree[i].num=n->num;
numnode=i;
if (n->left!=NULL)
tree(n->left,2*i);
if (n->right!=NULL)
tree(n->right,2*i+1);
}
-
من قبلا این کدو نوشته بودم .
کامل کار می کنه فقط موقع فشرده کردن که به 01 تبدیل می کنه تو فایل باینری ذخیره نمی کنه و تو همون Text File می ریزه
اگه اینو تغییر بدین کد کامل کامله .......
اگه هم خودم حوصله کنم درستش می کنم ...
کد:
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
using namespace std;
int letters[ 256 ];
struct comp{
bool operator() ( const int& a, const int& b ) const
{
return a > b;
}
};
void count( string &str )
{
for( int i = 0; i < str.length(); i++ )
letters[ str[ i ] ]++;
}
int main()
{
string str;
multimap< int, char, comp > sortedLetters;
ifstream inputFile( "C:\\input.txt" );
if( inputFile.is_open() )
{
while( !inputFile.eof() )
{
getline( inputFile, str );
count( str );
}
inputFile.close();
}
else
{
cout << "Can't open the file!\n";
exit( 0 );
}
int index = 0;
for( int i = 0; i < 256; i++ )
{
if( letters[ i ] != 0 )
sortedLetters.insert( make_pair( letters[ i ], i ) );
}
vector< pair< char, string > > table;
pair< char, string > w;
string temp1, temp2;
for( multimap< int, char, comp >::iterator i = sortedLetters.begin(); i != sortedLetters.end(); ++i )
{
temp2 = temp1;
temp2 += "0";
w.first = i->second;
w.second = temp2;
table.push_back( w );
temp1 += "1";
}
if( sortedLetters.size() > 1 )
{
temp1 = table[ table.size() - 1 ].second;
string ss( temp1.begin(), temp1.begin() + temp1.size() - 1 );
table[ table.size() - 1 ].second = ss;
}
ofstream outputFile;
outputFile.open( "C:\\output.txt", ios::app | ios::out );
inputFile.open( "C:\\input.txt", ios::app | ios::in );
if( inputFile.is_open() )
{
while( !inputFile.eof() )
{
getline( inputFile, str );
for( int i = 0; i < str.length(); i++ )
{
for( int j = 0; j < table.size(); j++ )
if( str[ i ] == table[ j ].first )
outputFile << table[ j ].second;
}
outputFile << endl;
}
inputFile.close();
}
outputFile.close();
inputFile.open( "C:\\output.txt", ios::app | ios::in );
outputFile.open( "C:\\output2.txt", ios::app | ios::out );
int l = 0;
int s;
string t;
int c = 0;
if( inputFile.is_open() )
{
while( !inputFile.eof() )
{
if( c != 0 )
outputFile << endl;
getline( inputFile, str );
for( int a = 0; a < str.length(); )
{
for( int i = table.size() - 1; i >= 0; i-- )
{
s = table[ i ].second.size();
t = table[ i ].second;
if( l + s > str.size() )
continue;
string tm( str.begin() + l, str.begin() + l + s );
if( tm == t )
{
outputFile << table[ i ].first;
l += s;
break;
}
}
a += s;
}
l = 0;
c++;
}
inputFile.close();
}
outputFile.close();
return 0;
}