Exemplo n.º 1
0
//---- Encode a given string ----
double encode(const void *nodes, char *string, unsigned size,  double *encoding) {
    struct node *root = ((struct node *) nodes);
    double total_size = 0;
    
    for (int i = 0; i < (int) size; i++) {
        encoding[i] = get_key(root, string[i]);
        total_size += length_of_number(encoding[i]);
    }
    
    return total_size/size;
}
/* This function converts the strings to integers */
int string_to_integer(char *s){
  int expn;          /* The power of 10 with which to multiply values */
  int len_temp;      /* To temporarily store length value */
  int num_temp;      /* To temporarily store the number */
  int i = 0;         /* Counter integer for conversion loop */
  int j = 0;         /* Counter integer for the -vity loop */
  int neg = 1;       /* To capture the -vity or +vity */
  int total = 0;     /* Where the numbers get added to get the actual
                        numerical values*/

  len_temp = length_of_number(s);  /* Call to function for length */
  /* for ridiculously long numbers */
  if(len_temp > 10){
    return 0;
  }
  expn = expo(len_temp);           /* Call to function for exponent */

  /* Loop to determine +vity or -vity */
  while(*(s + j) != '\0'){
    if(*(s + j) == '-'){
      if(*(s + (j + 1)) > '9' || *(s + (j + 1)) < '0'){
        /* To stop the search for numbers in the string */
        break;
      }
      neg *= -1;
    }
    j++;
  }

  while(*(s + i) != '\0'){
    /* Assign the value of (s + i) to an int variable */
    num_temp = *(s + i) - 48;
    if(num_temp >=0 && num_temp <= 9){
      num_temp *= expn;          /* Multiply by expn so that if the number
                                    is 9 then it becomes 90 (for string '98')*/
      expn /= 10;                /* Divide the exponent by 10 so that the
                                    next number (8 in string '98) is
                                    only multiplied by 1 */
      /* To check if the number is going to be greater than an int
         can hold */
      if(total == 2147483640 && num_temp > 7 && neg == 1){
        return 0;
      }
      total += num_temp;         /* Add the value to total */

    }
    i++;                         /* Increase the counter */
  }

  total *= neg;                   /* To convert the number to -ve */
  return total;
}
Exemplo n.º 3
0
//---- Assign Huffman Keys to Nodes in Tree ----
double assign_keys(const void *nodes, int arity) {
    struct node *root = ((struct node *) nodes);
    double expected_code_length = 0;
    
    for (int i = arity - 1; i >= 0; i--) {
        if (root->children[i] != 0) {
            root->children[i]->key = (double) concatenate_numbers((unsigned) root->key,(unsigned) i);
             expected_code_length += assign_keys(root->children[i], arity);
        } else if (root->probability != 0) { 
            //printf("Item with probability %f has key %d\n", root->probability, (int) root->key);
            expected_code_length += (root->probability * (int) length_of_number((unsigned) root->key));
            return expected_code_length;
        }
    }
    
    return expected_code_length;
}
Exemplo n.º 4
0
//---- Get number of digits in a number ----
unsigned length_of_number(unsigned n) {
    if (n < 10) return 1;
    return (1 + length_of_number(n/10));
}