Data Structure Course Design-Harman encoding and decoding
//************************************** * ***** // Program functions: haffman encoding and decoding // Date: november 18, 2014 ////*********************************** * ******** # include
# Include
# Include
# Include
# Define MAX 128 // Number of leaf nodes (corresponding ASCII code) # define M 2 * Number of MAX-1 // Tree nodes typedef struct node {int weight; // weight int parent; // int LChild; // int RChild of the left child; // HTNode, HuffmanTree [M + 1]; typedef struct Node {char letter; // char * code; // encode int w; // weight (number of occurrences of letters (letter) in the document)} Huffman [MAX + 1]; HuffmanTree ht; // store the Huffman qz; // int weight [M + 1] = {0}; // store the temporary value int t = 0; // store the number of leaf nodes/********************* function declaration ********** ****** * *****/Void select (int * s1, int * s2); // select the int ReadWeight () value when a user creates a Harman tree (); // read the document from the file to obtain the weight value, and return the number of weights void CrtHuffmanTree (); // create the Haffman tree void Encoding (); // encode void Print (); // print void WriteTree (); // write the User-Defined identity (void Initialization) to the file; // initialize void WriteCode (); // write the encoding void Decoding () to the file; // decodes int find_letter (char ch); // finds the character int find_code (char s []); // search for the encoding void InitTree (); // initialize the tree void TreePrinting (); // print the Haffman tree void Menu (); // Menu void load (); // lo Adingvoid _ print (FILE * fp, node hft, int n); // The main function int main () {Menu (); // call the Menu to complete a series of work return 0 ;} void Menu () {char chose; load (); InitTree (); printf ("************************* Menu ********** * ************** \ n "); printf ("******* initialization (I) ************ encoding (E) ********* decoding (D) * \ n "); printf (" * print the code file (P) * print the Harman tree (T) * *** exit (O) ******** \ n "); printf ("************************************* * ***************** \ n "); while (true) {printf ("Please select:"); scanf ("% c", & chose); getchar (); // remove the carriage return switch (chose) {case 'I': Initialization (); break; // initialize case 'E': Encoding (); break; // encode case 'D': Decoding (); break for leaf nodes; // decoding case 'p': Print (); break; // Print the encoding case 'T': TreePrinting (); break; // Print the Harman tree case 'O ': printf ("exit now! \ N "); // exit reminder Sleep (2000); // suspend exit (1); // exit program default: printf (" How can I choose wrong! \ N ") ;}}// loadingvoid load () {printf (" loading "); for (int I = 1; I <= 10; I ++) {printf (". "); Sleep (500);} printf (" \ n is about to enter! \ N "); Sleep (2000);} // initialization tree void InitTree () {ht [0]. weight = 0; // indicates whether a qz [0] exists in the Harman tree. w = 0; // indicates whether to encode} // initialize void Initialization () {ht [0]. weight = 1; // indicates the initialization flag, indicating that the user tree must exist t = ReadWeight (); // read the CrtHuffmanTree (); // create the user tree WriteTree (); // write the Harman tree to the file printf ("Yeah! 'Initializing 'is successful! \ N ");} // write the user tree to the FILE void WriteTree () {FILE * fp; // hfmTree FILE pointer int m = 2 * T-1; int I; // open the file if (fp = fopen ("F: \ hfmTree.txt", "w") = NULL) {printf ("open hfmTree.txt -- file error \ n"); exit (0);} // else printf ("open hfmTree.txt -- file sucess! \ N "); // write the file for (I = 1; I <= m; I ++) fprintf (fp, "% d \ n", ht [I]. weight, ht [I]. parent, ht [I]. LChild, ht [I]. RChild); // close the file if (fclose (fp) {printf ("close hfmTree.txt -- file error! \ N "); exit (0);} // else printf (" close hfmTree.txt -- file success! \ N ");} // select s1, s2void select (int n, int * s1, int * s2) {int I; int min; // find a node without any parent and exit the loop for (I = 1; I <= n; I ++) {if (ht [I]. parent = 0) {min = I; break ;}// find the minimum no-parent node for (I = 1; I <= n; I ++) {if (ht [I]. weight
= 1) {qz [n]. letter = (char) I; qz [n]. w = weight [I]; n ++ ;}} return n-1; // n starts counting from 1} // creates the void CrtHuffmanTree () {int I, s1, s2, m = 2 * T-1; // * initialize * // for (I = 1; I <= t; I ++) {// 1st to n locations place n leaf nodes ht [I]. weight = qz [I]. w; ht [I]. parent = ht [I]. LChild = ht [I]. RChild = 0 ;}for (I = t + 1; I <= m; I ++) {// place a non-leaf node ht [I] From n + 1 to m. weight = ht [I]. parent = ht [I]. LChild = ht [I]. RChild = 0;} // * Create * // for (I = t + 1; I <= m; I ++) {select (I-1, & s1, & s2); // pri Ntf ("s1 = % d, s2 = % d \ n", s1, s2); ht [I]. weight = ht [s1]. weight + ht [s2]. weight; ht [s1]. parent = ht [s2]. parent = I; ht [I]. LChild = s1; ht [I]. RChild = s2 ;}/// user code void Encoding () {if (ht [0]. weight = 0) {printf ("Wow !! Not initialized !! \ N "); Sleep (2000); // return;} int I, start, c, p; char * cd; cd = (char *) malloc (t * sizeof (char); cd [T-1] = '\ 0'; for (I = 1; I <= t; I ++) {// encode n leaf nodes start = T-1; // locate the last c = I in the temporary encoding array; // record the current node position p = ht [I]. parent; // record the parent location of the current node while (p! = 0) {-- start; if (ht [p]. LChild = c) // If the node is the left child of its parent, it is encoded as 0cd [start] = '0 '; else // encode 1cd [start] = '1'; c = p; // the preparation condition for the next Loop p = ht [p]. parent;} qz [I]. code = (char *) malloc (t-start) * sizeof (char); strcpy (qz [I]. code, & cd [start]);} free (cd); // code above/* test code for (I = 1; I <= n; I ++) printf ("% c % d % s \ n", hc [I]. letter, hc [I]. w, hc [I]. code); * // write the code to the file WriteCode ();/* for (I = 1; I <= n; I ++) {printf ("% s \ n", hc [I]. code);} */qz [0]. w = 1; // mark to code pr Intf ("Yeah! Encoding is successful! \ N ") ;}// encode the write function void WriteCode () {FILE * fp_code, * fp_text; char ch; int I; // open the encoding storage file if (fp_code = fopen ("F: \ CodeFile.txt", "w") = NULL) {printf ("open CodeFile.txt -- file error! \ N "); exit (0);} // else printf (" open CodeFile.txt -- file success! \ N "); // open the text file to be encoded if (fp_text = fopen (" F: \ ToBeTran.txt "," r ") = NULL) {printf ("open ToBeTran.txt -- file error! \ N "); exit (0);} // else printf (" open ToBeTran.txt -- file success! \ N "); while (! Feof (fp_text) {ch = fgetc (fp_text); // printf ("% c", ch); I = find_letter (ch ); // printf ("I = % d \ n", I); if (I! = 0) fprintf (fp_code, "% s", qz [I]. code);} // close the file if (fclose (fp_code) {printf ("close CodeFile.txt -- file error! \ N "); exit (0);} // else printf (" close CodeFile.txt -- file success! \ N "); if (fclose (fp_text) {printf (" close ToBeTran.txt -- file error! \ N "); exit (0);} // else printf (" close ToBeTran.txt -- file success! \ N ") ;}// query the character int find_letter (char ch) {int low, high, I; low = 1; high = t; // binary search while (high-low> = 0) {I = (low + high)/2; if (qz [I]. letter = ch) return I; else if (qz [I]. letter <ch) {low = I + 1;} elsehigh = I-1;} return 0;} // Print the node weight of the Harman tree void Print () {if (ht [0]. weight = 0) {printf ("Wow !! Not initialized! \ N "); Sleep (2000); // return;} if (qz [0]. w = 0) {printf (" Wow !! No code !! \ N "); Sleep (2000); return;} int I = 0; char code [100]; FILE * fp_r, * fp_w; if (fp_r = fopen ("F: \ CodeFile.txt", "r") = NULL) {printf ("open CodeFile.txt -- file error! \ N "); exit (0);} // else printf (" open CodeFile.txt success! \ N "); if (fp_w = fopen (" F: \ CodePrint.txt "," w ") = NULL) {printf (" open CodePrint.txt -- file error! \ N "); exit (0);} // else printf (" open CodePrint.txt success! \ N "); while (! Feof (fp_r) {fscanf (fp_r, "% s \ n", code); printf ("% s", code); fprintf (fp_w, "% s ", code); I ++; if (I % 5 = 0) {printf ("\ n"); fprintf (fp_w, "\ n ");}} printf ("Yeah! Print it! \ N ") ;}// Decoding void Decoding () {if (ht [0]. weight = 0) {printf (" Wow !! Not initialized! \ N "); Sleep (2000); // return;} if (qz [0]. w = 0) {printf (" Wow !! No code !! \ N "); Sleep (2000); // return;} char code [100]; FILE * fp_r, * fp_w; int I; // open the codefile.txt FILE, read from the encoding if (fp_r = fopen ("F: \ CodeFile.txt", "r") = NULL) {printf ("open CodeFile.txt -- file error! \ N "); exit (0);} // else printf (" open CodeFile.txt success! \ N "); // open the TextFile.txt file and store the translation content if (fp_w = fopen (" F: \ TextFile.txt "," w ") = NULL) {printf ("open TextFile.txt -- file error! \ N "); exit (0);} // else printf (" open TextFile.txt success! \ N "); while (! Feof (fp_r) {fscanf (fp_r, "% s \ n", code); I = find_code (code); if (I! = 0) fprintf (fp_w, "% c", qz [I]. letter);} if (fclose (fp_r) {printf ("close CodeFile.txt -- file error! \ N "); exit (0);} // else printf (" close CodeFile.txt -- file success! \ N "); if (fclose (fp_w) {printf (" close TextFile.txt -- file error! \ N "); exit (0);} // else printf (" close TextFile.txt -- file success! \ N "); printf (" Yeah! 'Decode' is successful! \ N ");} int find_code (char s []) // the query encoding {int I; for (I = 1; I <= t; I ++) {if (strcmp (qz [I]. code, s) = 0) return I;} return 0;} void TreePrinting () {if (ht [0]. weight = 0) {printf ("Wow !! Not initialized! \ N "); Sleep (2000); // return;} FILE * fp; int I, r = t * 2-1; // open the file if (fp = fopen ("F: \ TreePrint.txt", "w") = NULL) {printf ("open CodeFile.txt -- file error! \ N "); exit (0) ;}for (I = 1; I <= r; I ++) {if (ht [I]. parent = 0) break;} // printf ("% d \ n", ht [I]. parent); _ print (fp, ht [I], 0); if (fclose (fp) {printf ("close hfmTree.txt -- file error! \ N "); exit (0);} printf (" Yeah! Print it! \ N ");} // display and write the FILE void _ print (FILE * fp, node hft, int n) in the portrait direction of the hft tree {if (hft. LChild = 0 & hft. RChild = 0) {for (int I = 0; I