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

あなたは大きな間違いをしました。
このようにforループの後にセミコロンを配置しました

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

こんな感じになります

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

これが役立つかもしれません。

于 2012-12-12T15:42:06.673 に答える
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 に答える
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 に答える