aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--6.3/Makefile13
-rw-r--r--6.3/getch.c23
-rw-r--r--6.3/getch.h2
-rw-r--r--6.3/main.c45
-rw-r--r--6.3/tree.c60
-rw-r--r--6.3/tree.h18
-rw-r--r--6.3/word.c20
-rw-r--r--6.3/word.h1
8 files changed, 182 insertions, 0 deletions
diff --git a/6.3/Makefile b/6.3/Makefile
new file mode 100644
index 0000000..ca85182
--- /dev/null
+++ b/6.3/Makefile
@@ -0,0 +1,13 @@
+objects = main.o tree.o word.o getch.o
+CC = gcc
+CFLAGS = -Wvla -Wall -Wextra -Wstrict-prototypes -Wmissing-prototypes -fsanitize=address
+
+words : $(objects)
+ $(CC) $(CFLAGS) -o words $(objects)
+
+main.o : word.h tree.h
+word.o : getch.h
+
+.PHONY : clean
+clean:
+ rm words $(objects)
diff --git a/6.3/getch.c b/6.3/getch.c
new file mode 100644
index 0000000..f271434
--- /dev/null
+++ b/6.3/getch.c
@@ -0,0 +1,23 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <error.h>
+#include <errno.h>
+#include "getch.h"
+
+#define MAXBUFF 100
+
+int top;
+char buff[MAXBUFF];
+
+char getch(void)
+{
+ return (top > 0) ? buff[--top] : getchar();
+}
+
+void ungetch(char c)
+{
+ if (top < MAXBUFF)
+ buff[top++] = c;
+ else
+ error(EXIT_FAILURE, ENOBUFS, "getch");
+}
diff --git a/6.3/getch.h b/6.3/getch.h
new file mode 100644
index 0000000..b0e086b
--- /dev/null
+++ b/6.3/getch.h
@@ -0,0 +1,2 @@
+char getch(void);
+void ungetch(char c);
diff --git a/6.3/main.c b/6.3/main.c
new file mode 100644
index 0000000..cd52cec
--- /dev/null
+++ b/6.3/main.c
@@ -0,0 +1,45 @@
+#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]))
+
+const char *noise[] = {
+ "the",
+ "and"
+};
+
+int isnoise(char *word);
+
+int main(void)
+{
+ char word[MAXWORD];
+ struct tnode *root;
+ int line = 0;
+
+ root = NULL;
+ while (getword(word, MAXWORD) != EOF)
+ if (isalpha(word[0]) && !isnoise(word))
+ root = addtree(root, word, line);
+ else if(word[0] == '\n')
+ ++line;
+
+ tprint(root);
+ tfree(root);
+
+ return 0;
+}
+
+int isnoise(char *word)
+{
+ size_t i;
+
+ for(i = 0; i < ARR_SIZE(noise); ++i)
+ if (!strcmp(word, noise[i]))
+ return 1;
+
+ return 0;
+}
diff --git a/6.3/tree.c b/6.3/tree.c
new file mode 100644
index 0000000..c58da53
--- /dev/null
+++ b/6.3/tree.c
@@ -0,0 +1,60 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <error.h>
+#include <errno.h>
+#include "tree.h"
+
+struct tnode *talloc(void);
+
+struct tnode *addtree(struct tnode *p, char *w, int line)
+{
+ int cond;
+
+ if (p == NULL) {
+ p = talloc();
+ p->word = strdup(w);
+ p->lc = 0;
+ p->lines[p->lc++] = line;
+ p->left = p->right = NULL;
+ } else if (!(cond = strcasecmp(w, p->word))) {
+ if (p->lc < MAXLINES)
+ p->lines[p->lc++] = line;
+ else
+ error(EXIT_FAILURE, ENOBUFS, "addtree");
+ } else if (cond < 0) {
+ p->left = addtree(p->left, w, line);
+ } else if (cond > 0) {
+ p->right = addtree(p->right, w, line);
+ }
+
+ return p;
+}
+
+void tprint(struct tnode *p)
+{
+ int lc;
+
+ if (p != NULL) {
+ tprint(p->left);
+ printf("---> %s\n", p->word);
+ for (lc = 0; lc < p->lc; ++lc)
+ printf((lc == p->lc-1) ? "%d" : "%d, ", p->lines[lc]);
+ putchar('\n');
+ tprint(p->right);
+ }
+}
+void tfree(struct tnode *p)
+{
+ if (p != NULL) {
+ tfree(p->left);
+ tfree(p->right);
+ free(p->word);
+ free(p);
+ }
+}
+
+struct tnode *talloc(void)
+{
+ return (struct tnode *) malloc(sizeof(struct tnode));
+}
diff --git a/6.3/tree.h b/6.3/tree.h
new file mode 100644
index 0000000..c6ebc53
--- /dev/null
+++ b/6.3/tree.h
@@ -0,0 +1,18 @@
+#ifndef TREE_H
+
+#define MAXLINES 1000
+
+struct tnode {
+ char *word;
+ int lc;
+ int lines[MAXLINES];
+ struct tnode *left;
+ struct tnode *right;
+};
+
+#define TREE_H
+#endif
+
+struct tnode *addtree(struct tnode *p, char *w, int line);
+void tprint(struct tnode *p);
+void tfree(struct tnode *p);
diff --git a/6.3/word.c b/6.3/word.c
new file mode 100644
index 0000000..c5a6381
--- /dev/null
+++ b/6.3/word.c
@@ -0,0 +1,20 @@
+#include <ctype.h>
+#include "getch.h"
+#include "word.h"
+
+char getword(char *word, int lim)
+{
+ char *w = word;
+
+ while (isblank(*w = getch()))
+ ;
+
+ if (isalpha(*w)) {
+ while ((isalnum(*++w = getch()) || *w == '_') && --lim > 0)
+ ;
+ ungetch(*w--);
+ }
+
+ *++w = '\0';
+ return word[0];
+}
diff --git a/6.3/word.h b/6.3/word.h
new file mode 100644
index 0000000..96140df
--- /dev/null
+++ b/6.3/word.h
@@ -0,0 +1 @@
+char getword(char *word, int lim);