如何在BST中制作交互式程序? C郎

时间:2017-12-07 06:41:10

标签: c memory struct binary-search-tree interactive

我需要打开和读取多个文本文件,这些文件将被存储类似于二叉搜索树结构 注意:在用户输入输入之前,都需要打开它们并存储它们的内容。

我确实需要一些建议,因为我不再看到我的错误了。

然而,我真正没有得到的是如何使其互动? 当我按下A'时,它需要从左侧节点和对面读取文本。

输出结构:

  

------------------ \ n
  \ n
  标题\ n
  file1 //文件存在 - ' A'
  file2 //文件为空/非空 - ' B' \ n
  文字\ n
  \ n
  您的选择(A / B):\ n

这是程序输出应该在开头看的方式: terminal

struct的元素

typedef struct _Elements_
{
  char* title_;
  struct _Elements_* left;
  struct _Elements_* right;
  char* text_;
} Elements;

//存在正向初始化

int main (int argc, char* argv[])
{
  char returned_value;
  Elements element;

  if (argc != 2)
  {
    printf("Usage: ./ass2 [file-name]\n");
    return 1;
  }

  returned_value = openFile(&element, argv[1]);

  while(returned_value != ('A' || 'B'))
  {
    repeatEntry(returned_value); // Function to scan the value
  }

  // Tricky part! Read from left ???
  if(returned_value == 'A')
  {
    printf("%s",element.left->text_);
  }
  else
  {
    printf("%s",element.right->text_);
  }

  return 0;
}

//这里我想初始化节点元素

//对于每个malloc,我必须确保它成功

Elements* newNode(Elements* element)
{
  Elements* newNode = (Elements*)malloc(sizeof(Elements));
  if(newNode == NULL)
  {
    printf("[ERR] Out of memory.\n");
     return (void*)2; // void* - to avoid warning of different type?
  }
  newNode->title_ = NULL;
  newNode->left = NULL;
  newNode->right = NULL;
  newNode->text_ = NULL;

  return newNode;
}

//打开文件的功能

char openFile(Elements* element, char* input)
{
  // Create new memory if tree is empty
  if(element == NULL)
  {
    return newNode(element);
  }
  FILE* file_open = fopen(input, "r");
  if (file_open == NULL)
  {
    printf("[ERR] Could not read file %s.\n", input);
    return 3;
  }

  // Local variables to handle the parsing file ?
  char line[80];
  int lenght_of_the_line; // Was just for me
  int line_number = 0; // this also

  // Do i really need local variables beside struct ? 
  char* title_local = NULL;
  char* first_file = NULL;
  char* second_file = NULL;
  char* text_local = NULL;

  while(fgets(line, 80, file_open))
  {
    lenght_of_the_line = strlen(line);
    //printf("%d ", lenght_of_the_line);
    //printf("%s ", line);

    // Get title
    if((line[lenght_of_the_line - 1] == '\n') && (line_number == 0))
    {

      title_local = (char*)malloc(lenght_of_the_line * (sizeof(char)) + 1);
      if(title_local == NULL)
      {
        printf("[ERR] Out of memory.\n");
        return 2;
      }
      strcpy(title_local, line);
      printf("%s ", element->title_ = title_local);
      printf("\n");
    }

    // Get 1st file name
    else if( (line[lenght_of_the_line - 1] == '\n') && (line_number == 1) )
    {
      if( (line[lenght_of_the_line - 5] == '.') &&
          (line[lenght_of_the_line - 4] == 't') &&
          (line[lenght_of_the_line - 3] == 'x') &&
          (line[lenght_of_the_line - 2] == 't') )
      {
        // Allocate enough memory for first file name
        first_file = (char*)malloc(lenght_of_the_line * (sizeof(char)) + 1);
        if(first_file == NULL)
        {
          printf("[ERR] Out of memory.\n");
          return 2;
        }
        strcpy(first_file, line);

         //name of file to open and store to left node
         //Seems to work, since I got no error
        openFile(element->left, first_file);
      }
    }

    // Get 2nd file name
    else if( (line[lenght_of_the_line - 1] == '\n') && (line_number == 2) )
    {
      if( (line[lenght_of_the_line - 5] == '.') &&
          (line[lenght_of_the_line - 4] == 't') &&
          (line[lenght_of_the_line - 3] == 'x') &&
          (line[lenght_of_the_line - 2] == 't') )
      {
        second_file = (char*)malloc(lenght_of_the_line * (sizeof(char)) + 1);
        if(second_file == NULL)
        {
          printf("[ERR] Out of memory.\n");
          return 2;
        }
        strcpy(second_file, line);
        openFile(element->right, second_file);
      }
    }
    else
    {
      text_local = (char*)malloc(lenght_of_the_line * (sizeof(char)) + 1);
      if(text_local == NULL)
        {
          printf("[ERR] Out of memory.\n");
          return 2;
        }
      strcpy(text_local, line);
      element->text_ = text_local;
      printf("%s", element->text_);
    }

    // Increase line number for 1
    line_number++;
  }
  //free(line);

  fclose(file_open);
  printf("\n");
  printf("Your choice (A/B)? ");
  char user_input;
  scanf("%c", &user_input);

  // DONT FORGET TO FREE THE MEMORY !

  return user_input;
}

1 个答案:

答案 0 :(得分:0)

这个

while(returned_value != ('A' || 'B'))

不会做你所期望的。它将返回值与编译器用于表示true的任何值进行比较 因此,当等待returned_value与true相同时,您的循环可能看起来是无限循环。

你需要

while((returned_value != 'A') && (returned_value != 'B'))

returned_value与'A'或'B'相同时,这将离开循环。
这样就解决了无限循环问题,这可能是阻止文件BST交互的核心问题。

顺便说一下,为了方便用户,我建议也接受'a'或'b'。

为避免在每次输入后“再试一次”,请使用

scanf(" %c", &user_input);

在开头有一个空格,用于忽略前导空格,包括每个输入'A'或'B'后的换行/返回。