Write a program that takes any input text and produces both a frequency table and the
corresponding Huffman code. Take approximately 360 words from any English document as
your input text. Ignore all blanks, all punctuation marks, al special symbols. Create an input file
with this input text. Construct the frequency table according to the input text read from the file,
in the form: The Frequency\'s MUST be listed, in order, from largest (at the top) to smallest (at
the bottom). Only the BELOW Tablet Format will be accepted: Letter Comma Space Percentage
Example: A, 2.5% Then, using the Huffman algorithm, construct the optimal prefix binary code
for the table. Design your program to read the input from the input file \"infile.dat\" Your
program must produce the output, in the file \"outfile.dat\". consisting of the frequency table for
the source text. the Huffman code for each letter and digit in the source code, and the length of
the coded message in terms of number of bits. MUST use exact file names provided
Solution
#include \"FileLoader.h\"
#include \"CharFreqInVector.h\"
#include \"LeafNode.h\"
#include \"TreeFromListConstruction.h\"
#include \"GenerateHuffFile.h\"
#include \"Decompressor.h\"
#include
#include
#include
int main()
{
{
vector vFile;
vector cvFile;
vector charByte;
vector frequency;
FileLoader File(\"shakespeare.txt\");
vFile = File.loadFileIntoVector();
CharFreqInVector Frequency(vFile);
Frequency.calculateFrequency();
charByte = Frequency.getEachChar();//Frequency.getEachChar(); // need copies of the char
and its frequency to put to a LeafNode
frequency = Frequency.getEachFrequency();//Frequency.getEachFrequency();
TreeFromListConstruction tree(charByte, frequency);
//passing into a class that can modify the list into a binary tree
tree.formTree(); //modify the list into a binary tree
GenerateHuffFile Huff(tree.getTree(), vFile, \"compressed.txt\",charByte, frequency);
Huff.writeHuffFile();
///////////////////////////////////////////////////////////////////////////////////////decompression time
FileLoader cFile(\"compressed.txt\");
cvFile = cFile.loadFileIntoVector();
CharFreqInVector Formated(cvFile);
Formated.calcFreqInCompressed();
charByte = Formated.getEachChar();//Frequency.getEachChar(); // need copies of the char and
its frequency to put to a LeafNode
frequency = Formated.getEachFrequency();//Frequency.getEachFrequency();
TreeFromListConstruction recontructedTree(charByte, frequency);
recontructedTree.formTree();
Decompressor uncompress(recontructedTree.getTree(), cvFile, \"compressed.txt\",
\"decompressed.txt\", vFile.size());
uncompress.remakeFile();
}
cout << _CrtDumpMemoryLeaks(); //no memory leaks yay!
system(\"pause\");
return 0;
}
CharFreqInVector.h
#ifndef CHARFREQINVECTOR_H
#define CHARFREQINVECTOR_H
#include
#include
#include
using namespace std;
class CharFreqInVector
{
public:
CharFreqInVector(vector& charVec);
~CharFreqInVector();
void clearVectors();
void calculateFrequency();
void calcFreqInCompresse.
Write a program that takes any input text and produces both a frequen.pdf
1. Write a program that takes any input text and produces both a frequency table and the
corresponding Huffman code. Take approximately 360 words from any English document as
your input text. Ignore all blanks, all punctuation marks, al special symbols. Create an input file
with this input text. Construct the frequency table according to the input text read from the file,
in the form: The Frequency's MUST be listed, in order, from largest (at the top) to smallest (at
the bottom). Only the BELOW Tablet Format will be accepted: Letter Comma Space Percentage
Example: A, 2.5% Then, using the Huffman algorithm, construct the optimal prefix binary code
for the table. Design your program to read the input from the input file "infile.dat" Your
program must produce the output, in the file "outfile.dat". consisting of the frequency table for
the source text. the Huffman code for each letter and digit in the source code, and the length of
the coded message in terms of number of bits. MUST use exact file names provided
Solution
#include "FileLoader.h"
#include "CharFreqInVector.h"
#include "LeafNode.h"
#include "TreeFromListConstruction.h"
#include "GenerateHuffFile.h"
#include "Decompressor.h"
#include
#include
#include
int main()
{
{
vector vFile;
vector cvFile;
vector charByte;
vector frequency;
FileLoader File("shakespeare.txt");
vFile = File.loadFileIntoVector();
CharFreqInVector Frequency(vFile);
2. Frequency.calculateFrequency();
charByte = Frequency.getEachChar();//Frequency.getEachChar(); // need copies of the char
and its frequency to put to a LeafNode
frequency = Frequency.getEachFrequency();//Frequency.getEachFrequency();
TreeFromListConstruction tree(charByte, frequency);
//passing into a class that can modify the list into a binary tree
tree.formTree(); //modify the list into a binary tree
GenerateHuffFile Huff(tree.getTree(), vFile, "compressed.txt",charByte, frequency);
Huff.writeHuffFile();
///////////////////////////////////////////////////////////////////////////////////////decompression time
FileLoader cFile("compressed.txt");
cvFile = cFile.loadFileIntoVector();
CharFreqInVector Formated(cvFile);
Formated.calcFreqInCompressed();
charByte = Formated.getEachChar();//Frequency.getEachChar(); // need copies of the char and
its frequency to put to a LeafNode
frequency = Formated.getEachFrequency();//Frequency.getEachFrequency();
TreeFromListConstruction recontructedTree(charByte, frequency);
recontructedTree.formTree();
Decompressor uncompress(recontructedTree.getTree(), cvFile, "compressed.txt",
"decompressed.txt", vFile.size());
uncompress.remakeFile();
}
cout << _CrtDumpMemoryLeaks(); //no memory leaks yay!
system("pause");
return 0;
}
CharFreqInVector.h
#ifndef CHARFREQINVECTOR_H
#define CHARFREQINVECTOR_H
#include
9. GenerateHuffFile(vector *aList, vector vFile, string outPutFile,vector charBytes, vector
frequencies);
void writeHuffFile();//writes the true and bits to file
void writeTreePortionToFile();//writes the tree into a file
void writeBitPortionToFile();//writes the bits to a file
void writeBits(vector bitVector,int i);
void convertBoolVecToBits();//turns the boolean vector representing bits to real bits to write
to file
string getBitsInChar(char dummyByte);
private:
vector *myList;
vector vectorFile;
vector charBytes ;
vector frequencies ;
string outputFile;
ofstream fout;
char dummyByte;
int bitCounter;
int bitPosition;
int totalBitsInFile;
static const int bitsInAByte = 8;
};
#endif
//GenerateHuffFile.cpp
#include "GenerateHuffFile.h"
GenerateHuffFile::GenerateHuffFile(vector *aList, vector vFile, string outFile, vector cBytes,
vector freqs)
{
vectorFile = vFile;
outputFile = outFile;
myList = aList;
bitCounter = 0;
bitPosition = 128;
10. totalBitsInFile = 0;
charBytes = cBytes;
frequencies = freqs;
}
void GenerateHuffFile::writeHuffFile()
{
fout.open(outputFile.c_str(), ios::binary);
//diplay tree in file
for(unsigned int i = 0; i < charBytes.size(); i++)
{
fout << charBytes[i];
fout << frequencies[i];
fout << ',';
}
fout << "00" << ','; //marker to mark the end of the tree in the file
writeBitPortionToFile();
fout.close();
}
void GenerateHuffFile::writeBits(vector bitVector,int index)
{
// for(unsigned int i = 1; i < bitVector.size();i++)
//{
//cout << bitVector[i];
// }
//cout << endl;
for(unsigned int i = 1; i < bitVector.size(); i++)
{
if(bitVector[i])