2012-12-11 82 views
3

我想創建一個計算器,保持算術運算的順序。我的想法是將中綴表示法轉換爲後綴表示法,以便我可以從左到右解決它而不用擔心括號。在嘗試將中綴轉換爲後綴表示法之前,我想解決一個後綴表示法練習,並嘗試使用節點來解決此問題,但我在將數字和運算符劃分爲節點時遇到問題。 我是新來的指針和結構,所有的事情都讓我困惑。爲什麼我的節點上下文不打印任何東西?

這是一種嘗試的功能來劃分的:

typedef char* String; 
typedef struct node 
{ 
    String  str; 
    struct node *next; 
} Node; 

Node *rpn_divider(String equation, int eq_size) 
{ 
    Node *rpn_parts = node_alloc(1); //pointer to first element in the node 
    Node *part_temp = rpn_parts; //pointer to the lattest element in the node 
    String temp = malloc(sizeof(char*) * NUM_SIZE); 

    int i, j; //i = string equation index, j = string temp index 

    for (i = 0, j = 0; i < eq_size; i++) 
    { 
     if (isNum(equation[i])) 
      temp[j++] = equation[i]; 
     else if (isOper(equation[i])) 
     { 
       temp[0] = equation[i]; 
       temp[1] = '\0'; 
       next_node(part_temp, temp); 
     } 
     else 
     { 
       if (temp == '\0') continue; 
       temp[j] = '\0'; 
       next_node(part_temp, temp); 
       j = 0; 
     } 
    } 
    free(part_temp->next); 
    free(temp); 
    return rpn_parts; 
} 

這裏是next_node功能:

void next_node(Node *node, String str) 
{ 
    node->str = str; 
    node->next = node_alloc(1); 
    node = node->next; 
    free(str); 
    str = malloc(sizeof(char*) * NUM_SIZE); 
    str[0] = '\0'; 
} 

,當我試圖打印節點上下文,它不會做任何東西:

Node *ptr; 
for (ptr = head; ptr != NULL; ptr = ptr->next); 
{ 
    printf("The Str = %s", ptr->str); 
} 
+0

請加什麼rpn_parts是,以及爲節點和字符串的定義。爲什麼你在next_node有免費(str)?它可能不會打印,因爲你已經釋放了所有的str。什麼是頭? – Myforwik

回答

2

next_node函數中,您正在分配內存並將其分配給str的本地副本。這是內存泄漏,調用者永遠不會看到str的新值。相反,你可以這樣做:

void next_node(Node *node, String *str) 
{ 
    node->str = *str; 
    node->next = node_alloc(1); 
    node = node->next; 
    free(*str); 
    *str = malloc(sizeof(char*) * NUM_SIZE); 
    (*str)[0] = '\0'; 
} 

而且使用這樣的:

next_node(part_temp, &temp); 
+0

perreal感謝評論,但它不是問題,String已經是一個指針(指向字符的指針).. – PieThon

+0

是的,但是你不能從一個函數爲它分配一個新的值。你需要一個指針(指向一個字符)。 – perreal

+0

這裏似乎還有一個額外的問題,那就是您將字符串指針存儲到節點中,然後立即釋放該指針。所以現在節點的字符串指針指向虛假內存。 – JasonD

2

你已經做了一個很大的錯誤。
你已經把一個分號後的for循環這樣

for (ptr = head; ptr != NULL; ptr = ptr->next); 

它應該是這樣的

for (ptr = head; ptr != NULL; ptr = ptr->next) 

可能有所幫助。

+0

大聲笑,謝謝,我不能相信我做了那個愚蠢的錯誤..但現在我需要改變功能,因爲他們不工作,現在我可以繼續該項目,謝謝.. – PieThon

+0

我很高興我能幫到 –

+0

我有新問題,低頭看問題.. – PieThon

1

確定這奇怪的,如果我輸入的字符串的工作:

String rpn_equation = "2 3 5 + 6 2 + 5 * + *"; 

,甚至2個位數或4位數,但如果我輸入一個3位數或5位數,它把它錯了,我無法理解爲什麼:

Node *rpn_divider(String equation, int eq_size) 
{ 
    Node *head = node_alloc(1); //pointer to first element in the node 
    Node *part_temp = head; //pointer to the lattest element in the node 
    String temp = malloc(sizeof(char*) * NUM_SIZE); 

    int i, j = 0; //i = string equation index, j = string temp index 

    for (i = 0; i < eq_size; i++) 
    { 
     if (isNum(equation[i])) 
      temp[j++] = equation[i]; 
     else if (isOper(equation[i])) 
     { 
       temp[0] = equation[i]; 
       temp[1] = '\0'; 
       part_temp->str = temp; 
       part_temp->next = node_alloc(1); 
       part_temp = part_temp->next; 
       temp = malloc(sizeof(char*) * NUM_SIZE); 
       temp[0] = '\0'; 
     } 
     else 
     { 
       if (temp[j] == '\0') continue; 
       temp[j] = '\0'; 
       part_temp->str = temp; 
       part_temp->next = node_alloc(1); 
       part_temp = part_temp->next; 
       temp = malloc(sizeof(char*) * NUM_SIZE); 
       temp[0] = '\0'; 
       j = 0; 
     } 
    } 
    free(part_temp);   
    return head; 
} 

我刪除了node_next功能CUS它沒有使用它..

相關問題