麻烦在C中使用递归调用

时间:2013-11-09 23:01:16

标签: c recursion

我正在开发一个项目,要求我构建一个表达式堆栈(+, - ,/,*,%,变量名称),并从该堆栈中,在父级,右侧,以二进制形式构建表达式树,左命令。我以前在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

1 个答案:

答案 0 :(得分: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;
}