]> git.sur5r.net Git - cc65/blob - test/ref/wf1.c
added tests as prepared by oliver
[cc65] / test / ref / wf1.c
1 /*
2   !!DESCRIPTION!! print word frequencies; uses structures
3   !!ORIGIN!!      LCC 4.1 Testsuite
4   !!LICENCE!!     own, freely distributeable for non-profit. read CPYRIGHT.LCC
5 */
6
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <ctype.h>
11
12 #define MAXWORDS 250
13
14 struct node
15 {
16         int count;              /* frequency count */
17         struct node *left;      /* left subtree */
18         struct node *right;     /* right subtree */
19         char *word;             /* word itself */
20 } words[MAXWORDS];
21 int next;               /* index of next free entry in words */
22
23 /*struct node *lookup();*/
24
25 #if defined(NO_NEW_PROTOTYPES_FOR_OLD_FUNC_DECL) && !defined(NO_OLD_FUNC_DECL)
26
27 #else
28
29 int err(char *s);
30 int getword(char *buf);
31 void tprint(struct node *tree);
32 struct node *lookup(char *word, struct node **p);
33
34 #endif
35
36 int isletter(char c);
37
38 /* err - print error message s and die  */
39 #ifndef NO_OLD_FUNC_DECL
40 err(s) char *s; {
41 #else
42 int err(char *s) {
43 #endif
44         printf("? %s\n", s);
45         exit(1);
46 }
47
48 /* getword - get next input word into buf, return 0 on EOF */
49 #ifndef NO_OLD_FUNC_DECL
50 int getword(buf) char *buf;
51 #else
52 int getword(char *buf)
53 #endif
54 {
55         char *s;
56         int c;
57
58         while (((c = getchar()) != -1) && (isletter(c) == 0))
59                 ;
60         for (s = buf; (c = isletter(c)); c = getchar())
61                 *s++ = c;
62         *s = 0;
63         if (s > buf)
64                 return 1;
65         return 0;
66 }
67
68 /* isletter - return folded version of c if it is a letter, 0 otherwise */
69 int isletter(char c)
70 {
71         if ((c >= 'A') && (c <= 'Z')) c += 'a' - 'A';
72         if ((c >= 'a') && (c <= 'z')) return c;
73         return 0;
74 }
75
76 /* lookup - lookup word in tree; install if necessary */
77 #ifndef NO_OLD_FUNC_DECL
78 struct node *lookup(word, p)
79 char *word; struct node **p;
80 #else
81 struct node *lookup(char *word, struct node **p)
82 #endif
83 {
84         int cond;
85 /*      char *malloc(); */
86
87         if (*p) {
88                 cond = strcmp(word, (*p)->word);
89                 if (cond < 0)
90                         return lookup(word, &(*p)->left);
91                 else if (cond > 0)
92                         return lookup(word, &(*p)->right);
93                 else
94                         return *p;
95         }
96         if (next >= MAXWORDS)
97                 err("out of node storage");
98         words[next].count = 0;
99         words[next].left = words[next].right = 0;
100         words[next].word = malloc(strlen(word) + 1);
101         if (words[next].word == 0)
102                 err("out of word storage");
103         strcpy(words[next].word, word);
104         return *p = &words[next++];
105 }
106
107 /* tprint - print tree */
108 #ifndef NO_OLD_FUNC_DECL
109 void tprint(tree) struct node *tree; {
110 #else
111 void tprint(struct node *tree) {
112 #endif
113         if (tree) {
114                 tprint(tree->left);
115                 printf("%d:%s\n", tree->count, tree->word);
116                 tprint(tree->right);
117         }
118 }
119
120 int main(void)
121 {
122         struct node *root;
123         char word[20];
124
125         root = 0;
126         next = 0;
127         while (getword(word))
128                 lookup(word, &root)->count++;
129         tprint(root);
130
131         return 0;
132 }