| |

VerySource

 Forgot password?
 Register
Search
View: 698|Reply: 1

Solve the problems encountered in the programming process (1)-data redundancy, interested parties please enter ...

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-4 12:40:01
| Show all posts |Read mode
aims:
1. General: Use Huffman coding to encrypt the input text (input-> storage-> statistical frequency weighting-> encoding-> replacement-> output);
2. Points: I am currently staying in the statistics section to realize the statistics of the number of occurrences of characters in the input string (as weights, for the purpose of constructing the tree and encoding), and output them.

thought:
The string storage uses a linear sequence table L, and the array w [1000] stores weights (number of times).

problem:
During the counting process of the Counting function, each character read by default is not equal. For example: Enter 'Then what?&' (&is the terminator), then in the weight array: w [1] = w [6] = 2. This will be used to find the two nodes with the smallest weight in the array to create Huffman trees are harmful. How should the algorithm be improved to eliminate it?


Source code:
#include <stdio.h>
#include <stdlib.h>
#define Init_Size 1000
typedef struct {
char * Elem;
int len;
} Sqlist;
Initlist (Sqlist * L)
{L-> Elem = (char *) malloc (sizeof (char) * Init_Size);
  if (L-> Elem)
    L-> len = 0;
  else printf ("ERROR!\n");
  printf ("\nInitializing ... Success!\n");
}

ReadIn (Sqlist * L)
{int i = 0;
   do
    scanf ("% c",&L-> Elem [i ++]);
   while ((L-> Elem [i-1])! = '&');
  L-> len = i;
  printf ("\nREAD IN SUCCESS!\n");
}

Counting (Sqlist * L, char w [1000])
{int count, i, j;
  for (i = 0; i <(L-> len); i ++)
    {count = 0;
       for (j = 0; j <(L-> len); j ++)
if (L-> Elem [i] == L-> Elem [j]) count ++;
       w [i] = count;
      printf ("w [% d] =% d", i, w [i]);
      if ((i% 6 == 0)&&(i> 5)) printf ("\n");
     }

}

main ()
{Sqlist * L;
 char w [1000];
  Initlist (L);
  ReadIn (L);
  Counting (L, w [1000]);
}
Reply

Use magic Report

0

Threads

22

Posts

18.00

Credits

Newbie

Rank: 1

Credits
18.00

 China

Post time: 2020-1-8 14:45:01
| Show all posts
Counting (Sqlist * L, char w [1000])
{int count, i, j;
  for (i = 0; i <(L-> len); i ++)
  {
    count = 1;
    for (j = i + 1; j <(L-> len); j ++) {
      if (L-> Elem [i] == L-> Elem [j])
        count ++;
      else if (count> 1)
        L-> Elem [j-count + 1] = L-> Elem [j]; / * Each element forward count-1 * /
    }
    L-> len-= count-1;
    w [i] = count;
    printf ("w [% d] =% d", i, w [i]);
    if ((i% 6 == 0)&&(i> 5)) printf ("\n");
  }
}

I do n’t see very clearly, I do n’t know if the landlord meant this
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list