Created
January 1, 2013 21:36
-
-
Save anonymous/4430213 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include "tree.h" | |
#include "utility.h" | |
/*check to make sure that the correct number of inputs is put in*/ | |
int CheckArgCount(int argc){ | |
if (argc != 3){ | |
printf("Not enough arguments.\n Use: ipa2_5 <filename> <output_filename>\n"); | |
return FALSE; | |
} | |
return TRUE; | |
} | |
/*open the file and make sure it opens correctly*/ | |
FILE* OpenFile(char FileName[], char op[]){ | |
FILE* fptr; | |
fptr = fopen(FileName, op); | |
if(fptr == NULL){ | |
printf("Error opening file!\n"); | |
return NULL; | |
} | |
return fptr; | |
} | |
void construct_tree(FILE *fptr,LEAF **huffman_tree){ | |
int freq_array [255]; | |
int value_array [255]; | |
int test; | |
/*set all values in the array's to 0*/ | |
memset(freq_array, 0, sizeof (freq_array)); | |
memset(value_array, 0, sizeof (value_array)); | |
/*read the file and create 2 arrays. the first of what character is located at what location and the second for the frequency of each character located at its ascii value within the array*/ | |
fseek(fptr,0, SEEK_SET); | |
while ((test = fgetc(fptr))!= EOF){ | |
if (test!= '\n'){ | |
value_array[test] = test; | |
freq_array[test] += 1; | |
} | |
if (test == '\n'){ | |
value_array[test] = test; | |
freq_array[test] += 1; | |
} | |
} | |
/*condence the array*/ | |
int i; | |
int j =0; | |
int length = 0; | |
int org_array[255][2]; | |
memset(org_array, 0, sizeof org_array); | |
for(i=0;i<255;i++){ | |
if (value_array[i] != 0){ | |
org_array[j][0]= value_array[i]; | |
org_array[j][1]= freq_array[i]; | |
length = j; | |
j++; | |
} | |
} | |
length++; | |
int compressed_array[length][2]; | |
for (i=0;i<length;i++){ | |
compressed_array[i][0] = org_array[i][0]; | |
compressed_array[i][1] = org_array[i][1]; | |
} | |
int a; | |
int b; | |
for (j=0;j<length;j++){ | |
for (i=0;i<length;i++){ | |
if ((compressed_array[i][1] > compressed_array[i+1][1])&&(compressed_array[i+1][1]!= 0)){ | |
a = compressed_array[i][0]; | |
b =compressed_array[i][1]; | |
compressed_array[i][0]=compressed_array[i+1][0]; | |
compressed_array[i][1]=compressed_array[i+1][1]; | |
compressed_array[i+1][0] = a; | |
compressed_array[i+1][1] = b; | |
} | |
printf("value : %c freq: %d\n",compressed_array[i][0],compressed_array[i][1]); | |
} | |
} | |
/*create a node for each value within the array*/ | |
LEAF *array[length] ; | |
LEAF *temp=NULL; | |
LEAF *temp2=NULL; | |
for(i = 0; i <length; i++){ | |
array[i] = malloc(sizeof(LEAF)); | |
(array[i] -> value) = compressed_array[i][0]; | |
(array[i] -> freq) = compressed_array[i][1]; | |
(array[i] -> lchild) = NULL; | |
(array[i] -> rchild) = NULL; | |
} | |
/*combine the nodes in the correct order until all but one node remains*/ | |
LEAF *temp3=NULL; | |
int max = length; | |
int location = 0; | |
while (max>1){ | |
temp = array[location]; | |
temp3 = array[location+1]; | |
array[location] = malloc(sizeof(LEAF)); | |
array[location]->freq = temp->freq + array[location+1]->freq; | |
array[location]->value = 127; | |
array[location]->rchild = temp3; | |
array[location]->lchild = temp; | |
location++; | |
for (i=0;i<length;i++){ | |
if (((array[i]->freq) >= (array[i+1]->freq))&& (array[i]->value == 127)&& (i+1 < length)){ | |
temp2 = array[i]; | |
array[i]=array[i+1]; | |
array[i+1] = temp2; | |
} | |
} | |
max = max - 1; | |
} | |
*huffman_tree = array[length-1]; | |
} | |
void postorder(LEAF *node,FILE *optr){ | |
if(node == NULL){ | |
return; | |
} | |
postorder(node->lchild,optr); | |
postorder(node->rchild,optr); | |
if (node->value != 127){ | |
fprintf(optr,"1"); | |
convert(node->value, optr); | |
} | |
if (node->value == 127){ | |
fprintf(optr, "0");} | |
} | |
void Destroy(LEAF* tree){ | |
if(tree==NULL){return;} | |
/* Free the left child */ | |
Destroy(tree->lchild); | |
/* Free the right child */ | |
Destroy(tree->rchild); | |
/*Free the current node */ | |
free(tree); | |
return; | |
} | |
/*convert to binary*/ | |
void convert(char ch, FILE* optr){ | |
fprintf(optr,"%c%c%c%c%c%c%c%c", | |
(ch&0x80)?'1':'0', | |
(ch&0x40)?'1':'0', | |
(ch&0x20)?'1':'0', | |
(ch&0x10)?'1':'0', | |
(ch&0x08)?'1':'0', | |
(ch&0x04)?'1':'0', | |
(ch&0x02)?'1':'0', | |
(ch&0x01)?'1':'0' | |
); | |
} | |
/*compress to binary representation*/ | |
void compress(FILE* fptr, FILE* optr, LEAF* tree){ | |
char test=0; | |
int i =-1; | |
int array[255] = {'2'}; | |
int key = 0; | |
fseek(fptr, 0, SEEK_SET); | |
while ((test = fgetc(fptr))!= EOF){ | |
find_val(test, optr, tree, array,i, key); | |
} | |
int find_val(char test, FILE* optr, LEAF* node, int array[], int i, int key){ | |
if (i == -1){memset(array, '2', sizeof (array));} | |
i++; | |
if (node == NULL){ | |
array[i-1] = '2'; | |
return key; | |
} | |
if (key ==0){ | |
array[i] = '0'; | |
key = find_val(test, optr,node->lchild,array, i, key); | |
array[i] = '1'; | |
key = find_val(test, optr,node->rchild,array, i, key); | |
} | |
if((node->value == test)){ | |
int j; | |
for(j=0;j<255; j++){ | |
if ((array[j] != '2')&& (array[j] != 0)){ | |
if (array[j]== 49){fprintf(optr,"%d",1);} | |
if (array[j]== 48){fprintf(optr,"%d",0);} | |
} | |
} | |
key = 1; | |
} | |
return key; | |
} | |
/*convert 8 numbers represented in binary to actual binary in bit representation*/ | |
void tobit(FILE* optr, FILE *ptr){ | |
fseek(optr, 0, SEEK_SET); | |
int i; | |
int test; | |
int Binary[8]={128,64,32,16,8,4,2,1}; | |
do{ | |
int FullBinary[8] = {0}; | |
for (i=0;i<8;i++){ | |
test=fgetc(optr); | |
FullBinary[i] = test-48; | |
// printf("%d", FullBinary[i]); | |
} | |
int x=0; | |
int Total=0; | |
for (x=0; x<8; x++){ | |
if (FullBinary[x]==1) { | |
Total = Total+Binary[x]; | |
} | |
} | |
char temp = Total; | |
printf("%d\n", Total); | |
if ((Total <255 )&&(Total>0)){ | |
fprintf(ptr, "%c", temp);} | |
}while(test!= EOF); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment