| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
问题出现在哈夫曼码那个函数里 当结构体定义函数指针时而且输入的参数也是另一个结构体 这里要怎么做?其他函数都能运行 就这里不会 
 
 
 
 
 
#include <stdlib.h> 
#include <stdio.h> 
#include <string.h> 
 
 
// 
struct codage{                          //编码的结构体里有一个字母和一个字母出现的频率 
    char symbole[100]; 
    int freq; 
}; 
 
typedef struct codage codage; 
 
 
// 
struct noeud{                          //树的结构体有字符串,左枝点 右枝点 父枝点 
    char symbole[100]; 
    struct noeud *fils_g;         //左枝点 
    struct noeud *fils_d;         //右枝点 
    struct noeud *pere;         //父枝点 
}; 
 
typedef struct noeud noeud; 
 
 
void frequence(int *freq,char *chaine)     //此函数是为了找出字符串里每个ASCII字符的频率 
{ 
    char car; 
    int i; 
    int length; 
     
    // 计算字符串的长度 
    length=strlen(chaine); 
     
    //建立一个表格 现在频率全是0 
    for(i=0;i<256;i++) 
    { 
        freq[i]=0; 
    } 
     
    // 计算字符串里每个字母的频率 
    for(i=0;i<length;i++) 
    { 
        car=chaine[i]; 
        freq[car]++; 
    } 
     
    return; 
} 
 
 
void  tri_bulle(codage *tab_codage, int length)      //排序函数 
{ 
    int u; 
    char s[100]; 
    int i,j; 
     
    for(i=0;i<length;i++) 
    { 
        for(j=0;j<length-1;j++) 
        { 
            if( tab_codage[j].freq < tab_codage[j+1].freq ) 
            { 
                // on permute tab_codage[j] et tab_codage[j+1] 
                // symbole 
                strcpy(s,tab_codage[j].symbole); 
                strcpy(tab_codage[j].symbole,tab_codage[j+1].symbole); 
                strcpy(tab_codage[j+1].symbole,s); 
                 
                //freq 
                u=tab_codage[j].freq; 
                tab_codage[j].freq=tab_codage[j+1].freq; 
                tab_codage[j+1].freq=u; 
            } 
        } 
    } 
    return; 
} 
 
 
int init_tab_codage(codage *tab_codage,int *freq)   //使用频率>0的字母填入表格里并且以降序排序 
{ 
    int nb=0,i,j,length; 
     
    //我们在tab_codage这个列表中插入每个频率大于0的字符 
     
    j=0; 
    for(i=0;i<256;i++) 
    { 
        if(freq[i] > 0) 
        { 
            tab_codage[j].freq=freq[i]; 
            tab_codage[j].symbole[0]=(char)i; 
            tab_codage[j].symbole[1]='\0'; 
            j++; 
        } 
    } 
     
    length=j; 
    // on affiche le contenu de tab_codage 
    for(i=0;i<length;i++) 
    { 
        printf("caractère \"%s\", freq=%d\n",tab_codage[i].symbole,tab_codage[i].freq); 
    } 
     
    // 进行表格排序 
    tri_bulle(tab_codage, length); 
     
    // 显示已经排好序的表格 
    for(i=0;i<length;i++) 
    { 
        printf("caractère \"%s\", freq=%d\n",tab_codage[i].symbole,tab_codage[i].freq); 
    } 
     
    return(length); 
} 
 
 
 
noeud *trouve_noeud(noeud *racine,char *symbole)   //这个函数, 用于搜索包含符号树的叶 
{ 
    noeud *t; 
    t=racine; 
    int compteur=0; 
     
    while( (t != NULL) ) 
    { 
         
        if( strcmp(t->symbole,symbole) == 0) 
        { 
            return(t); 
        } 
        else if( (t->fils_g != NULL ) && ( strstr((t->fils_g)->symbole,symbole) != NULL) ) 
        { 
             
            t=t->fils_g; 
        } 
        else if( (t->fils_d != NULL ) && ( strstr((t->fils_d)->symbole,symbole) != NULL) ) 
        { 
             
            t=t->fils_d; 
        } 
        else 
            return(NULL); 
    } 
     
} 
 
 
 
noeud *huffman_code(codage *source,  int N)       //这里弄不太懂  这个是结构体定义函数指针 还有输入的参数也是另一个结构体 
{ 
     
    codage *t; 
    t=source; 
    int i; 
    int a[10]; 
    if (t[i].symbole !=NULL) 
    { 
        if(t[i].fils_g==NULL&&t[i].fils_d==NULL) 
        { 
            int i; 
             
            for(i=0;i<N;i++) 
                printf("%s",t[i]); 
            printf("\n"); 
        } 
        else 
        { 
            a[N]=0; 
            huffman_code(t[i].fils_g,N+1); 
            a[N]=1; 
            huffman_code(t[i].fils_d,N+1); 
        } 
    } 
     
} 
 
int main() 
{ 
     
     
    char *chaine="aaababaaababbbbbbbbbbbbabcddddbcaaabbbbbbbbbbbabbaa"; 
     
    int freq[256]; 
    codage source[256]; 
    int length; 
    noeud *racine; 
     
    frequence(freq,chaine); 
    length=init_tab_codage(source,freq); 
    racine=huffman_code(source,length); 
     
    return(EXIT_SUCCESS); 
}
好了 
- #include <stdio.h>
 
 - #include <stdlib.h>
 
 - #include <string.h>
 
  
- #define DATA_SIZE        256
 
 - #define CODE_TABLE_SIZE        256
 
  
- typedef struct noeud_tag
 
 - {
 
 -         char ch;
 
 -         size_t freq;
 
 -         struct noeud_tag *fils_g;        // 左枝点
 
 -         struct noeud_tag *fils_d;        // 右枝点
 
 - } noeud_t;
 
  
- typedef struct
 
 - {
 
 -         char code[100];
 
 - } code_item_t;
 
  
- // 能不改的函数尽量不改
 
 - void frequence(int *freq, char *chaine)                // 此函数是为了找出字符串里每个ASCII字符的频率
 
 - {
 
 -         char car;
 
 -         int i;
 
 -         int length;
 
  
-         // 计算字符串的长度
 
 -         length = strlen(chaine);
 
  
-         //建立一个表格 现在频率全是0
 
 -         for(i = 0; i < 256; i++)
 
 -         {
 
 -                 freq[i] = 0;
 
 -         }
 
  
-         // 计算字符串里每个字母的频率
 
 -         for(i = 0; i < length; i++)
 
 -         {
 
 -                 car = chaine[i];
 
 -                 freq[car]++;
 
 -         }
 
  
-         return;
 
 - }
 
  
- // 重写此函数
 
 - size_t init_noeud(noeud_t *data, int *freq)
 
 - {
 
 -         size_t index = 0;
 
 -         for(size_t i = 0; i < DATA_SIZE; ++i)
 
 -         {
 
 -                 if(freq[i] > 0)
 
 -                 {
 
 -                         memset(&data[index], 0, sizeof(data[index]));
 
 -                         data[index].ch = (char)i;
 
 -                         data[index].freq = freq[i];
 
 -                         ++index;
 
 -                 }
 
 -         }
 
  
-         return index;
 
 - }
 
  
- // 按频率从小到大排序
 
 - void sort(noeud_t *data, size_t length)
 
 - {
 
 -         for(size_t i = 0; i < length; ++i)
 
 -         {
 
 -                 for(size_t j = i + 1; j < length; ++j)
 
 -                 {
 
 -                         if(data[i].freq > data[j].freq)
 
 -                         {
 
 -                                 noeud_t tmp = data[i];
 
 -                                 data[i] = data[j];
 
 -                                 data[j] = tmp;
 
 -                         }
 
 -                 }
 
 -         }
 
 - }
 
  
- noeud_t *huffman_tree(noeud_t *data, size_t length)
 
 - {
 
 -         size_t index = DATA_SIZE;        // 指向最后一个可用位置
 
 -         while(length != 1)
 
 -         {
 
 -                 sort(data, length);
 
  
-                 data[--index] = data[0];
 
 -                 data[--index] = data[1];
 
  
-                 memset(&data[0], 0, sizeof(data[0]));
 
 -                 data[0].fils_g = &data[index + 1];
 
 -                 data[0].fils_d = &data[index];
 
 -                 data[0].freq = data[0].fils_g->freq + data[0].fils_d->freq;
 
  
-                 for(size_t i = 1; i < length - 1; ++i)
 
 -                         data[i] = data[i + 1];
 
 -                 --length;
 
 -         }
 
 -         return &data[0];
 
 - }
 
  
- void preorder_traversal(noeud_t *root)
 
 - {
 
 -         if(root)
 
 -         {
 
 -                 printf("\nch:\t\'%c\'\n", root->ch);
 
 -                 printf("pre:\t%u\n", root->freq);
 
 -                 preorder_traversal(root->fils_g);
 
 -                 preorder_traversal(root->fils_d);
 
 -         }
 
 - }
 
  
- void traverse_tree(noeud_t *root, code_item_t *code_table, char code[1024], size_t index)
 
 - {
 
 -         if(!root->fils_g && !root->fils_d)
 
 -         {
 
 -                 code[index] = '\0';
 
 -                 strcpy(code_table[root->ch].code, code);
 
 -         }
 
 -         if(root->fils_g)
 
 -         {
 
 -                 code[index] = '0';
 
 -                 traverse_tree(root->fils_g, code_table, code, index + 1);
 
 -         }
 
 -         if(root->fils_d)
 
 -         {
 
 -                 code[index] = '1';
 
 -                 traverse_tree(root->fils_d, code_table, code, index + 1);
 
 -         }
 
 - }
 
  
- void init_code_table(code_item_t *code_table, noeud_t *code_tree)
 
 - {
 
 -         char code[1024];
 
 -         size_t index = 0;
 
 -         memset(code_table, 0, sizeof(code_item_t) * CODE_TABLE_SIZE);
 
 -         traverse_tree(code_tree, code_table, code, index);
 
 - }
 
  
- // 尽量保证和原来一样,这里也使用数组
 
 - // 说真的,数组很不好用
 
 - // 为了和原来尽量一样,不得不用
 
 - int main(void)
 
 - {
 
 -         char *chaine = "aaababaaababbbbbbbbbbbbabcddddbcaaabbbbbbbbbbbabbaa";
 
 -         int freq[256];
 
 -         size_t length;
 
 -         noeud_t data[DATA_SIZE];
 
 -         noeud_t *code_tree;
 
 -         code_item_t code_table[CODE_TABLE_SIZE];
 
  
-         frequence(freq, chaine);
 
 -         length = init_noeud(data, freq);
 
 -         code_tree = huffman_tree(data, length);
 
 -         init_code_table(code_table, code_tree);
 
  
-         printf("a: %s\n", code_table['a'].code);
 
 -         printf("b: %s\n", code_table['b'].code);
 
 -         printf("c: %s\n", code_table['c'].code);
 
 -         printf("d: %s\n", code_table['d'].code);
 
  
-         preorder_traversal(code_tree);
 
 -         return(EXIT_SUCCESS);
 
 - }
 
 
  复制代码 
- a: 01
 
 - b: 1
 
 - c: 000
 
 - d: 001
 
  
- ch:     ' '
 
 - pre:    51
 
  
- ch:     ' '
 
 - pre:    21
 
  
- ch:     ' '
 
 - pre:    6
 
  
- ch:     'c'
 
 - pre:    2
 
  
- ch:     'd'
 
 - pre:    4
 
  
- ch:     'a'
 
 - pre:    15
 
  
- ch:     'b'
 
 - pre:    30
 
 - 请按任意键继续. . .
 
  复制代码 
(, 下载次数: 0)
 
 
 
 |   
 
 
 
 |