2012-12-24 1 views
1

a, an, the 등의 "노이즈"단어를 무시하고 단어 목록과 줄 번호를 인쇄하는 상호 참조자를 작성하려고합니다. (Kernighan and Ritchie, ANSI 판 143 항 질문 6-3).교차 참조 자의 linecount를 증가시킬 수 없습니다.

#include<stdio.h> 

#include<stdlib.h> 

#include<string.h> 

#include"getch.h" 

#include"search_tools.h" /*this contains the binsearch function*/ 

#include"getword.h" 

#define MAXWORD 100 

char* noise[] = {"a","and","if","is","the"}; /*noise words that we need to exclude*/ 

int linecount = 1; 

struct tnode { 

     int line_number; 

     char* word; 

     struct tnode* left; /*for all those words lexicographically less than word*/ 

     struct tnode* middle; /*for all those words lexicographically equal to word*/ 

     struct tnode* right; /*for all those words lexicographically greater than word*/ 

       }; 

/*this function tells you where you should put your new word*/ 

struct tnode* addtree (struct tnode* p, char* w) { 

     int cond; 

     if (p == NULL) { 

       p = (struct tnode*)malloc(sizeof(struct tnode)); 

       p -> word = strdup(w); 

       p -> line_number = linecount; 

       p -> left = p -> right = p -> middle = NULL; 

         } 

     else if ((cond = strcmp(w,p->word)) == 0) { 

       p -> middle = addtree(p -> middle,w); 


                } 

     else if (cond > 0) { 

       p -> right = addtree(p -> right,w); 

          } 

     else if (cond < 0) { 

       p -> left = addtree(p -> left, w); 

          } 

     else { 

       ; 

      } 

     return p; 
                } 

void treeprint (struct tnode* p) { 

     struct tnode* q; 

     if (p != NULL) { 

       treeprint(p->left); 

       printf("%s occurs in the following lines:\n",p -> word); 

       for (q = p; q != NULL; q = q->middle) 

         printf("%4d ",q -> line_number); 

       printf("\n\n"); 

       treeprint(p->right); 
         } 


            } 

int main (int argc, char* argv[]) { 

     struct tnode* root; 

     char word[MAXWORD]; 

     root = NULL; 

     int c; 

     while ((c = getword(word,MAXWORD)) != EOF) { 

       if (isalpha(word[0]) && binsearch(word,noise,5) == -1) 

         root = addtree(root,word); 

       else if (c == '\n') 

         linecount++; 




                } 

     treeprint(root); 

     return 0; 

            } 

이 내가 사용하는 getword 기능입니다 : 다음은 코드

int getword (char* word, int lim) { 

    int c; 

    char* w; 

    w = word;  

    while (isspace(c = getch())) /*skip white spaces*/ 

      ; 

    if (c != EOF) 

      *w++ = c; 

    if (!isalpha(c)) { /*the first character in a word can be a #, as in #define or #include*/ 

      *w = '\0'; 

      return c; 

    } 

    while(isalnum(c = getch()) && --lim > 0) 

      *w++ = c; 

    ungetch(c); 

    *w = '\0'; 

    return word[0]; /*signal that a word has been collected*/ 
} 

는 다음과 같은 숯불 포인터를 검색하기위한 binsearch입니다 : getword가 건너 오는 경우

int binsearch (char * word, struct key tab[], int n) { 

     int cond; 

     int low, high, mid; 

     low = 0; 

     high = n -1; 

     while (low <= high) { 

       mid = (low+high)/2; 

       if ((cond = strcmp(word,tab[mid].word)) < 0) 

         high = mid - 1; 

       else if (cond > 0) 

         low = mid + 1; 

       else /*found it*/ 

         return mid; 
          } 

     return -1; 

} 

을 비 영문자 (단어의 첫 번째 문자) 또는 숫자가 아닌 문자 인 경우이를 반환해야합니다. 따라서 getword가 '\ n'을 반환 할 때 lincecount를 증가 시키도록 설정했습니다. 그러나 이것은 일어나지 않는 것 같습니다.

+0

여기에 문제의 코드 스 니펫을 붙여 넣을 수 있다면 정말 감사 할 것입니다. –

+1

사소한 nitpick :'p = malloc (sizeof * p); 이후''p-> left = p-> right = p-> middle = NULL; ' – wildplasser

+0

@wildplasser, 고맙습니다. 코드를 입력합니다. – saad

답변

1

여기에 getword 기능에 문제가 있습니다.

while (isspace(c = getch())) /*skip white spaces*/ 

     ; 

'\n'은 건너 뛸 공백 문자 중 하나이기 때문에. 따라서 줄 수를 계산하는 코드는 결코 그것을 볼 수 없습니다.

+0

와우 나는 그것을 결코 깨닫지 못했습니다. 나는 공백이 공백과 탭만을 의미한다고 생각하곤했다. 고맙습니다. @ 보 페르손! – saad