바이너리 검색 트리 구조와 유사하게 저장 될 여러 텍스트 파일을 열어 읽어야합니다.
참고 : 사용자가 입력하기 전에 모두 열어야하고 내용이 저장되어야합니다.BST에서 대화 형 프로그램을 만드는 방법은 무엇입니까? C Lang
나는 내 오류가 더 이상 보이지 않기 때문에 정말로 조언이 필요합니다.
그러나 실제로 얻지 못하는 것은 어떻게 만들 수 있습니까? 대화 형?
'A'를 누르면 왼쪽 노드에서 반대쪽으로 텍스트를 읽어야합니다. 출력
구조 :
------------------ \ n
\ n
제목 \ n
// 파일 FILE1 본 - 'A'
파일 2 // 파일이 아닌 빈/빈 - 'B'\ n
텍스트 \ n을
\ n
당신의 선택 (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에 대해 반드시 t 모자는 당신이 아마 기대하지 않는 파일
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;
}
파일 내용이있는 "문자열"에 대한 포인터가 포함 된 BST를 만들지 못하게하는 요인은 무엇입니까? 문자열 포인터의 연결된 목록이있는 구조를 더 추가 할 수 있습니다. 알 수없는 길이의 텍스트는 메모리를 사용합니다. – Yunnosch
"정확히 어떻게해야합니까?" 개념에 대해 읽어보고, 문제에 대한 자습서를 가지고 놀고, 작고 개별적으로 테스트 한 부분을 토대로합니다. 첫 번째 작은 파일은 malloced 메모리로 읽어 들이고 거기에서 인쇄됩니다. 그런 다음 BST를 도입하여 각 파일의 첫 번째 부분에 연결하십시오. 사용자 입력을 읽지 마십시오. 많은 트랩이 당신을 기다리고 있습니다. – Yunnosch
나는이 'while (returned_value! = ('A '||'B '))'가 당신이 기대하는 것을하지 않는다는 것을 확실히 확신합니다. 반환 값을 컴파일러가 'true'를 나타내는 데 사용하는 값과 비교합니다. 아마도 while ((returned_value! = 'A') && (returned_value! = 'B'))'를 의미 할 것입니다. – Yunnosch