/***** * run_huffman.c * * Create a huffman code from an analysis of a text file, * and print a description of the generated code. * * $Id: run_huffman.c 13167 2007-04-30 03:54:16Z mahoney $ *********/ #include #include "heap.h" #include "huffman.h" void process_file(char* filename, int start_index, int max_chars){ int frequencies[N_ASCII]; huffnode huffnodes[N_ASCII]; char* codes[N_ASCII]; heap the_heap; huffnode root; if (max_chars<0){ start_index = 0; printf("\n\n======== all of '%s' =======\n", filename); } else { printf("\n\n====== %i chars starting from %i of '%s' ===\n", max_chars, start_index, filename); } calculate_frequencies(filename, frequencies, start_index, max_chars); frequencies2huffnodes(frequencies, huffnodes); the_heap = huffnodes2heap(huffnodes); root = make_huffman_tree(the_heap); calculate_codes(root, "", codes); print_summary(frequencies, codes); print_analysis(frequencies, codes); } int main(){ char* moby_dick = "moby_dick.txt"; process_file(moby_dick, 0, -1); // whole file process_file(moby_dick, 0, 1000); // 1st 1000 characters process_file(moby_dick, 2000, 1000); // different 1000 characters process_file(moby_dick, 10000, 1000); // different 1000 characters }