2014-11-02 93 views
0

我試圖編寫一個編碼和解碼目的的代碼。一旦我們有字典,形成的字典就存儲在一個結構數組中。這種結構在下面c中的字符串與字符數組的比較

typedef struct Tuple { 
    char a; 
    char* cod; 
} result; 

原始字符串是字符數組

char str[100]; 

現在我們需要一個方法的字符的原始陣列與所形成的辭典中進行比較。 形成的詞典是這樣

a ---0 
b ---1 
c ---01 

實施例:原始的字符串是aabcab,那麼編碼應0010101

的字符串與字典數據如下比較的代碼,但是當該碼是執行它的結果如下:[Warning]傳遞'strcmp'的參數2使得整型指針沒有一個強制轉換[缺省情況下啓用]。

幫助將不勝感激。

for(i=0; i<strlen(str);i++)//read the original string; 
    { 
     j=0; 
     while(j<number_of_elements_in_dictionary)// for above example=3 
     { 
      if (strcmp(str[i],values[j]->a)==0) //compare original string character with the //dictionary 
      { 
       printf("%s", values[j]->cod);//print corresponding code from //dictionary 
       j++; //check with the next value of the dictionary 
      } 
     }   
    } 
    printf("last=%s", str_en);//To print the dictionary data corresponding to //the original string data 

#include<string.h> 
 
#include<stdio.h> 
 
#include<limits.h> 
 
#include<stdlib.h> 
 
typedef struct node 
 
{ 
 
     char ch; 
 
     int freq; 
 
     struct node *left; 
 
     struct node *right; 
 
}node; 
 

 
typedef struct Tuple { 
 
    char a; 
 
    char* cod; 
 
}result; 
 
/*Declaring heap globally so that we do not need to pass it as an argument every time*/ 
 
/* Heap implemented here is Min Heap */ 
 
node * heap[1000000]; 
 
result * values[200]; 
 
int heapSize; 
 
\t char * str; 
 
\t char str_en[100]; 
 
// str_en[0] = '\0'; 
 
/*Initialize Heap*/ 
 
void Init() 
 
{ 
 
     heapSize = 0; 
 
     heap[0] = (node *)malloc(sizeof(node)); 
 
     heap[0]->freq = -INT_MAX; 
 
} 
 
/*Insert an element into the heap */ 
 
void Insert(node * element) 
 
{ 
 
     heapSize++; 
 
     heap[heapSize] = element; /*Insert in the last place*/ 
 
     /*Adjust its position*/ 
 
     int now = heapSize; 
 
     while(heap[now/2] -> freq >= element -> freq) 
 
     { 
 
       heap[now] = heap[now/2]; 
 
       now /= 2; 
 
     } 
 
     heap[now] = element; 
 
} 
 
node * DeleteMin() 
 
{ 
 
     /* heap[1] is #ifndef 
 

 
#elif 
 

 
#endifthe minimum element. So we remove heap[1]. Size of the heap is decreased. 
 
      Now heap[1] has to be filled. We put the last element in its place and see if it fits. 
 
      If it does not fit, take minimum element among both its children and replaces parent with it. 
 
      Again See if the last element fits in that place.*/ 
 
     node * minElement,*lastElement; 
 
     int child,now; 
 
     minElement = heap[1]; 
 
     lastElement = heap[heapSize--]; 
 
     /* now refers to the index at which we are now */ 
 
     for(now = 1; now*2 <= heapSize ;now = child) 
 
     { 
 
       /* child is the index of the element which is minimum among both the children */ 
 
       /* Indexes of children are i*2 and i*2 + 1*/ 
 
       child = now*2; 
 
       /*child!=heapSize beacuse heap[heapSize+1] does not exist, which means it has only one 
 
        child */ 
 
       if(child != heapSize && heap[child+1]->freq < heap[child] -> freq) 
 
       { 
 
         child++; 
 
       } 
 
       /* To check if the last element fits ot not it suffices to check if the last element 
 
        is less than the minimum element among both the children*/ 
 
       if(lastElement -> freq > heap[child] -> freq) 
 
       { 
 
         heap[now] = heap[child]; 
 
       } 
 
       else /* It fits there */ 
 
       { 
 
         break; 
 
       } 
 
     } 
 
     heap[now] = lastElement; 
 
     return minElement; 
 
} 
 
void encode(result *value, int s) 
 
{ 
 
int pos,i,j; 
 
pos=1; 
 
\t values[pos]=value;//Im here 
 
\t values[pos]->a =value->a; 
 
\t values[pos]->cod=value->cod; 
 
\t     
 
       printf("RESULT= %c and %s", values[pos]->a, values[pos]->cod); 
 
     
 
       pos++; 
 
       
 
      /*the problem exists here while executing the following for-loop, the code doesn't execute due to this for loop*/ 
 
      
 
       for(i=0; i<strlen(str);i++){ 
 
       \t j=0; 
 
       \t while(j<4) 
 
\t \t \t \t { 
 
\t \t \t \t  \t if(str[i]==values[j]->a) 
 
\t \t \t \t  \t { 
 
\t \t \t \t  \t \t printf("%s", values[j]->cod); 
 
\t \t \t \t  \t \t j++; 
 
\t \t \t \t \t } 
 
\t \t \t \t } \t \t } 
 
\t \t \t \t  
 
\t \t \t \t printf("last=%s", str_en); 
 
\t \t \t \t 
 
    
 
    } 
 
void print(node *temp,char *code, int s)//, char *buf) 
 
{ 
 
\t 
 
\t int i,pos=1,j; 
 
     if(temp->left==NULL && temp->right==NULL) 
 
     { 
 
       printf("\n\nchar %c code %s\n",temp->ch,code); 
 
       result * value = (result *) malloc(sizeof(result)); 
 
       value->a=temp->ch; 
 
       value->cod= code; 
 
       encode(value,s); 
 
       
 
\t \t return; 
 
      
 
       
 
     } 
 
     int length = strlen(code); 
 
     char leftcode[512],rightcode[512]; 
 
     strcpy(leftcode,code); 
 
     strcpy(rightcode,code); 
 
     leftcode[length] = '0'; 
 
     leftcode[length+1] = '\0'; 
 
     rightcode[length] = '1'; 
 
     rightcode[length+1] = '\0'; 
 
     print(temp->right,rightcode,s); 
 
     print(temp->left,leftcode,s); 
 
     
 
    }  
 

 
/* Given the list of characters along with their frequencies, our goal is to predict the encoding of the 
 
    characters such that total length of message when encoded becomes minimum */ 
 
int main() 
 
{ 
 
\t char buf[250]; 
 

 
\t char character[26]; 
 
\t int i = 0,j=0,count[26]={0}; 
 
    char c = 97; 
 
     Init(); 
 
     int distinct_char=0 ; 
 
     
 
     char ch; 
 
     int freq;  
 
     int iter; 
 
    
 
     printf("enter the string"); 
 
     scanf("%s", str); 
 
     printf("string=%s",str); 
 
     for (i=0; i<strlen(str);i++) 
 
     { 
 
     \t 
 
     for(j=0;j<26;j++) 
 
      { 
 
      if (tolower(str[i]) == (c+j)) 
 
       { 
 
        count[j]++; 
 
       } 
 
     } 
 
    } 
 
    for(j=0;j<26;j++) 
 
     { 
 
\t \t \t if(count[j]>0) 
 
\t \t \t { 
 

 
      printf("\n%c -> %d",97+j,count[j]); 
 
      distinct_char++; 
 
      character[j] = 97+j;  
 
\t \t \t } 
 

 
    \t } 
 
    \t printf("\n number of distinct_characters=%d\n", distinct_char); 
 
\t 
 
\t 
 
\t  if(distinct_char==1) 
 
     { 
 
       printf("char %c code 0\n",c); 
 
       return 0; 
 
     } 
 
     
 
     for(j=0;j<distinct_char;j++) 
 
     { 
 
     \t printf("\ncharacter= %c and the frequency=%d", character[j],count[j]); 
 
     \t node * temp = (node *) malloc(sizeof(node)); 
 
       temp -> ch = character[j]; 
 
       temp -> freq = count[j]; 
 
       temp -> left = temp -> right = NULL; 
 
       Insert(temp); 
 
      
 
     } 
 
     for(i=0;i<distinct_char-1 ;i++) 
 
     { 
 
       node * left = DeleteMin(); 
 
       node * right = DeleteMin(); 
 
       node * temp = (node *) malloc(sizeof(node)); 
 
       temp -> ch = 0; 
 
       temp -> left = left; 
 
       temp -> right = right; 
 
       temp -> freq = left->freq + right -> freq; 
 
       Insert(temp); 
 
     } 
 
     node *tree = DeleteMin(); 
 
     
 
     
 
     char code[512]; 
 
     code[0] = '\0'; 
 
    
 
    print(tree,code, distinct_char); 
 
    
 

 
}

+2

沒有'strcmp'在你的代碼顯示我們確切的代碼 – Rohan 2014-11-02 07:46:17

+1

@vmp請提供生成的代碼。 。警告我不在代碼看到'strcmp' – Pradhan 2014-11-02 07:46:23

+0

而(j <4) \t \t { \t \t \t如果(的strcmp(STR [I],值[J] - >。一個)== 0 ) \t \t \t \t { \t \t \t \t printf(「%s」,values [j] - > cod); \t \t \t \t \t \t j ++; \t \t \t \t \t} \t \t \t \t} – vmp 2014-11-02 07:49:58

回答

1

作爲警告提示,你在這裏失蹤的指針。 strcmp的簽名讀取

int strcmp(const char *s1, const char *s2); 

但兩者參數實際上char型的(陣列索引使得charchar*,就像常規解除引用不和所述第二參數是char反正)。

但是,您真正需要的是將字符串中的單個字符與另一個字符進行比較。你可以只使用常規關係運算符:

if(str[i] == values[j]->a) 
{ 
    // ... 
} 

請注意,這只是回答您的精確的問題,但你的代碼可能是錯誤的或無效的反正。

+0

試圖用,但代碼不工作 – vmp 2014-11-02 08:04:53

+0

正如我所說的。但是「不起作用」是什麼意思?任何錯誤/警告? – Jan 2014-11-02 08:06:12

+0

這段代碼做的是哈夫曼編碼。我想要編碼的字符串被打印。檢查編輯的代碼。 Im發佈整個文件 – vmp 2014-11-02 08:14:19

0

也許不是完全的「on-topic」,但是如果你使用查找表方法,你的代碼會大大改善。來自str的輸入字符應該用作帶字典數組的索引 - 那麼您只需要一個遍歷輸入字符串的循環。爲了應對這一事實char可以有256個值,在外面你只需要幾個你可以:

  • 只使用char值的一部分指標 - 實際的索引之前減去一定的價值,你希望(第一個可打印字符編碼),並將結果與​​LUT中的最大索引進行比較,浪費一些內存並在LUT中包括全部256個索引,使用另一個LUT將字符從輸入轉換爲LUT中的索引 - 這樣您就可以映射您不支持一個通用輸出元素的所有字符,
  • ...

這樣你就不會在你的代碼中的任何對比 - 只是索引(;