Code:
//Nicholas Riseden
//CSCI 3300
//Assignment 4 Version 2
#include "tree.h"
#include "pqueue.h"
#include <string>
#include <fstream>
#include <iostream>
#include <stdio.h>
#include "binary1.h"
using namespace std;
/*****************************************************************
* buildTree *
******************************************************************
* buildTree takes the array built from getFrequency and builds a *
* tree that holds a character and its priority using the insert *
* function. *
* *
* For example, if the file reads "AAb" it creates a node for the *
* character 'A' and stores its priority 2 for that node. *
*****************************************************************/
Node* buildTree(int arrayOfInts[])
{
PriorityQueue q;
for(int x = 0; x <= 255; x++)
{
if(arrayOfInts[x]!=0)
{
Node* leaf = new Node((char)x);
insert(q, leaf, arrayOfInts[x]);
}
}
Node* t1,*t2;
int p1, p2;
remove(q, t1, p1);
while(isEmpty(q)==false)
{
remove(q,t2, p2);
Node* t3 = new Node(t1, t2);
insert(q, t3, (p1 +p2));
remove(q, t1, p1);
}
return t1;
}
/*****************************************************************
* getFrequency *
******************************************************************
* getFrequency takes an array of 256 integers. It opens a *
* file and counts the amount of character *
* frequencies that occur, storing that count into the position *
* in the array that correpsonds with the character's *
* Ascii value. *
* *
* For example, if the file reads "AAb" it stores the number 2 in *
* position 64 in the array since capital A is a 65 on the Ascii *
* chart and arrays are zero based. *
*****************************************************************/
int* getFrequency(char* filename, int arrayOfInts[])
{
ifstream in;
in.open(filename);
for(int i = 0; i < 256; i++)
{
arrayOfInts[i] = 0;
}
int c = in.get();
while(c!=EOF)
{
arrayOfInts[c]++;
c = in.get();
}
int count = 0;
cout<<"\nThe character frequencies are: \n\n";
int k = 0;
while(k < 256)
{
if(k>0)
{
if(k == 10)
{
cout<<"\\n "<<arrayOfInts[k]<<"\n";
}
else if(k!=10)
{
cout<<char(k)<< " "<<arrayOfInts[k]<<"\n";
}
}
count++;
cout<<"count is "<<count<<"\n";//count reaches 256, then segmentation fault is printed
k++;
}
in.close();
cout<<"This is sparta\n";
return arrayOfInts;
cout<< "This is over\n";
}
//comments to describe this function
void writeTree(BFILE* f, Node* t)
{
if(t->kind!=leaf)
{
writeBit(f, 0);
writeTree(f, t->left);
writeTree(f, t->right);
}
else
{
writeBit(f, 1);
writeByte(f, t->ch);
}
}
//buildcode
void buildCode(Node* n, string codeArray[], string pref)
{
if(n->kind == leaf)
{
codeArray[int(n->ch)] = pref;
}
else if(n->kind == nonleaf)
{
buildCode(n->left, codeArray, pref + "0");
buildCode(n->right, codeArray, pref + "1");
}
}
//writecode(f, str) writes string str to BFILE* f, one bit
//at a time.
void writeCode(BFILE* f, string str)
{
int y = str.length();
for(int x = 0; x < y; x++)
{
writeBit(f, str[x]);
}
}
//function description
void writeCodedFile(char* filename, string codearray[], BFILE* f)
{
ifstream in;
in.open(filename);
int c = cin.get();
while(c != EOF)
{
writeCode(f, codearray[c]);
c = cin.get();
}
cout<<"Test E";
in.close();
}
int main(int argc, char* argv[])
{
string codeArray[255];
int arrayOfInts[255];
int* frequencyArray = getFrequency(argv[1], arrayOfInts);//problem is here
cout<<"I am about to do buildTree.";//this never gets printed
Node* n = buildTree(frequencyArray);
cout<<"I am done buildTree.";//this never gets printed
buildCode(n, codeArray, "");
BFILE* f = openBinaryFileWrite(argv[2]);
writeTree(f,n);
writeCodedFile(argv[1], codeArray,f);
closeBinaryFileWrite(f);
return 0;
}
Comment