Tag:io ar sp for Data on art amp ef
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> #define N 100typedef struct {int weight;int parent, lchild, Rchild;} hafuman;typedef struct {char data[n];//character data char copy[n][10*n];//encoded}bianma;void display (); int input (int w[], Bianma *bm), void Creat_hafuman (Hafuman ht[], int w[], int n); void Select (Hafuman ht[], int m, int *s1, int *s2); void encoding (Hafuman ht[], Bianma *bm, int n); void coding (Bianma *bm, int n);//decoding void Coding_2 (Hafuman ht[], bi Anma *BM, int n);//decode void output (Bianma *bm, int n); int I, J, k;void Display () {printf ("\n\n\n");p rintf ("\t\t\t1. Output encoding \ n \ nthe" );p rintf ("\t\t\t2. decode \ n");p rintf ("\t\t\t3. exit \ n \ nthe");p rintf ("\t\t Please select (1~~3):");} int input (int w[], Bianma *bm) {int n=0;printf ("\ n Please enter the article or sentence to encode (#结束) \ n"), while (1) {Bm->data[n]=getchar (); if (bm- >data[n]== ' # ') break;n++;} for (i=0; i<n; i++) {w[i]=1;for (j=i+1; j<n;) {if (bm->data[i] = = Bm->data[j]) {w[i]++;for (k=j; k<n; k++) {bm->data[k]=bm->data[k+1];} n--; //n--after the cover is finished;} elsej++;}} printf ("\ n");p rintf ("different characters \ n"), for (i=0; i<n; i++) {printf ("%c", Bm->data[i]);} return n;} void Creat_hafuman (Hafuman ht[], int w[], int n) {int S1, s2;int t;for (t=1; t<=n; t++) {ht[t].weight=w[t-1];ht[t].parent =0;ht[t].lchild=0;ht[t].rchild=0;} for (t=n+1; t<=2*n-1; t++) {ht[t].weight=0;ht[t].parent=0;ht[t].lchild=0;ht[t].rchild=0;} for (t=n+1; t<=2*n-1; t++) {Select (HT, t-1, &S1, &S2);//Pre-I-1 selected parents are 0, the minimum weight ht[t].weight=ht[s1].weight + ht[s2]. WEIGHT;HT[T].LCHILD=S1, Ht[t].rchild=s2;ht[s1].parent=t, Ht[s2].parent=t;}} void Select (Hafuman ht[], int m, int *s1, int *s2) {int min1, min2, A, b;i=1;while (ht[i].parent! = 0) {i++;} Min1=ht[i].weight; A=i;for (j=i+1; j<=m; J + +) {if (Min1 > Ht[j].weight && ht[j].parent==0) {min1=ht[j].weight;a=j;}} I=1;while (ht[i].parent! = 0 | | a==i) {i++;} Min2=ht[i].weight;b=i;for (j=i+1; j<=m; J + +) {if (j==a) continue;if (min2 > Ht[j].weight && ht[j].parent==0 ) {min2=ht[j].weight;b=j;}} *s1=a; *s2=b;} void Encoding (Hafuman ht[], Bianma *bm, int n)//encoding, *copy[] for copy encoding {int start, C, P;char *ch;ch= (char *) malloc (n*sizeof (char)); ch[n-1]= ' \ 0 '; for (i=1; i<=n; i++)//n leaf node {start=n-1; C=i, P=ht[i].parent; P is parent, C is childwhile (p!=0) {start--;if (ht[p].lchild==c) ch[start]= ' 0 '; elsech[start]= ' 1 '; c=p; p=ht[p].parent;// printf ("\n123\n");} strcpy (Bm->copy[i-1], &ch[start]);//printf ("\n%s\n", Bm->copy[i-1]);} Free (ch);} void Coding_2 (Hafuman ht[], Bianma *bm, int n)//decoding {char s[10*n];int p;printf ("\ n Please enter the character to decode \ n"); Fflush (stdin); get (s); printf ("\ n decode \ n \ nthe");p =2*n-1;for (i=0; s[i]! = ' s[i]== '; i++) {if (' 0 ') p=ht[p].lchild;elseif (s[i]== ' 1 ') p=ht[p].rchild ; if (Ht[p].lchild = = 0 && Ht[p].rchild = = 0) {printf ("%c", Bm->data[p-1]);//p:1~~2*n-1, bbm->data[0~~n-1]p =2*n-1;continue;}} Puts ("\ n");} void output (Bianma *bm, int n) {printf ("\ n"), for (i=0; i<n; i++) {printf ("%c\t", Bm->data[i]);p rintf ("%s\n", bm- >copy[i]);}} void Main () {Hafuman Ht[n];bianma *bm;int w[n];int N, m;bm= (Bianma *) maLloc (sizeof (BIANMA)), N=input (W, BM);p rintf ("\ n \ nthe total number of different characters:%d\n\n", N); Creat_hafuman (HT, W, N); Encoding (HT, BM, n); Getch (); System ("CLS"); Loop:display (); scanf ("%d", &m), switch (m) {case 1:output (BM, N);p rintf ("\ n" Press any key to continue "); Getch (); Sy Stem ("cls"); Goto loop;break;case 2://coding (BM, n);//decoding coding_2 (HT, BM, n);//decoding printf ("\ n \ nyou Press any key to continue"); Getch (); system ("CLS"); Goto loop;break;case 3:break;default:system ("CLS"); goto Loop;}}
C Language: The encoding and decoding of Huffman tree