2011-06-03 07:23:47 +08:00
|
|
|
//===-- dictionary.c ---------------------------------------------*- C -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-06-03 07:23:47 +08:00
|
|
|
//
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
#include <ctype.h>
|
2016-09-07 04:57:50 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2011-06-03 07:23:47 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
typedef struct tree_node {
|
2011-06-03 07:23:47 +08:00
|
|
|
const char *word;
|
|
|
|
struct tree_node *left;
|
|
|
|
struct tree_node *right;
|
|
|
|
} tree_node;
|
|
|
|
|
|
|
|
/* Given a char*, returns a substring that starts at the first
|
|
|
|
alphabet character and ends at the last alphabet character, i.e. it
|
|
|
|
strips off beginning or ending quotes, punctuation, etc. */
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
char *strip(char **word) {
|
2011-06-03 07:23:47 +08:00
|
|
|
char *start = *word;
|
2016-09-07 04:57:50 +08:00
|
|
|
int len = strlen(start);
|
2011-06-03 07:23:47 +08:00
|
|
|
char *end = start + len - 1;
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
while ((start < end) && (!isalpha(start[0])))
|
2011-06-03 07:23:47 +08:00
|
|
|
start++;
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
while ((end > start) && (!isalpha(end[0])))
|
2011-06-03 07:23:47 +08:00
|
|
|
end--;
|
|
|
|
|
|
|
|
if (start > end)
|
|
|
|
return NULL;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2011-06-03 07:23:47 +08:00
|
|
|
end[1] = '\0';
|
|
|
|
*word = start;
|
|
|
|
|
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Given a binary search tree (sorted alphabetically by the word at
|
|
|
|
each node), and a new word, inserts the word at the appropriate
|
|
|
|
place in the tree. */
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
void insert(tree_node *root, char *word) {
|
2011-06-03 07:23:47 +08:00
|
|
|
if (root == NULL)
|
|
|
|
return;
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
int compare_value = strcmp(word, root->word);
|
2011-06-03 07:23:47 +08:00
|
|
|
|
|
|
|
if (compare_value == 0)
|
|
|
|
return;
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
if (compare_value < 0) {
|
|
|
|
if (root->left != NULL)
|
|
|
|
insert(root->left, word);
|
|
|
|
else {
|
|
|
|
tree_node *new_node = (tree_node *)malloc(sizeof(tree_node));
|
|
|
|
new_node->word = strdup(word);
|
|
|
|
new_node->left = NULL;
|
|
|
|
new_node->right = NULL;
|
|
|
|
root->left = new_node;
|
2011-06-03 07:23:47 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
} else {
|
|
|
|
if (root->right != NULL)
|
|
|
|
insert(root->right, word);
|
|
|
|
else {
|
|
|
|
tree_node *new_node = (tree_node *)malloc(sizeof(tree_node));
|
|
|
|
new_node->word = strdup(word);
|
|
|
|
new_node->left = NULL;
|
|
|
|
new_node->right = NULL;
|
|
|
|
root->right = new_node;
|
2011-06-03 07:23:47 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2011-06-03 07:23:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read in a text file and storea all the words from the file in a
|
|
|
|
binary search tree. */
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
void populate_dictionary(tree_node **dictionary, char *filename) {
|
2011-06-03 07:23:47 +08:00
|
|
|
FILE *in_file;
|
|
|
|
char word[1024];
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
in_file = fopen(filename, "r");
|
|
|
|
if (in_file) {
|
|
|
|
while (fscanf(in_file, "%s", word) == 1) {
|
|
|
|
char *new_word = (strdup(word));
|
|
|
|
new_word = strip(&new_word);
|
|
|
|
if (*dictionary == NULL) {
|
|
|
|
tree_node *new_node = (tree_node *)malloc(sizeof(tree_node));
|
|
|
|
new_node->word = new_word;
|
|
|
|
new_node->left = NULL;
|
|
|
|
new_node->right = NULL;
|
|
|
|
*dictionary = new_node;
|
|
|
|
} else
|
|
|
|
insert(*dictionary, new_word);
|
2011-06-03 07:23:47 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2011-06-03 07:23:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Given a binary search tree and a word, search for the word
|
|
|
|
in the binary search tree. */
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
int find_word(tree_node *dictionary, char *word) {
|
2011-06-03 07:23:47 +08:00
|
|
|
if (!word || !dictionary)
|
|
|
|
return 0;
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
int compare_value = strcmp(word, dictionary->word);
|
2011-06-03 07:23:47 +08:00
|
|
|
|
|
|
|
if (compare_value == 0)
|
|
|
|
return 1;
|
|
|
|
else if (compare_value < 0)
|
2016-09-07 04:57:50 +08:00
|
|
|
return find_word(dictionary->left, word);
|
2011-06-03 07:23:47 +08:00
|
|
|
else
|
2016-09-07 04:57:50 +08:00
|
|
|
return find_word(dictionary->right, word);
|
2011-06-03 07:23:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Print out the words in the binary search tree, in sorted order. */
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
void print_tree(tree_node *dictionary) {
|
2011-06-03 07:23:47 +08:00
|
|
|
if (!dictionary)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (dictionary->left)
|
2016-09-07 04:57:50 +08:00
|
|
|
print_tree(dictionary->left);
|
2011-06-03 07:23:47 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
printf("%s\n", dictionary->word);
|
2011-06-03 07:23:47 +08:00
|
|
|
|
|
|
|
if (dictionary->right)
|
2016-09-07 04:57:50 +08:00
|
|
|
print_tree(dictionary->right);
|
2011-06-03 07:23:47 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
int main(int argc, char **argv) {
|
2011-06-03 07:23:47 +08:00
|
|
|
tree_node *dictionary = NULL;
|
|
|
|
char buffer[1024];
|
|
|
|
char *filename;
|
|
|
|
int done = 0;
|
|
|
|
|
|
|
|
if (argc == 2)
|
|
|
|
filename = argv[1];
|
|
|
|
|
|
|
|
if (!filename)
|
|
|
|
return -1;
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
populate_dictionary(&dictionary, filename);
|
|
|
|
fprintf(stdout, "Dictionary loaded.\nEnter search word: ");
|
|
|
|
while (!done && fgets(buffer, sizeof(buffer), stdin)) {
|
|
|
|
char *word = buffer;
|
|
|
|
int len = strlen(word);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < len; ++i)
|
|
|
|
word[i] = tolower(word[i]);
|
|
|
|
|
|
|
|
if ((len > 0) && (word[len - 1] == '\n')) {
|
|
|
|
word[len - 1] = '\0';
|
|
|
|
len = len - 1;
|
2011-06-03 07:23:47 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
if (find_word(dictionary, word))
|
|
|
|
fprintf(stdout, "Yes!\n");
|
|
|
|
else
|
|
|
|
fprintf(stdout, "No!\n");
|
|
|
|
|
|
|
|
fprintf(stdout, "Enter search word: ");
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stdout, "\n");
|
2011-06-03 07:23:47 +08:00
|
|
|
return 0;
|
|
|
|
}
|