Guest User

Untitled

a guest
May 23rd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.35 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define USAGE "Usage: ./edekoder zdekoderOut ekoderCode odrediste\n"
  4. #define MAX_CHAR_NUM 4
  5. #define MAX_CODE_LEN 15
  6.  
  7. struct huffcode
  8. {
  9.     char sign;
  10.     char code[MAX_CODE_LEN];
  11. };
  12.  
  13. struct btree
  14. {
  15.     char value;
  16.     struct btree *left, *right;
  17. };
  18.  
  19. int main(int argc, char **argv) {
  20.     char code[MAX_CODE_LEN], codes[MAX_CHAR_NUM][MAX_CODE_LEN], *to;
  21.     char signs[MAX_CHAR_NUM];
  22.     struct huffcode symbols[MAX_CHAR_NUM];
  23.     struct btree *ps, root, *hufftree = malloc( sizeof(struct btree) );
  24.     int i, j, found, count;
  25.     FILE *ekoderCode;
  26.    
  27.     if (argc != 4) {
  28.         printf(USAGE);
  29.         return -1;
  30.     }
  31.  
  32.     if ((ekoderCode = fopen(argv[2], "r")) == NULL) {
  33.         printf("Unable to read file\n");
  34.         return -2;
  35.     }
  36.    
  37.     count = 0;
  38.     for (i=0; i<MAX_CHAR_NUM; i++) {
  39.         fgets(code, MAX_CODE_LEN, ekoderCode);
  40.         count++;
  41.         symbols[i].sign=code[0];
  42.         to = &code[2];
  43.         for (j=0; j!='\n'; j++) {
  44.             codes[i][j]=to[j];
  45.         }
  46.     }
  47.    
  48.     // Makni newline znakove
  49.     for (i=0; i<MAX_CHAR_NUM; i++) {
  50.         j=0;
  51.         found=0;
  52.         do {
  53.             if(codes[i][j]=='\n') {
  54.                 codes[i][j]='\0';
  55.                 found=1;
  56.             }
  57.             j++;
  58.         } while (found!=1);
  59.         found=0;
  60.     }
  61.     hufftree[0].left = NULL;
  62.     hufftree[0].right = NULL;
  63.     hufftree[0].value = 'r';
  64.     count = 1;
  65.     for (i=0; i<MAX_CHAR_NUM; i++) {
  66.         j=0;
  67.         found=0;
  68.         ps = &(hufftree[0]);
  69.         printf("%c\n", ps->value);
  70.         do {
  71.             if (codes[i][j]=='0') {
  72.                 if (ps->left!=NULL) {
  73.                     ps = ps->left;
  74.                 }
  75.                 else {
  76.                     hufftree = realloc(hufftree, (count+2)*sizeof(struct btree));
  77.                     ps->left = &hufftree[count];
  78.                     ps->right = &hufftree[count+1];
  79.                     hufftree[count].left = NULL;
  80.                     hufftree[count].right = NULL;
  81.                     hufftree[count+1].left = NULL;
  82.                     hufftree[count+1].right = NULL;
  83.                     ps = ps->left;
  84.                     ps->value = '0';
  85.                     count = count + 2;
  86.                 }
  87.             }
  88.             if (codes[i][j]=='1') {
  89.                 if (ps->right!=NULL) {
  90.                     ps = ps->right;
  91.                 }
  92.                 else {
  93.                     hufftree = realloc(hufftree, (count+2)*sizeof(struct btree));
  94.                     ps->left = &hufftree[count];
  95.                     ps->right = &hufftree[count+1];
  96.                     ps->right->value = '1';
  97.                     if (hufftree[0].right == NULL)
  98.                         printf("[%d][%d] right! %c rootvalue=%c\n", i, j, hufftree[0].right->value, hufftree[count+1].value);
  99.                     hufftree[count].left = NULL;
  100.                     hufftree[count].right = NULL;
  101.                     hufftree[count+1].left = NULL;
  102.                     hufftree[count+1].right = NULL;
  103.                     ps = ps->right;
  104.                     ps->value = '1';
  105.                     count = count + 2;
  106.                 }
  107.             }
  108.             if(codes[i][j]=='\0') {
  109.                 found=1;
  110.             }
  111.             j++;
  112.         } while (found!=1);
  113.         found=0;
  114.     }
  115.     //for (ps = &(hufftree[0]); ps != NULL; ps = ps->right) {
  116.     //    printf("%c\n", ps->value);
  117.     //}
  118.     free(hufftree);
  119. }
Add Comment
Please, Sign In to add comment