如何在 BST 中制作交互式程序? C郎
How to make Interactive program in BST ? C Lang
我需要打开并读取多个文本文件,这些文件将以类似于二叉搜索树构造的方式存储。
注意:它们都需要在用户输入之前打开并存储它们的内容。
我确实需要一些建议,因为我再也看不到我的错误了。
然而,我真正不明白的是,如何使它 互动?
当我按下 'A' 时,它需要从左侧节点和对侧节点读取文本。
输出结构:
------------------\n
\n
Title\n
file1 // file present - 'A'
file2 // file empty/not empty - 'B'\n
Text\n
\n
Your choice (A/B):\n
这是程序输出在开始时的样子:
terminal
结构的元素
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;
}
这个
while(returned_value != ('A' || 'B'))
不符合您的预期。它将返回值与编译器用来表示 true
.
的任何值进行比较
目前,您的循环可能看起来是一个无限循环,同时等待 returned_value 与 true
相同。
你需要
while((returned_value != 'A') && (returned_value != 'B'))
当 returned_value
与 'A' 或 'B' 相同时,这将离开循环。
这样就解决了死循环问题,这可能是阻止文件 BST 交互的核心问题。
顺便说一下,为了方便用户,我建议也接受 'a' 或 'b'。
为避免每次输入后出现 "try again",请使用
scanf(" %c", &user_input);
以space开头,用于忽略前导白色space,包括每个输入'A'或'B'后的newline/return。
我需要打开并读取多个文本文件,这些文件将以类似于二叉搜索树构造的方式存储。
注意:它们都需要在用户输入之前打开并存储它们的内容。
我确实需要一些建议,因为我再也看不到我的错误了。
然而,我真正不明白的是,如何使它 互动?
当我按下 'A' 时,它需要从左侧节点和对侧节点读取文本。
输出结构:
------------------\n
\n
Title\n
file1 // file present - 'A'
file2 // file empty/not empty - 'B'\n
Text\n
\n
Your choice (A/B):\n
这是程序输出在开始时的样子: terminal
结构的元素
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;
}
这个
while(returned_value != ('A' || 'B'))
不符合您的预期。它将返回值与编译器用来表示 true
.
的任何值进行比较
目前,您的循环可能看起来是一个无限循环,同时等待 returned_value 与 true
相同。
你需要
while((returned_value != 'A') && (returned_value != 'B'))
当 returned_value
与 'A' 或 'B' 相同时,这将离开循环。
这样就解决了死循环问题,这可能是阻止文件 BST 交互的核心问题。
顺便说一下,为了方便用户,我建议也接受 'a' 或 'b'。
为避免每次输入后出现 "try again",请使用
scanf(" %c", &user_input);
以space开头,用于忽略前导白色space,包括每个输入'A'或'B'后的newline/return。