2010-03-11 269 views
0

我聲明一個全局變量並在函數中使用和修改它的值。然後我想獲得這個全局變量的修改值,它有一些問題。誰能幫我?全局變量問題

#include<stdio.h> 
#include<string.h> 
#include<conio.h> 
#include<stdlib.h> 
#define MAX 10 
struct link 
{ 
      int freq; 
      char value[MAX]; 
      struct link* right; 
      struct link* left; 
}; 
typedef struct link node; 
void sort(node *[], int); 
node* create(char[], int); 
void sright(node *[], int); 
void Assign_Code(node*, int [], int); 
void Delete_Tree(node *); 


int test[720][720]; 

main() 
{ 
    node* ptr, * head; 
    int i, n, total = 0, u, c[256]; 
    char str[MAX]; 
    node* a[256]; 
    int freq; 

    printf( "Huffman Algorithm\n"); 
    printf("\nEnter the no. of letter to be coded:"); 
    /*input the no. of letters*/ 
    scanf("%d", &n); 
    for (i = 0; i < n; i++) 
    { 
     printf("Enter the letter & frequency:"); 
     /*input the letter & frequency*/ 
     scanf("%s %d", str, &freq); 
     a[i] = create(str, freq); 
    } 
    while (n > 1) 
    { 
     sort(a, n); 
     u = a[0]->freq + a[1]->freq; 
     strcpy(str,a[0]->value); 
     strcat(str,a[1]->value); 
     ptr = create(str, u); 
     ptr->right = a[1]; 
     ptr->left = a[0]; 
     a[0] = ptr; 
     sright(a, n); 
     n--; 
    } 

    Assign_Code(a[0], c, 0); 
    //getch(); 
    printf("Code: "); 
     for (i = 1; i <= n; i++) 
     { 
      printf("%d", test[0][i]); 
     } 
     printf("\n"); 

    Delete_Tree(a[0]); 


} 

node* create(char a[], int x) 
{ 
    node* ptr; 
    ptr = (node *) malloc(256*sizeof(node)); 
    ptr->freq = x; 
    strcpy(ptr->value , a); 
    ptr->right = ptr->left = NULL; 
    return(ptr); 
} 
void sort(node* a[], int n) 
{ 
    int i, j; 
    node* temp; 
    for (i = 0; i < n - 1; i++) 
     for (j = i; j < n; j++) 
      if (a[i]->freq > a[j]->freq) 
      { 
       temp = a[i]; 
       a[i] = a[j]; 
       a[j] = temp; 
      } 
} 
void sright(node* a[], int n) 
{ 
    int i; 
    for (i = 1; i < n - 1; i++) 
     a[i] = a[i + 1]; 
} 
void Assign_Code(node* tree, int c[], int n) 
{ 
    int i; 
    if ((tree->left == NULL) && (tree->right == NULL)) 
    { 
     printf("%s code: ", tree->value); 
     test[0][0]=tree->value; 
     for (i = 0; i < n; i++) 
     { 
      test[0][i+1]=c[i]; 
      printf("%d", c[i]); 
     } 
     printf("\n"); 

    } 
    else 
    { 
     c[n] = 1; 
     n++; 
     Assign_Code(tree->left, c, n); 
       c[n - 1] = 0; 
     Assign_Code(tree->right, c, n); 
    } 
} 
void Delete_Tree(node * root) 
{ 
    if(root!=NULL) 
    { 
     Delete_Tree(root->left); 
     Delete_Tree(root->right); 
     free(root); 
    } 
} 
+3

什麼問題? _ – kennytm 2010-03-11 08:41:46

+3

我們也可以猜測這個問題嗎?你知道,這裏的人只是_love_猜測不完整和模糊的問題...... – 2010-03-11 08:42:04

+0

在main中,我想打印出全局變量的值,但結果爲零。 – user239468 2010-03-11 08:45:23

回答

0

作爲附帶說明:

node* create(char a[], int x) 
{ 
    node* ptr; 
    ptr = (node *) malloc(256*sizeof(node)); // <--- This is wrong 
    ptr->freq = x; 
    strcpy(ptr->value , a); 
    ptr->right = ptr->left = NULL; 
    return(ptr); 
} 

沒有理由來分配節點的大小256倍來存儲一個節點。您正在創建一個節點並將其存儲在指向節點的指針數組中。在此處分配一個節點,如下所示:

malloc (sizeof (node)); or malloc ((sizeof (*ptr)); 
3

我要強調的問題:

while (n > 1) 
{ 
    ... 
    n--; 
} 
... 
for (i = 1; i <= n; i++) 
{ 
    printf("%d", test[0][i]); 
} 

通過第二循環開始n是一個,而printf只執行一次,讓您打印的test[0][1]只有該值的時間。

test[0][1]值在Assign_Code覆蓋多次(多達樹的葉節點的數目):現在

void Assign_Code(node* tree, int c[], int n) 
{ 
    if ((tree->left == NULL) && (tree->right == NULL)) 
    { 
     ... 
     for (i = 0; i < n; i++) 
     { 
      test[0][i+1]=c[i]; 
     } 
    } 
    ... 
} 

,因爲你現在的樣子遍歷樹最後一次test[0][1]被覆蓋的是具有'0'作爲第一個字符的霍夫曼碼。

+0

但我用一個整數替換n,就像3 輸出是0,這是不正確的。 – user239468 2010-03-11 09:05:29

0
you can store the n value to some temporary variables, after you get the 

值。

Then use the temporary variable in your for loop condition. 
scanf("%d", &n); 
int temp = n ; 
for (i = 1; i <= temp ; i++) 
{ 
    printf("%d", test[0][i]); 
}