aboutsummaryrefslogtreecommitdiff
path: root/6.3/main.c
diff options
context:
space:
mode:
Diffstat (limited to '6.3/main.c')
-rw-r--r--6.3/main.c54
1 files changed, 36 insertions, 18 deletions
diff --git a/6.3/main.c b/6.3/main.c
index cd52cec..5c35e24 100644
--- a/6.3/main.c
+++ b/6.3/main.c
@@ -7,25 +7,20 @@
#define MAXWORD 100
#define ARR_SIZE(X) (sizeof(X)/sizeof(X[0]))
-const char *noise[] = {
- "the",
- "and"
-};
-
-int isnoise(char *word);
+int noiseword(char *w);
int main(void)
{
char word[MAXWORD];
struct tnode *root;
- int line = 0;
+ int linenum = 0;
root = NULL;
while (getword(word, MAXWORD) != EOF)
- if (isalpha(word[0]) && !isnoise(word))
- root = addtree(root, word, line);
+ if (isalpha(word[0]) && noiseword(word) == -1)
+ root = addtree(root, word, linenum);
else if(word[0] == '\n')
- ++line;
+ ++linenum;
tprint(root);
tfree(root);
@@ -33,13 +28,36 @@ int main(void)
return 0;
}
-int isnoise(char *word)
+int noiseword(char *w)
{
- size_t i;
-
- for(i = 0; i < ARR_SIZE(noise); ++i)
- if (!strcmp(word, noise[i]))
- return 1;
-
- return 0;
+ const char *noise[] = {
+ "a",
+ "an",
+ "and",
+ "are",
+ "in",
+ "is",
+ "of",
+ "or",
+ "that",
+ "the",
+ "this",
+ "to"
+ };
+
+ int cond, mid;
+ int low = 0;
+ int high = ARR_SIZE(noise);
+
+ while (low < high) {
+ mid = (low + high) / 2;
+ if ((cond = strcmp(w, noise[mid])) < 0)
+ high = mid;
+ else if (cond > 0)
+ low = mid + 1;
+ else
+ return mid;
+ }
+
+ return -1;
}