Home > Back-end >  Great god help to look at, look not to understand a program
Great god help to look at, look not to understand a program

Time:09-19

A Huffman code program, as a result of the C language foundation is bad, don't know how to input parameters, running after the great god give advice or comments, thanks!

# include "stdio.h"
# include "stdlib. H"
# include "string. H"
# include "math. H"
# define DELTA 1.0 e-6

Void sort (double *, char * *, * int, int).
Void code (double *, char * *, * int, int).

Int
The main (void)
{
Float * p_i, * p;
Float sum;
Float temp;
Char * * c;
Int * independence idx;
int q;
int i;

The fscanf (stdin, "% d", & amp; Q);
Independence idx=(int *) calloc (q, sizeof (int));
P_i=(float *) calloc (q, sizeof (float).
P=(float *) calloc (q, sizeof (float).
C=(char * *) calloc (q, sizeof (char *));

for(i=0; i{
[I]=c (char *) calloc (1, sizeof (char));
C [I] [0]='\ 0';
}

Sum=0.0;
for(i=0; i{
Fscanf (stdin, "& amp; F ", & amp; P [I]);
P_i [I]=p [I];
Independence idx [I]=I;
The sum +=p_i [I];
}

If (fabs (sum - 1.0) & gt; DELTA)
{
Fprintf (stderr, "p_i error \ n");
exit(-1);
}

Code (p_i, c, independence idx, q);

for(i=0; i{
Fprintf (stdout, "% d %. 6 f % s \ n", independence idx [I], p [independence idx [I]], c [independence idx [I]]).
}

For (I=q; I> 0; - I)
{
Free (c [I]);
}
Free (c);
free(p);
Free (p_i);
Free (independence idx);
exit(0);
}

Void sort (double * p, char * * c, independence idx int * and an int q)
{
Int finish=0;
Int I, j;
Int l1 and l2;
Char * s;
Float t;

While (i{
Finish=1;
for(j=0; J{
If (p [j]

{
T=p [j];
P=p [j] [j + 1);
P [m + 1]=t;
L1=independence idx [j];
Independence idx=independence idx [j] [j + 1);

Independence idx [j + 1)=l1;
L1=strlen (c [j]);
L2=strlen (c [j + 1));
S=(char *) calloc (l1 + 1, sizeof (char));
Strcpy (s, c [j]);
Realloc (c [j], l2 + 1);
Strcpy (c [j], c [j + 1));
Realloc (c [j + 1), l1 + 1);
Strcpy (c [j + 1), s);
Free (s);
}
}
i++;
}
}

Void code (double * p, char * * c, independence idx int * and an int q)
{
Int l1 and l2;
Char * s;
If (q==2)
{
Realloc (c [0], 3);
Realloc (c [1], 3);
Strcat (c [0], "0");
Strcat (c [1], "1");
}
The else
{
P=p [2] q - [q - 1) + p (q - 2),
Sort (p, c, independence idx, q - 1);
Code (p, c, independence idx, q - 1);
L1=strlen (c/q - 2);
L2=strlen (c/q - 1);
S=(char *) calloc (l1 + 2, sizeof (char *));
Strcpy (s, c/q - 2);
Realloc (c [2] q -, l1 + 2);
Strcat (c [2] q -, "0");
Realloc (c/q - 1, l2 + 2);
Strcat (s, "1");
Strcpy (c [q - 1), s);
Free (s);
}
}

CodePudding user response:

The fscanf (stdin, "% d", & amp; Q);
From the console input, also can on the command line input:
Exe program name [.] Redirected to the file input, convenient run multiple times, the input data is input in advance. The TXT
[. Exe] part can not lose
Command line executable program if you do not know to go back to

CodePudding user response:

Can baidu know the stdin, stdout, stderr and input and output redirection,

CodePudding user response:

Ok, thank you
  • Related