2017-12-07 5 views
-1

바이너리 검색 트리 구조와 유사하게 저장 될 여러 텍스트 파일을 열어 읽어야합니다.
참고 : 사용자가 입력하기 전에 모두 열어야하고 내용이 저장되어야합니다.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; 
} 
+0

파일 내용이있는 "문자열"에 대한 포인터가 포함 된 BST를 만들지 못하게하는 요인은 무엇입니까? 문자열 포인터의 연결된 목록이있는 구조를 더 추가 할 수 있습니다. 알 수없는 길이의 텍스트는 메모리를 사용합니다. – Yunnosch

+0

"정확히 어떻게해야합니까?" 개념에 대해 읽어보고, 문제에 대한 자습서를 가지고 놀고, 작고 개별적으로 테스트 한 부분을 토대로합니다. 첫 번째 작은 파일은 malloced 메모리로 읽어 들이고 거기에서 인쇄됩니다. 그런 다음 BST를 도입하여 각 파일의 첫 번째 부분에 연결하십시오. 사용자 입력을 읽지 마십시오. 많은 트랩이 당신을 기다리고 있습니다. – Yunnosch

+0

나는이 'while (returned_value! = ('A '||'B '))'가 당신이 기대하는 것을하지 않는다는 것을 확실히 확신합니다. 반환 값을 컴파일러가 'true'를 나타내는 데 사용하는 값과 비교합니다. 아마도 while ((returned_value! = 'A') && (returned_value! = 'B'))'를 의미 할 것입니다. – Yunnosch

답변

0

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

를 엽니 다

// 기능

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; 
} 

을 성공했다. 컴파일러가 true을 나타내는 데 사용하는 값과 반환 된 값을 비교합니다.
현재 루프는 무한 루프처럼 보일 수 있으며 returned_value가 true과 동일 할 때까지 기다리는 중입니다.

당신은 returned_value는 'A'또는 'B'중 하나와 동일한 경우 루프를 떠나

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

이 필요합니다.
그런 식으로 무한 루프 문제가 해결됩니다. 이는 핵심 문제로 인해 파일 BST가 대화 형이되지 못하게합니다.

그런데 사용자 편의를 위해 'a'또는 'b'도 허용하는 것이 좋습니다.A는 각 입력 후 "다시 시도"피를 들어

, 최고의 공백을 무시 각각 'A'또는 'B를 입력 한 후 개행/복귀 등을 위해, 시작에서 공간

scanf(" %c", &user_input); 

를 사용 '.

+0

우리는 자본금 A 또는 B를 받아들이라는 지시를 받았습니다. 그런데, 왜 출력에 삽입 된 요소의 수를 곱하는 것입니까? '선택 (A/B) :'/로 두 요소를 삽입하십시오 - 표시 : '선택 (A/B) : 선택 (A/B) : ' –

+0

아마도 그것은 개행/A 또는 B 다음으로 되돌아갑니다. 무시해야합니다. – Yunnosch

+0

'scanf ("% c", & user_input);'를 시도해보십시오. 시작 부분에 공백이 있으므로 공백을 모두 무시합니다. – Yunnosch

관련 문제