Write a program to compress a file using a Huffman code and to decompose a file
ID: 3568651 • Letter: W
Question
Write a program to compress a file using a Huffman code and to decompose a file generated
using this code. The program should first read through the file and determine the number of
occurrences of each character in the file and the total number of characters in the file. The
weight of each character will be the frequency count for that character. The program should
then use these weights to construct the Huffman codes for the characters in the file. It should
then read the file again and encode it using these Huffman codes and generate a file containing
this encoded data. Compute the compression ratio, which is the number of bits in the
compressed file divided by the total number of bits in the original file (eight times the number
of characters in the file). The program should also provide the option of decompressing a file
that was encoded using this Huffman code.
Requirements:
1. All characters in the file, including alphabetic letters, digits, symbols, and white spaces,
should be encoded. Alphabetic letters in different cases should be considered different.
2. The program should allow the user to choose between
Explanation / Answer
#include #include #include #include class bitChar{ public: unsigned char* c; int shift_count; std::string BITS; bitChar(); void setBITS(std::string _X); int insertBits(std::ofstream& outf); std::string getBits(unsigned char _X); void writeBits(std::ofstream& outf); ~bitChar(); }; #include "bitChar.h" bitChar::bitChar() { shift_count = 0; c = (unsigned char*)calloc(1, sizeof(char)); } void bitChar::setBITS(std::string _X) { BITS = _X; } //Returns number of bits inserted int bitChar::insertBits(std::ofstream& outf) { int total = 0; while(BITS.length()) { if(BITS[0] == '1') *c |= 1; *c count; } }; node* makeNode(char ch, int count) { node* tmp = new node; tmp->ch = ch; tmp->count = count; tmp->left = NULL; tmp->right = NULL; return tmp; }; typedef std::priority_queue mypq; void trie(mypq& _X) { while(_X.size() > 1) { node* holder = new node; holder->left = _X.top(); _X.pop(); holder->right = _X.top(); _X.pop(); holder->count = holder->left->count + holder->right->count; holder->ch = -1; _X.push(holder); } } //Create bit codes by recursively traversing the trie, adding a 0 for left and 1 for right, the key is to remove the end char when the recursion breaks and you have to go up a level void code(node* _X) { static std::string bits = ""; if (_X->right != NULL) { bits += "1"; code(_X->right); bits = bits.substr(0, bits.size() - 1); } if (_X->left != NULL) { bits += "0"; code(_X->left); bits = bits.substr(0, bits.size() - 1); } if(!_X->left && !_X->right) { str_code[_X->ch] = bits; } } void count(std::string file, int& _X){ char letter; std::ifstream inf(file.c_str()); inf >> std::noskipws; //Clears array for(int i = 0;i > letter){ if(letter >= 0 && letter > std::noskipws; while(inf >> input) { BITS += str_code[input]; } inf.close(); //Append ascii 3 EOT character to signify end of text BITS += str_code[3]; return BITS; } int main(int argc, char** argv) { int rc; char choice; unsigned char inChar; std::string inFile = "", outFile = "", BITS = "", BITSsub = "", mn = ""; std::ofstream outf; std::ifstream inf; mypq pq; bitChar bchar; int origSize = 0; std::coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.