Caroline Tice | 2e9dd93 | 2011-06-02 23:23:47 +0000 | [diff] [blame] | 1 | //===-- dictionary.c ---------------------------------------------*- C -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===---------------------------------------------------------------------===// |
Caroline Tice | 2e9dd93 | 2011-06-02 23:23:47 +0000 | [diff] [blame] | 9 | #include <ctype.h> |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
Caroline Tice | 2e9dd93 | 2011-06-02 23:23:47 +0000 | [diff] [blame] | 12 | #include <string.h> |
| 13 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 14 | typedef struct tree_node { |
Caroline Tice | 2e9dd93 | 2011-06-02 23:23:47 +0000 | [diff] [blame] | 15 | const char *word; |
| 16 | struct tree_node *left; |
| 17 | struct tree_node *right; |
| 18 | } tree_node; |
| 19 | |
| 20 | /* Given a char*, returns a substring that starts at the first |
| 21 | alphabet character and ends at the last alphabet character, i.e. it |
| 22 | strips off beginning or ending quotes, punctuation, etc. */ |
| 23 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 24 | char *strip(char **word) { |
Caroline Tice | 2e9dd93 | 2011-06-02 23:23:47 +0000 | [diff] [blame] | 25 | char *start = *word; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 26 | int len = strlen(start); |
Caroline Tice | 2e9dd93 | 2011-06-02 23:23:47 +0000 | [diff] [blame] | 27 | char *end = start + len - 1; |
| 28 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 29 | while ((start < end) && (!isalpha(start[0]))) |
Caroline Tice | 2e9dd93 | 2011-06-02 23:23:47 +0000 | [diff] [blame] | 30 | start++; |
| 31 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 32 | while ((end > start) && (!isalpha(end[0]))) |
Caroline Tice | 2e9dd93 | 2011-06-02 23:23:47 +0000 | [diff] [blame] | 33 | end--; |
| 34 | |
| 35 | if (start > end) |
| 36 | return NULL; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 37 | |
Caroline Tice | 2e9dd93 | 2011-06-02 23:23:47 +0000 | [diff] [blame] | 38 | end[1] = '\0'; |
| 39 | *word = start; |
| 40 | |
| 41 | return start; |
| 42 | } |
| 43 | |
| 44 | /* Given a binary search tree (sorted alphabetically by the word at |
| 45 | each node), and a new word, inserts the word at the appropriate |
| 46 | place in the tree. */ |
| 47 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 48 | void insert(tree_node *root, char *word) { |
Caroline Tice | 2e9dd93 | 2011-06-02 23:23:47 +0000 | [diff] [blame] | 49 | if (root == NULL) |
| 50 | return; |
| 51 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 52 | int compare_value = strcmp(word, root->word); |
Caroline Tice | 2e9dd93 | 2011-06-02 23:23:47 +0000 | [diff] [blame] | 53 | |
| 54 | if (compare_value == 0) |
| 55 | return; |
| 56 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 57 | if (compare_value < 0) { |
| 58 | if (root->left != NULL) |
| 59 | insert(root->left, word); |
| 60 | else { |
| 61 | tree_node *new_node = (tree_node *)malloc(sizeof(tree_node)); |
| 62 | new_node->word = strdup(word); |
| 63 | new_node->left = NULL; |
| 64 | new_node->right = NULL; |
| 65 | root->left = new_node; |
Caroline Tice | 2e9dd93 | 2011-06-02 23:23:47 +0000 | [diff] [blame] | 66 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 67 | } else { |
| 68 | if (root->right != NULL) |
| 69 | insert(root->right, word); |
| 70 | else { |
| 71 | tree_node *new_node = (tree_node *)malloc(sizeof(tree_node)); |
| 72 | new_node->word = strdup(word); |
| 73 | new_node->left = NULL; |
| 74 | new_node->right = NULL; |
| 75 | root->right = new_node; |
Caroline Tice | 2e9dd93 | 2011-06-02 23:23:47 +0000 | [diff] [blame] | 76 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 77 | } |
Caroline Tice | 2e9dd93 | 2011-06-02 23:23:47 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /* Read in a text file and storea all the words from the file in a |
| 81 | binary search tree. */ |
| 82 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 83 | void populate_dictionary(tree_node **dictionary, char *filename) { |
Caroline Tice | 2e9dd93 | 2011-06-02 23:23:47 +0000 | [diff] [blame] | 84 | FILE *in_file; |
| 85 | char word[1024]; |
| 86 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 87 | in_file = fopen(filename, "r"); |
| 88 | if (in_file) { |
| 89 | while (fscanf(in_file, "%s", word) == 1) { |
| 90 | char *new_word = (strdup(word)); |
| 91 | new_word = strip(&new_word); |
| 92 | if (*dictionary == NULL) { |
| 93 | tree_node *new_node = (tree_node *)malloc(sizeof(tree_node)); |
| 94 | new_node->word = new_word; |
| 95 | new_node->left = NULL; |
| 96 | new_node->right = NULL; |
| 97 | *dictionary = new_node; |
| 98 | } else |
| 99 | insert(*dictionary, new_word); |
Caroline Tice | 2e9dd93 | 2011-06-02 23:23:47 +0000 | [diff] [blame] | 100 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 101 | } |
Caroline Tice | 2e9dd93 | 2011-06-02 23:23:47 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /* Given a binary search tree and a word, search for the word |
| 105 | in the binary search tree. */ |
| 106 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 107 | int find_word(tree_node *dictionary, char *word) { |
Caroline Tice | 2e9dd93 | 2011-06-02 23:23:47 +0000 | [diff] [blame] | 108 | if (!word || !dictionary) |
| 109 | return 0; |
| 110 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 111 | int compare_value = strcmp(word, dictionary->word); |
Caroline Tice | 2e9dd93 | 2011-06-02 23:23:47 +0000 | [diff] [blame] | 112 | |
| 113 | if (compare_value == 0) |
| 114 | return 1; |
| 115 | else if (compare_value < 0) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 116 | return find_word(dictionary->left, word); |
Caroline Tice | 2e9dd93 | 2011-06-02 23:23:47 +0000 | [diff] [blame] | 117 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 118 | return find_word(dictionary->right, word); |
Caroline Tice | 2e9dd93 | 2011-06-02 23:23:47 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /* Print out the words in the binary search tree, in sorted order. */ |
| 122 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 123 | void print_tree(tree_node *dictionary) { |
Caroline Tice | 2e9dd93 | 2011-06-02 23:23:47 +0000 | [diff] [blame] | 124 | if (!dictionary) |
| 125 | return; |
| 126 | |
| 127 | if (dictionary->left) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 128 | print_tree(dictionary->left); |
Caroline Tice | 2e9dd93 | 2011-06-02 23:23:47 +0000 | [diff] [blame] | 129 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 130 | printf("%s\n", dictionary->word); |
Caroline Tice | 2e9dd93 | 2011-06-02 23:23:47 +0000 | [diff] [blame] | 131 | |
| 132 | if (dictionary->right) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 133 | print_tree(dictionary->right); |
Caroline Tice | 2e9dd93 | 2011-06-02 23:23:47 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 136 | int main(int argc, char **argv) { |
Caroline Tice | 2e9dd93 | 2011-06-02 23:23:47 +0000 | [diff] [blame] | 137 | tree_node *dictionary = NULL; |
| 138 | char buffer[1024]; |
| 139 | char *filename; |
| 140 | int done = 0; |
| 141 | |
| 142 | if (argc == 2) |
| 143 | filename = argv[1]; |
| 144 | |
| 145 | if (!filename) |
| 146 | return -1; |
| 147 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 148 | populate_dictionary(&dictionary, filename); |
| 149 | fprintf(stdout, "Dictionary loaded.\nEnter search word: "); |
| 150 | while (!done && fgets(buffer, sizeof(buffer), stdin)) { |
| 151 | char *word = buffer; |
| 152 | int len = strlen(word); |
| 153 | int i; |
Caroline Tice | 2e9dd93 | 2011-06-02 23:23:47 +0000 | [diff] [blame] | 154 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 155 | for (i = 0; i < len; ++i) |
| 156 | word[i] = tolower(word[i]); |
Caroline Tice | 2e9dd93 | 2011-06-02 23:23:47 +0000 | [diff] [blame] | 157 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 158 | if ((len > 0) && (word[len - 1] == '\n')) { |
| 159 | word[len - 1] = '\0'; |
| 160 | len = len - 1; |
Caroline Tice | 2e9dd93 | 2011-06-02 23:23:47 +0000 | [diff] [blame] | 161 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 162 | |
| 163 | if (find_word(dictionary, word)) |
| 164 | fprintf(stdout, "Yes!\n"); |
| 165 | else |
| 166 | fprintf(stdout, "No!\n"); |
| 167 | |
| 168 | fprintf(stdout, "Enter search word: "); |
| 169 | } |
| 170 | |
| 171 | fprintf(stdout, "\n"); |
Caroline Tice | 2e9dd93 | 2011-06-02 23:23:47 +0000 | [diff] [blame] | 172 | return 0; |
| 173 | } |