0

私は、式 (+、-、​​/、*、%、変数名) のスタックを構築し、そのスタックから、親、右、左の順序でバイナリ形式の式ツリーを構築する必要があるプロジェクトに取り組んでいます。以前にこのプロジェクトを Python で行ったことがあり、再帰を使用してこれを行うのはかなり簡単でしたが、C は一緒にプレイしたくないようです。これを再帰的に行いたい理由は、私が言ったように、以前にそれを行ったことがあり、それが方法を知っている唯一の方法だからです。印刷機能が印刷されていないため、問題は再帰呼び出しにあると思います。皆さんが私がやろうとしていること、そして私が直面している問題を理解するのに十分な情報を提供できたことを願っています。ありがとう!

//this builds our stack of expression tokens
ExpNode* buildStack (char expr[]){
   StackNode *stack;
   char *pch;
   int counter = 0;
   pch = strtok(expr, " ");

   //build the stack of tokens
   while (pch != NULL){
      //initialize the stack with the first token
      if (counter == 0){
         stack = makeStackNode(pch, NULL);
         counter = 1;
       }
       pch = strtok(NULL, " " );
       //out of tokens, break out of the loop
       if (pch == NULL){
          break;
       }
       //push the next token onto the stack
       push(&stack, pch);
   }

   ExpNode *node;
   printf("about to build tree\n");
   node = buildExpTreeRec(stack);
   if (node == NULL){
      printf("ITS A NULL");
   }
   else{
      printf("ITS NOT A NULL"); // this line doesn't get printed but it should
   }

   printf("returning the node\n"); // this line doesn't get printed but it should
   return node;
}

//this builds our binary expression tree
ExpNode *buildExpTreeRec(StackNode *stack){
   printf("top of the stack is %s \n", top(stack));
   if (emptyStack(stack) != 0){
      printf("HOUSTON WE HAVE A PROBLEM");
   }
   else{
      char *data = top(stack);
      pop(&stack);
      ExpNode *node;
      printf("Right before we makeExpNode\n");

      //I believe the issue is here
      node = makeExpNode(data, buildExpTreeRec(stack), buildExpTreeRec(stack));
      printf("Right after we makeExpNode\n"); // this line doesn't get printed but it should
      return node;
   }
   printf("About to return a NULL\n");
   return NULL;
}

//this builds our expression node
ExpNode* makeExpNode(char token[], ExpNode* left, ExpNode* right){
   printf("token is : %s\n", token); //this line is never printed but it should
   char * pch;
   int integer;
   pch = strchr(token, '.');
   ExpNode *node = malloc(sizeof(ExpNode));

   //do stuff to make node
   //this function was tested and it works correctly without using recursive calls
   return node;
}

出力:

リスト項目

a b + // this is user input
Made stack node with a
Made stack node with b
Pushed b onto the stack
Made stack node with +

Pushed + onto the stack
about to build tree
top of the stack is +

Right before we makeExpNode
top of the stack is b
Right before we makeExpNode
top of the stack is a
Right before we makeExpNode

問題は、次の行のパラメーターとしての再帰関数呼び出しにあります。

node = makeExpNode(data, buildExpTreeRec(stack), buildExpTreeRec(stack));

行を次のように変更すると:

 node = makeExpNode(data, NULL, NULL);

正しい出力が得られます:

a //input
Made stack node with a
about to build tree
top of the stack is a
Right before we makeExpNode
token is : a
Done making EXPNODE
Right after we makeExpNode
ITS NOT A NULL
4

1 に答える 1

0

友人が私に問題を指摘しました。再帰的な方法には基本的なケースがありませんでした。

修理:

ExpNode *buildExpTreeRec(StackNode *stack){
    ExpNode *node;
    char *data = top(stack);
    pop(&stack);
    if (emptyStack(stack) != 0 ){
        node = makeExpNode(data, NULL, NULL); // needed this
    }
    else{
        printf("Right before we makeExpNode\n");
        node = makeExpNode(data,buildExpTreeRec(stack), buildExpTreeRec(stack));
        printf("Right after we makeExpNode\n");
    }
    return node;
}
于 2013-11-10T01:50:12.010 に答える