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);
}
4

3 回答 3

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);
于 2012-12-11T21:40:15.683 回答
2

你犯了一个大错误。
你在 for 循环后面放了一个分号,像这样

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

应该是这样的

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

可能这有帮助。

于 2012-12-12T15:42:06.673 回答
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 函数,因为它无法使用它..

于 2012-12-12T18:18:56.920 回答