2010年9月12日 星期日

這個網誌拿來存code

這個網誌只拿來存有用的code

若是剛好符合大家需求

儘管拿去吧~

版權沒有,翻印不究

Huffman code(包含建立以及輸出)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct huffmannode {
   char key;
   int count;    
   struct huffmannode* left;
   struct huffmannode* right;
   struct huffmannode* next;
   int selected;
}Hnode;
FILE *f;
FILE *w;
Hnode* first=NULL;
char* s[127];
void ini_node(Hnode* );
int linklength(Hnode* );
int main(){  
    f=fopen("input.txt","r");
    char c;
    Hnode* tmp;
    Hnode* h;//traverse use
    int count[127];
    for(int i=0;i<127;i++)
       count[i]=0;
    while(fscanf(f,"%c",&c)!=EOF)
       count[c]++;
    //count[10]=0;
    for(int i=0;i<127;i++){
        //printf("[%c]",c);
        if(count[i]==0) continue;
        if(first==NULL){
            tmp=(Hnode*)malloc(sizeof(Hnode));               
            ini_node(tmp);
            tmp->key=i;
            tmp->count=count[i];
            first=tmp;
        }//end if
        else{//first!=NULL
           h=first;
           while(h!=NULL){   
              if(count[i]<=h->count && h==first){//  ->O...
                 tmp=(Hnode*)malloc(sizeof(Hnode));  
                 ini_node(tmp);
                 tmp->key=i;     
                 tmp->count=count[i];
                 tmp->next=first;
                 first=tmp;
                 break;
              }//end if
              else if(count[i]>=h->count && h->next==NULL){ //...O->
                 tmp=(Hnode*)malloc(sizeof(Hnode));  
                 ini_node(tmp);
                 tmp->key=i;     
                 tmp->count=count[i];
                 h->next=tmp; 
                 break;
              }//end else if
              else if(count[i]<=h->next->count && count[i]>=h->count){  //....O->O...
                 tmp=(Hnode*)malloc(sizeof(Hnode));  
                 ini_node(tmp);
                 tmp->key=i;     
                 tmp->count=count[i];
                 tmp->next=h->next;
                 h->next=tmp;         
                 break;        
              }
              h=h->next;          
           }//end loop 
        }                     
    }//end for
    h=first;
    /*while(h!=NULL){
       printf("[%c:%d]",h->key,h->count);   
       h=h->next;              
    }//end while
    */
    //getchar();
    while(linklength(first)>1){
       tmp=(Hnode*)malloc(sizeof(Hnode));  
       ini_node(tmp);     
       tmp->count=first->count + first->next->count;//1+2
       tmp->left=first;
       tmp->right=first->next;
       first=first->next->next;//從第三個開始搜尋
       h=first;
       while(h!=NULL){
          if(tmp->count<=h->count && h==first){
             printf("[%d]1\n",tmp->count);
             tmp->next=first;
             first=tmp;                     
             break;
          }//end if
          else if(tmp->count>=h->count && h->next==NULL){
             printf("[%d]2\n",tmp->count);
             //printf("[%d]",h->count);
             h->next=tmp;
             tmp->next=NULL;    
             break;
          }//end else if
          else if(tmp->count>=h->count && tmp->count <= h->next->count){
             printf("[%d]3\n",tmp->count);
             tmp->next=h->next;
             h->next=tmp; 
             break;
          }//end else if
          h=h->next;   
       }//end while
    }//end while
    first=tmp;
    char code[100];
    int length=0;
    //getchar();
    while(tmp->selected!=1){
       if(tmp->left==NULL && tmp->right==NULL){
            tmp->selected=1;
            code[length]='\0';
            //printf("%s",code);
            s[(tmp->key)]=(char*)malloc(length);
            strcpy(s[tmp->key],code);
            length=0;
            tmp=first;
       }//end if  
       else if(tmp->left!=NULL && tmp->left->selected==0){
           tmp=tmp->left;
           //getchar();
           code[length]='0';
           length++;    
       }//end else if          
       else if(tmp->right!=NULL && tmp->right->selected==0){
           tmp=tmp->right;
           code[length]='1';
           length++;
       }//end else if
       else if(tmp->left!=NULL && tmp->right!=NULL && tmp->left->selected==1 && tmp->right->selected==1){
           tmp->selected=1;    
           length=0;
           tmp=first;
       }//end else if
       else if(tmp->left!=NULL && tmp->left->selected==1 && tmp->right==NULL){
           tmp->selected=1;    
           length=0;
           tmp=first;
       }//end else if
       else if(tmp->right!=NULL && tmp->right->selected==1 && tmp->left==NULL){
           tmp->selected=1;    
           length=0;
           tmp=first;
       }//end else if
    }//end while
    for(int i=0;i<127;i++){
       if(s[i]!=NULL)
          printf("[%c]:%s\n",i,s[i]);
    }//end for
    rewind(f);
    w=fopen("output.txt","w");
    while(fscanf(f,"%c",&c)!=EOF){
       //getchar();
       fprintf(w,"%s",s[c]);                          
    }//end while
    fclose(w);
    w=fopen("output.txt","r");
    h=first;
    while(fscanf(w,"%c",&c)!=EOF){
       if(c=='0'){
          h=h->left;                  
          if(h->left==NULL && h->right==NULL){
             printf("%c",h->key);
             h=first;
          }
       }           
       else if(c=='1'){
          h=h->right;    
          if(h->left==NULL && h->right==NULL){
             printf("%c",h->key);
             h=first;
          }
       }  
    }//end while
    system("pause");
}//end main


void ini_node(Hnode* h){
    h->count=0;
    h->left=NULL;
    h->right=NULL;
    h->next=NULL;    
    h->selected=0;
}//end ini_node

int linklength(Hnode* h){
    int i=0;
    while(h!=NULL){           
       i++;       
       h=h->next;   
    }
    return i;
}//end linklength