2012-12-11 79 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它没有使用它..

相关问题