Harman coding Program

Source: Internet
Author: User

By writing this program, I realized a mistake.-1 is not necessarily the file Terminator !!!! (I called it for two days...: >_< ::)

 

# Include <iostream> <br/> # include <fstream> <br/> # include <conio. h> <br/> # include <math. h> <br/> # include <time. h> <br/> using namespace std; </p> <p> long P [600], HEAD, FILESIZE = 0; </p> <p> char INPATH [255], OUTPATH [255], W [8] = {0 }; <br/> struct tree <br/> {<br/> int weight, left, right, data; <br/> }; </p> <p> tree T [600]; </p> <p> void cal () /* Statistical Character occurrence frequency */<br/> {<br/> FILE * in = fopen (INPATH, "rb"); <br/> memset (P, 0, sizeof (P); <br /> Char a = fgetc (in); <br/> while (! Feof (in) <br/>{< br/> P [a + 300] ++; <br/> FILESIZE ++; <br/> a = fgetc (in); <br/>}< br/> fclose (in ); <br/>}< br/> char code [600] [600]; </p> <p> void getbianma (int left, int now, char c, int deep) <br/>{< br/> if (deep> 0) <br/>{< br/> strcpy (code [T [now]. data + 300], code [T [left]. data + 300]); <br/> code [T [now]. data + 300] [deep-1] = c; code [T [now]. data + 300] [deep] = '/0'; <br/>}</p> <p> if (T [now]. left! =-1) <br/> getbianma (now, T [now]. left, '0', deep + 1); <br/> if (T [now]. right! =-1) <br/> getbianma (now, T [now]. right, '1', deep + 1); <br/>}</p> <p> void creatHoffman () /* Create a Haffman tree */<br/> {<br/> int I, j, n = 0, k, s; </p> <p> for (I = 0; I <600; I ++) <br/> if (P [I]> 0) <br/> {<br/> T [n]. data = char (i-300); <br/> T [n]. weight = P [I]; <br/> T [n]. left = T [n]. right =-1; <br/> n ++; <br/>}< br/> char lock [600]; <br/> memset (lock, 0, sizeof (lock); <br/> for (k = 0, s = n; k <s-1; k ++) /* start generating the Haffman tree */<br/>{< br/> int min1, min2 =-1; <br/> (I = 0; I <n; I ++) <br/> if (! Lock [I]) {min1 = I; break ;}</p> <p> for (j = I; j <n; j ++) /* Find the node with the first small weight */<br/> if (! Lock [j] & T [j]. weight <T [min1]. weight) <br/> min1 = j; <br/> for (j = I; j <n; j ++) /* Find the node with the first small weight */<br/> if (! Lock [j] & j! = Min1 & (min2 =-1 | T [j]. weight <T [min2]. weight) <br/> min2 = j; <br/>/* Find the two nodes and start merging */</p> <p> T [n]. weight = T [min1]. weight + T [min2]. weight; <br/> T [n]. data =-300; T [n]. right = min1; T [n]. left = min2; </p> <p> code [T [n]. data + 300] [0] = '/0'; <br/> lock [min1] = lock [min2] = 1; </p> <p> n ++; <br/>}< br/> HEAD = n-1; <br/> getbianma (-1, n-1, '0', 0 ); <br/>}</p> <p> void savenote ()/* Save the password book */<br/> {<br/> ofstream out (OUTPATH, ios :: binary); <br/> Out. write (char *) & FILESIZE, sizeof (long); <br/> out. write (char *) & HEAD, sizeof (int); <br/> for (int I = 0; I <= HEAD; I ++) <br/> out. write (char *) & T [I], sizeof (tree); <br/> out. close (); <br/>}</p> <p> void readnote () <br/>{< br/> ifstream in (INPATH, ios: binary ); <br/> in. read (char *) & FILESIZE, sizeof (long); <br/> in. read (char *) & HEAD, sizeof (int); <br/> for (int I = 0; I <= HEAD; I ++) <br/> in. read (char *) & T [I], siz Eof (tree); <br/> in. close (); <br/>}</p> <p> void passed () <br/>{< br/> FILE * in = fopen (INPATH, "rb"), * out = fopen (OUTPATH, "wb"); </p> <p> char a = fgetc (in ); <br/> char temp = 0; int th = 0; <br/> while (! Feof (in) <br/>{< br/> for (int I = 0; I <strlen (code [a + 300]); I ++) <br/> {<br/> if (code [a + 300] [I]-48) <br/>{< br/> temp = temp | W [th]; <br/>}< br/> th ++; <br/> if (th> 7) <br/>{< br/> th = 0; <br/> fputc (temp, out ); <br/> temp = 0; <br/>}< br/> a = fgetc (in ); <br/>}< br/> if (th> 0) <br/> fputc (temp, out); <br/> fclose (in ); <br/> fclose (out); <br/>}</p> <p> void huanyuan () <br/> {<br/> FILE * in = fopen (INPATH, "rb"), * out = fopen (OU TPATH, "wb"); </p> <p> char a = fgetc (in); <br/> int p = HEAD; <br/> while (! Feof (in) & FILESIZE) <br/>{< br/> for (int I = 0; I <8 & FILESIZE; I ++) <br/>{< br/> if (a & W [I]) <br/>{< br/> if (T [p]. right! =-1) <br/> p = T [p]. right; <br/> else <br/> {<br/> fputc (T [p]. data, out); <br/> p = HEAD; <br/> FILESIZE --; <br/> I --; <br/>}< br/> else <br/> {<br/> if (T [p]. left! =-1) <br/> p = T [p]. left; <br/> else <br/> {<br/> fputc (T [p]. data, out); <br/> p = HEAD; <br/> FILESIZE --; <br/> I --; <br/>}< br/> a = fgetc (in); <br/>}< br/> while (FILESIZE --) <br/> fputc (T [p]. data, out); <br/> fclose (in); <br/> fclose (out); <br/>}</p> <p> int main () <br/>{< br/> int t1, t2; <br/> for (int I = 0; I <8; I ++) <br/> W [I] | = (char) pow (2,7-i); <br/> while (1) <br/>{< br/> system ("cls"); </p> <p> cout <"what you are using is User-Defined compression program! /N1: Encrypted/n2: decrypted/n0: Exited "<endl; <br/> char a = getch (); <br/> FILESIZE = 0; <br/> if (a = '1') <br/>{< br/> cout <"Enter the path of the file to be compressed! (You can also drag the file to be compressed to the program window)/n "; <br/> cin> INPATH; <br/> strcpy (OUTPATH, INPATH ); <br/> strcat (OUTPATH ,". passnote "); <br/> t1 = clock (); <br/> cal (); <br/> creatHoffman (); <br/> savenote (); <br/> strcpy (strrchr (OUTPATH ,'. '),". passed "); <br/> passed (); <br/> t2 = clock (); <br/> cout <" compressed, when "<double (t2-t1)/1000 <" s, please keep the compressed file and encoding this file !. /N "; <br/>}< br/> else if (a = '2 ') <br/>{< br/> cout <"Enter the path of the file to be decompressed! (You can also drag the files to be decompressed to the program window)/n "; <br/> cin> INPATH; <br/> strcpy (strrchr (INPATH ,'. '),". passnote "); <br/> strcpy (OUTPATH, INPATH); <br/> * strrchr (OUTPATH ,'. ') ='/0'; <br/> t1 = clock (); <br/> readnote (); <br/> getbianma (-1, HEAD, '0', 0); <br/> strcpy (strrchr (INPATH ,'. '),". passed "); <br/> huanyuan (); <br/> t2 = clock (); <br/> cout <" the file has been decompressed !, When "<double (t2-t1)/1000 <" s. /n "; <br/>}< br/> else <br/> break; <br/> system (" pause "); <br/>}< br/> return 0; <br/>}< br/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.