binary tree를 이용하여 출현 회수를 나타내는 함수 질문!
글쓴이: ekamfkr / 작성시간: 일, 2021/05/30 - 1:51오전
#include <stdio.h> #include <ctype.h> #include <string.h> #include "binTree-28.h" #define MAXWORD 100 struct key { char *word; int count; } keytab[] = { { "auto", 0 }, { "break", 0 }, { "case", 0 }, { "char", 0 }, { "const", 0 }, { "continue", 0 }, { "default", 0 }, { "do", 0 }, { "double", 0 }, { "else", 0 }, { "enum", 0 }, { "extern", 0 }, { "float", 0 }, { "for", 0 }, { "goto", 0 }, { "if", 0 }, { "int", 0 }, { "long", 0 }, { "register", 0 }, { "return", 0 }, { "short", 0 }, { "signed", 0 }, { "sizeof", 0 }, { "static", 0 }, { "struct", 0 }, { "switch", 0 }, { "typedef", 0 }, { "union", 0 }, { "unsigned", 0 }, { "void", 0 }, { "volatile", 0 }, { "while", 0 } }; #define NKEYS (sizeof keytab / sizeof(struct key)) int getWord(char *word, int lim); struct key *binSearch(char * word, struct key tab[], int n); int main(void) { char word[MAXWORD]; struct key *p; struct tnode *root; root = NULL; while(getWord(word, MAXWORD) != EOF) { if(isalpha(word[0]) && (binSearch(word, keytab, NKEYS) == NULL)) root = addTree(root, word); } for(p = keytab; p < keytab + NKEYS; p++) if(p->count > 0) printf("%4d %s\n", p->count, p->word); treePrint(root); return 0; } struct key * binSearch(char *word, struct key tab[], int n) { struct key *low = &tab[0]; struct key *high = &tab[n]; struct key *mid; int cond; while(low < high) { mid = low + (high - low) / 2; if((cond = strcmp(word, mid->word)) < 0) high = mid; else if(cond > 0) low = mid + 1; else return mid; } return NULL; }
이 코드 결과에 출현 행번호와 연산자의 출현 횟수를 출력하고 싶은데 어떻게 해야 할까요 ㅜㅠ
Forums:
댓글 달기