aboutsummaryrefslogtreecommitdiff
path: root/6.3/main.c
blob: 5c35e24f78c9151dd07d2e4255bd113ca481d6c5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "word.h"
#include "tree.h"

#define MAXWORD	100
#define ARR_SIZE(X) (sizeof(X)/sizeof(X[0]))

int noiseword(char *w);

int main(void)
{
	char word[MAXWORD];
	struct tnode *root;
	int linenum = 0;
	
	root = NULL;
	while (getword(word, MAXWORD) != EOF)
		if (isalpha(word[0]) && noiseword(word) == -1)
			root = addtree(root, word, linenum);
		else if(word[0] == '\n')
			++linenum;

	tprint(root);
	tfree(root);

	return 0;
}

int noiseword(char *w)
{
	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;
}