|
aims:
1. General: Use Huffman coding to encrypt the input text (input-> storage-> statistical frequency weighting-> encoding-> replacement-> output);
2. Points: I am currently staying in the statistics section to realize the statistics of the number of occurrences of characters in the input string (as weights, for the purpose of constructing the tree and encoding), and output them.
thought:
The string storage uses a linear sequence table L, and the array w [1000] stores weights (number of times).
problem:
During the counting process of the Counting function, each character read by default is not equal. For example: Enter 'Then what?&' (&is the terminator), then in the weight array: w [1] = w [6] = 2. This will be used to find the two nodes with the smallest weight in the array to create Huffman trees are harmful. How should the algorithm be improved to eliminate it?
Source code:
#include <stdio.h>
#include <stdlib.h>
#define Init_Size 1000
typedef struct {
char * Elem;
int len;
} Sqlist;
Initlist (Sqlist * L)
{L-> Elem = (char *) malloc (sizeof (char) * Init_Size);
if (L-> Elem)
L-> len = 0;
else printf ("ERROR!\n");
printf ("\nInitializing ... Success!\n");
}
ReadIn (Sqlist * L)
{int i = 0;
do
scanf ("% c",&L-> Elem [i ++]);
while ((L-> Elem [i-1])! = '&');
L-> len = i;
printf ("\nREAD IN SUCCESS!\n");
}
Counting (Sqlist * L, char w [1000])
{int count, i, j;
for (i = 0; i <(L-> len); i ++)
{count = 0;
for (j = 0; j <(L-> len); j ++)
if (L-> Elem [i] == L-> Elem [j]) count ++;
w [i] = count;
printf ("w [% d] =% d", i, w [i]);
if ((i% 6 == 0)&&(i> 5)) printf ("\n");
}
}
main ()
{Sqlist * L;
char w [1000];
Initlist (L);
ReadIn (L);
Counting (L, w [1000]);
} |
|