로그래밍 며 ¶
문를 리루 려구 는데 되.......
는 려, 명 데 못되구 는게 명데....... 러를 ......
는 려, 명 데 못되구 는게 명데....... 러를 ......
¶
~cpp #include <iostream> using namespace std; #include <fstream> struct node{ node * left_child; char data_word[30]; node * right_child; }; class SearchWord { private: node * head; node * temphead; int count; public: SearchWord(); void ReadWord(); node * Search(char *, node *); void InsertWord(char *, node *); void Display(node *); }; SearchWord::SearchWord() { count = 0; } // 는 단를 다 void SearchWord::ReadWord() { ifstream file; char line[300]; char * word = NULL; file.open("test.txt"); while(!file.eof()) { file.getline(line, 300); cout << line << "\n"; word = strtok(line, " ."); count ++; //단가 리 는 검 temphead = head; temphead = Search(word, temphead); //단가 리 면 단를 리 if(temphead == NULL) { temphead = head; InsertWord(word, temphead); } //단가 리 면 else { } while(word) { cout << word << "\n"; word = strtok(0, " ."); count++; //단가 리 는 검 temphead = head; temphead = Search(word, temphead); //단가 리 면 단를 리 if(temphead == NULL) { temphead = head; InsertWord(word, temphead); } //단가 리 면 else { } } } cout << "count : " << count << "\n"; } //단가 리 면 노드반, 면 NULL반 node * SearchWord::Search(char * word, node * root) { if(!root) return NULL; if(!strcmp(word, root->data_word)) return root; if(strcmp(word, root->data_word) < 0) return Search(word, root->left_child); return Search(word, root->right_child); } void SearchWord::InsertWord(char * word, node * root) { if(!root) { head = new node; strcpy(head->data_word, word); head->left_child = head->right_child = NULL; } else { while(root != NULL) { if(strcmp(word, root->data_word) < 0) root = root->left_child; else root = root->right_child; } node * temp = new node; strcpy(temp->data_word, word); temp->left_child = temp->right_child = NULL; root = temp; } } void SearchWord::Display(node * tree) { cout << tree->data_word << "\n"; } int main() { SearchWord word; SearchWord(); word.ReadWord(); return 0; }