Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #define USAGE "Usage: ./edekoder zdekoderOut ekoderCode odrediste\n"
- #define MAX_CHAR_NUM 4
- #define MAX_CODE_LEN 15
- struct huffcode
- {
- char sign;
- char code[MAX_CODE_LEN];
- };
- struct btree
- {
- char value;
- struct btree *left, *right;
- };
- int main(int argc, char **argv) {
- char code[MAX_CODE_LEN], codes[MAX_CHAR_NUM][MAX_CODE_LEN], *to;
- char signs[MAX_CHAR_NUM];
- struct huffcode symbols[MAX_CHAR_NUM];
- struct btree *ps, root, *hufftree = malloc( sizeof(struct btree) );
- int i, j, found, count;
- FILE *ekoderCode;
- if (argc != 4) {
- printf(USAGE);
- return -1;
- }
- if ((ekoderCode = fopen(argv[2], "r")) == NULL) {
- printf("Unable to read file\n");
- return -2;
- }
- count = 0;
- for (i=0; i<MAX_CHAR_NUM; i++) {
- fgets(code, MAX_CODE_LEN, ekoderCode);
- count++;
- symbols[i].sign=code[0];
- to = &code[2];
- for (j=0; j!='\n'; j++) {
- codes[i][j]=to[j];
- }
- }
- // Makni newline znakove
- for (i=0; i<MAX_CHAR_NUM; i++) {
- j=0;
- found=0;
- do {
- if(codes[i][j]=='\n') {
- codes[i][j]='\0';
- found=1;
- }
- j++;
- } while (found!=1);
- found=0;
- }
- hufftree[0].left = NULL;
- hufftree[0].right = NULL;
- hufftree[0].value = 'r';
- count = 1;
- for (i=0; i<MAX_CHAR_NUM; i++) {
- j=0;
- found=0;
- ps = &(hufftree[0]);
- printf("%c\n", ps->value);
- do {
- if (codes[i][j]=='0') {
- if (ps->left!=NULL) {
- ps = ps->left;
- }
- else {
- hufftree = realloc(hufftree, (count+2)*sizeof(struct btree));
- ps->left = &hufftree[count];
- ps->right = &hufftree[count+1];
- hufftree[count].left = NULL;
- hufftree[count].right = NULL;
- hufftree[count+1].left = NULL;
- hufftree[count+1].right = NULL;
- ps = ps->left;
- ps->value = '0';
- count = count + 2;
- }
- }
- if (codes[i][j]=='1') {
- if (ps->right!=NULL) {
- ps = ps->right;
- }
- else {
- hufftree = realloc(hufftree, (count+2)*sizeof(struct btree));
- ps->left = &hufftree[count];
- ps->right = &hufftree[count+1];
- ps->right->value = '1';
- if (hufftree[0].right == NULL)
- printf("[%d][%d] right! %c rootvalue=%c\n", i, j, hufftree[0].right->value, hufftree[count+1].value);
- hufftree[count].left = NULL;
- hufftree[count].right = NULL;
- hufftree[count+1].left = NULL;
- hufftree[count+1].right = NULL;
- ps = ps->right;
- ps->value = '1';
- count = count + 2;
- }
- }
- if(codes[i][j]=='\0') {
- found=1;
- }
- j++;
- } while (found!=1);
- found=0;
- }
- //for (ps = &(hufftree[0]); ps != NULL; ps = ps->right) {
- // printf("%c\n", ps->value);
- //}
- free(hufftree);
- }
Add Comment
Please, Sign In to add comment