Ok, this program will not run for you because l didn’t include all the files needed to run the program. First of all, this was one of my class assignments and the files that l have included in the header already existed on my machine. l just have to write the code for the algorithm using those included files. If you need those files to see how the program actually works, then leave a comment, apart from that, just explore the logic used and see how it works in general.
#include <iostream>
#include <fstream>
#include “d_hcomp.h”
#include “d_hdcomp.h”
#include “d_hnode.h”
using namespace std;
int main()
{
int i = 0;
char store[20];
int level = -1;
char ch;
ofstream fout;
ifstream fin;
int afreq = 17, bfreq = 8, cfreq = 16,dfreq = 18, efreq = 36, ffreq = 8;
int acount=0,bcount=0,ccount=0,dcount=0,ecount=0,fcount = 0,factor;
cout<<“This program is to demostrate huffman compression and decompression”<<‘n’;
cout<<“Please enter your Factor :”;
cin>>factor;
cout<<“Please enter a total of not more than 20 characters from a to f(lowercase)”<<‘n’;
cin>>ch;
//Generating the input file
while( ch != ‘n’)
{
switch(ch)
{
case ‘a’:
acount++;
store[i] = ch;
i++;
break;
case ‘b’:
bcount++;
store[i] = ch;
i++;
break;
case ‘c’:
ccount++;
store[i] = ch;
i++;
break;
case ‘d’:
dcount++;
store[i] = ch;
i++;
break;
case ‘e’:
ecount++;
store[i] = ch;
i++;
break;
case ‘f’:
fcount++;
store[i] = ch;
i++;
break;
default:
cout<<“Unacceptable character encounterd, program will exit “<<endl;
cout<<ch<<” is not part of the characters from a to f”<<endl;
exit(1);
}
cin.get(ch);
}
//writing the user chars to the input file.
//open the file for writing and clear out any existing text
fout.open(“huf.dat”, ios::in | ios::trunc);
if(!fout)
{
cerr<<“File huf.dat can’t open”;
exit(1);
}
char nchar;
for (int k = 0; k<i;k++)
{
nchar = store[k];
fout<<nchar;
}
//populating the file with user chars using their frequency
int numofchars;
//numofchars is the freq times factor minus number of times a character
//appeared in the user chars
for( int j = 0;j<i;j++)
{
nchar = store[j];
if (nchar == ‘a’ && acount !=0)
{
numofchars = (factor * afreq) – acount;
acount = 0;
for (int m = 0; m < numofchars;m++)
fout<<nchar;
}
else
if (nchar ==’b’ && bcount !=0)
{
numofchars = (factor * bfreq) – bcount;
bcount = 0;
for (int m = 0; m < numofchars;m++)
fout<<nchar;
}
else
if (nchar ==’c’ && ccount !=0)
{
numofchars = (factor * cfreq) – ccount;
ccount = 0;
for (int m = 0; m < numofchars;m++)
fout<<nchar;
}
else
if (nchar ==’d’ && dcount !=0)
{
numofchars = (factor * dfreq) – dcount;
dcount = 0;
for (int m = 0; m < numofchars;m++)
fout<<nchar;
}
else
if (nchar ==’e’ && ecount !=0)
{
numofchars = (factor * efreq) – ecount;
ecount = 0;
for (int m = 0; m < numofchars;m++)
fout<<nchar;
}
else
if (nchar ==’f’ && fcount !=0)
{
numofchars = (factor * ffreq) – fcount;
fcount = 0;
for (int m = 0; m < numofchars;m++)
fout<<nchar;
}
}
//generating chars from a – f which are not in the user chars entered
bool present = false;
for(int k = 0; k < i;k++)
{
if (store[k] == ‘a’)
present = true;
}
if (!present)
{
for(int j = 0; j < factor * afreq;j++)
fout<<“a”;
}
present = false;
for(int k = 0; k < i;k++)
{
if (store[k] == ‘b’)
present = true;
}
if (!present)
{
for(int j = 0; j < factor * bfreq;j++)
fout<<“b”;
}
present = false;
for(int k = 0; k < i;k++)
{
if (store[k] == ‘c’)
present = true;
}
if (!present)
{
for(int j = 0; j < factor * cfreq;j++)
fout<<“c”;
}
present = false;
for(int k = 0; k < i;k++)
{
if (store[k] == ‘d’)
present = true;
}
if (!present)
{
for(int j = 0; j < factor * dfreq;j++)
fout<<“d”;
}
present = false;
for(int k = 0; k < i;k++)
{
if (store[k] == ‘e’)
present = true;
}
if (!present)
{
for(int j = 0; j < factor * efreq;j++)
fout<<“e”;
}
present = false;
for(int k = 0; k < i;k++)
{
if (store[k] == ‘f’)
present = true;
}
if (!present)
{
for(int j = 0; j < factor * ffreq;j++)
fout<<“f”;
}
//close the file after writing all the characters to it.
fout.close();
// compress the file by declaring an object of the hCompress class and call
//it’s member function compress
hCompress hc(“huf.dat”,true);
hc.compress();
//display the huffmantree
hc.displayTree();
//decompress the file
hDecompress idecompress(“huf.huf”,”huf_new.dat”);
idecompress.decompress();
//Open the decompressed file and output on the screen the user chars
fin.open(“huf_new.dat”,ios::out);
if(!fin)
{
cerr<<“The file Huf_new.dat can not be opened”<<endl<<endl;
cin.ignore(256,’n’);
}
else
{
//if successful open, read the first i number of files and output on screan
//i is the number of user chars and they are at the begining of the file.
cout<<“Below is the decompressed user chars”<<endl<<endl;
for(int j=0;j<i;j++)
{
fin>>ch;
cout<<ch;
}
cout<<endl<<endl;
fin.close();
}
cin.get();
return 0;
}