存储在struct中的值随着我无法想到而发生变化

时间:2017-11-28 02:55:38

标签: c struct

这个程序是一个猜谜游戏,当你玩它时会变得更好(我在这里没有包括一些方法)。它基本上是由结构组成的二叉树。只有树末端的结构才是猜测。所有其他结构都是问题。每次我玩游戏,都会很好。但是,第三次玩游戏时,它会将树内所有结构的问题更改为1个问题而我无法理解为什么(树的整体结构看起来很好,因为它以正确的猜测结束) 。我以一种非常混乱的方式编码,我为此道歉。但是,如果我能解决这个问题的任何帮助将非常感激。

struct node
{
    char *guess;
    char *question;
    struct node *yes_ptr;
    struct node *no_ptr;
};
struct node *createObjectNode(char *guess)
{
   struct node *newNode = malloc(sizeof(struct node));
   newNode->guess = guess;
   return newNode;
}

int response(char *str)
{
  char yesOrNo[5];
  printf("%s\n>", str);
  fgets(yesOrNo, 5, stdin);
  int i = strlen(yesOrNo)-1;
  yesOrNo[i] = '\0';
  if(strcmp(yesOrNo, "yes") == 0)
  {
    return 1;
  }
  if(strcmp(yesOrNo, "no") == 0)
  {
    return 0;
  }
  return 0;
}
int main(int argc, char *argv[])
{

  struct node *root;
  char guess[20];
  char question[120];
  char buffer[120];
  root = createObjectNode("pangolin");
  if(response("Shall we play a game?") == 1)
  {
    snprintf(buffer, sizeof(buffer), "Is it a %s?", getGuess(root));
    if(response(buffer) == 1)
    {
      printf("I win\n");
    }
    else
    {
      printf("What were you thinking of?\n>");
      fgets(guess, 50, stdin);
      int i = strlen(guess)-1;
      if( guess[ i ] == '\n') 
        guess[i] = '\0';
      printf("Please give me a question about a %s,so I can tell the difference between a %s and a ", 
      guess, guess);
      printf("%s\n>",getGuess(root));
      fgets(question, 50, stdin);
      i = strlen(question)-1;
      if( question[ i ] == '\n') 
        question[i] = '\0';
      snprintf(buffer, sizeof(buffer), "What is the answer for %s?", guess);
      if(response(buffer) == 1)
      {
        struct node *child1 = createObjectNode(guess);
        struct node *child2 = createObjectNode(getGuess(root));
        child1->yes_ptr = NULL;
        child2->no_ptr = NULL;
        child1->question = NULL;
        child2->question = NULL;
        root->guess = NULL;
        root->question = question;
        root->yes_ptr = child1;
        root->no_ptr = child2;      
      }
      else
      {
        struct node *child1 = createObjectNode(guess);
        struct node *child2 = createObjectNode(getGuess(root));
        child1->yes_ptr = NULL;
        child2->no_ptr = NULL;
        child1->question = NULL;
        child2->question = NULL;
        root->guess = NULL;
        root->question = question;
        root->yes_ptr = child2;
        root->no_ptr = child1;  
      }
      printTree(root);

    }
  }

  while(1)
  {
    char guess2[20];
    char question2[120];
    char buffer2[120];
    struct node *thisNode = malloc(sizeof(struct node));
    thisNode->question = root->question;
    thisNode->yes_ptr = root->yes_ptr;
    thisNode->no_ptr = root->no_ptr;
    if(response("Shall we play again?"))
    {
      while(thisNode->yes_ptr != NULL && thisNode->no_ptr != NULL && !thisNode->guess)
      {
        if(response(thisNode->question) == 1)
        {
          thisNode = thisNode->yes_ptr;
        }
        else
        {
          thisNode = thisNode->no_ptr;
        }
      }
      snprintf(buffer2, sizeof(buffer2), "Is it a %s?", getGuess(thisNode));
      if(response(buffer2) == 1)
      {
        printf("I win\n");
        break;
      }
      else
      {
        printf("What were you thinking of?\n>");
        fgets(guess2, 10, stdin);
        printf("Please give me a question about a %s,so I can tell the difference between a %s and a %s\n>", 
                guess2, guess2, getGuess(thisNode));
        int i = strlen(guess2)-1;
        guess2[i] = '\0';
        fgets(question2, 120, stdin);
        i = strlen(question2)-1;
        if( question2[i] == '\n') 
          question2[i] = '\0';
        snprintf(buffer2, sizeof(buffer2), "What is the answer for %s?", guess2);
        if(response(buffer2) == 1)
        {
          struct node *child1 = createObjectNode(guess2);
          struct node *child2 = createObjectNode(getGuess(thisNode));
          child1->yes_ptr = NULL;
          child2->no_ptr = NULL;
          child1->question = NULL;
          child2->question = NULL;
          thisNode->guess = NULL;
          thisNode->question = question2;
          thisNode->yes_ptr = child1;
          thisNode->no_ptr = child2;  
        }
        else
        {
          struct node *child1 = createObjectNode(guess2);
          struct node *child2 = createObjectNode(getGuess(thisNode));
          child1->yes_ptr = NULL;
          child2->no_ptr = NULL;
          child1->question = NULL;
          child2->question = NULL;
          thisNode->guess = NULL;
          thisNode->question = question2;
          thisNode->yes_ptr = child2;
          thisNode->no_ptr = child1;  
        }
      }
      printTree(root);

    } 

  } 
  return 0;
}  

0 个答案:

没有答案