In C++ read.cpp #include <iostream> #include <fstream> using namespace std; int
ID: 3708439 • Letter: I
Question
In C++
read.cpp
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[]){
unsigned char ch;
ifstream in(argv[1],ios::in|ios::binary|ios::ate);
size_t size = 0; // here
size = in.tellg() ; // get the length of the file
cout << "Size of file: " << size << endl;
in.seekg(0, ios::beg); // set the pointer to the beginning
for (int i =0; i<size; i++) {
ch = in.get();
cout << (int) ch << endl;
}
}
decode.cpp
#include <iostream>
#include <fstream>
using namespace std;
struct treenode {
unsigned char data;
treenode *left, *right;
};
int strchr(unsigned char s[], unsigned char c) {
int i=0;
while(s[i]!=c) i++;
return i;
}
treenode * treebuild(unsigned char pre[], unsigned char in[], int & pos, int size) {
if (size==pos) return NULL;
treenode *t = new treenode;
t->data = pre[pos]; t->left = NULL; t->right = NULL;
int loc = strchr(in, pre[pos++]);
in[loc] = 0;
if (loc > 0 && in[loc-1] != 0)
t->left = treebuild(pre, in, pos, size);
if (loc < size-1 && in[loc+1] != 0)
t->right = treebuild(pre,in,pos, size);
return t;
}
void parse(string c, treenode *t) {
treenode *p = t;
for(int pos=0; pos < c.length(); pos++) {
p = c[pos]=='0' ? p->left : p->right;
if (p->left == NULL) {
cout << p->data;
p = t;
}
}
}
int main(int argc, char* argv[]) {
ifstream prefile(argv[1],ios::in|ios::binary|ios::ate), infile(argv[2],ios::in|ios::binary), codefile(argv[3]);
size_t size = 0; // here
size = prefile.tellg() ; // get the length of the file
prefile.seekg(0, ios::beg); // set the pointer to the beginning
unsigned char *pre = new unsigned char[size], *in = new unsigned char[size];
for (int i =0; i<size; i++) {
pre[i] = prefile.get();
in[i] = infile.get();
}
string code;
getline(codefile,code);
int pos = 0;
treenode *tree = treebuild(pre,in,pos,size);
parse(code,tree);
cout << endl;
}
decodebin.cpp
#include <iostream>
#include <fstream>
using namespace std;
struct treenode {
unsigned char data;
treenode *left, *right;
};
int strchr(unsigned char s[], unsigned char c) {
int i=0;
while(s[i]!=c) i++;
return i;
}
treenode * treebuild(unsigned char pre[], unsigned char in[], int & pos, int size) {
if (size==pos) return NULL;
treenode *t = new treenode;
t->data = pre[pos]; t->left = NULL; t->right = NULL;
int loc = strchr(in, pre[pos++]);
in[loc] = 0;
if (loc > 0 && in[loc-1] != 0)
t->left = treebuild(pre, in, pos, size);
if (loc < size-1 && in[loc+1] != 0)
t->right = treebuild(pre,in,pos, size);
return t;
}
void parse(string c, treenode *t,short count) {
treenode *p = t;
for(int pos=0; pos < c.length(); pos++) {
p = c[pos]=='0' ? p->left : p->right;
if (p->left == NULL) {
cout << p->data;
p = t;
if(--count == 0) return;
}
}
}
int main(int argc, char* argv[]) {
ifstream prefile(argv[1],ios::in|ios::binary|ios::ate), infile(argv[2],ios::in|ios::binary), codefile(argv[3],ios::in|ios::binary|ios::ate);
size_t size = 0; // here
size = prefile.tellg() ; // get the length of the file
prefile.seekg(0, ios::beg); // set the pointer to the beginning
unsigned char *pre = new unsigned char[size], *in = new unsigned char[size];
for (int i =0; i<size; i++) {
pre[i] = prefile.get();
in[i] = infile.get();
}
int pos = 0;
short count = 0;
treenode *tree = treebuild(pre,in,pos,size);
string code="";
size = codefile.tellg() ; // get the length of the file
codefile.seekg(0, ios::beg); // set the pointer to the beginning
codefile.read(reinterpret_cast<char *>(&count),2);
for (int i=0; i<size-2; i++) {
unsigned char ch = codefile.get(), mask = 128;
for (int j=0;j<8;j++) {
code += (ch & mask)?'1':'0';
mask >>=1;
}
}
parse(code,tree,count);
cout << endl;
}
Explanation / Answer
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
class HuffmanCodes
{
struct Node
{
char data;
size_t freq;
Node* left;
Node* right;
Node(char data, size_t freq) : data(data),
freq(freq),
left(nullptr),
right(nullptr)
{}
~Node()
{
delete left;
delete right;
}
};
struct compare
{
bool operator()(Node* l, Node* r)
{
return (l->freq > r->freq);
}
};
Node* top;
void printCode(Node* root, std::string str)
{
if(root == nullptr)
return;
if(root->data == '$')
{
printCode(root->left, str + "0");
printCode(root->right, str + "1");
}
if(root->data != '$')
{
std::cout << root->data << " : " << str << " ";
printCode(root->left, str + "0");
printCode(root->right, str + "1");
}
}
public:
HuffmanCodes() {};
~HuffmanCodes()
{
delete top;
}
void GenerateCode(std::vector<char>& data, std::vector<size_t>& freq, size_t size)
{
Node* left;
Node* right;
std::priority_queue<Node*, std::vector<Node*>, compare > minHeap;
for(size_t i = 0; i < size; ++i)
{
minHeap.push(new Node(data[i], freq[i]));
}
while(minHeap.size() != 1)
{
left = minHeap.top();
minHeap.pop();
right = minHeap.top();
minHeap.pop();
top = new Node('$', left->freq + right->freq);
top->left = left;
top->right = right;
minHeap.push(top);
}
printCode(minHeap.top(), "");
}
};
int main()
{
HuffmanCodes set1;
std::vector<char> data({'d', 'e', 'b', 'c', 'a', 'f'});
std::vector<size_t> freq({16, 9, 13, 12, 45, 5});
size_t size = data.size();
set1.GenerateCode(data, freq, size);
return 0;
}