File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #include <stdio.h>
2+ #include <string.h>
3+ #define BUFSIZE 100
4+
5+ int buf [BUFSIZE ]; /* buffer for ungetch */
6+ int bufp = 0 ; /* buffer pointer (not a real pointer) */
7+
8+ int getch (void ) /* get a (possibly pushed back) character */ {
9+ return (bufp > 0 ) ? buf [-- bufp ] : getchar ();
10+ }
11+
12+ void ungetch (int c ) /* push character back on input */ {
13+ if (bufp >= BUFSIZE )
14+ printf ("ungetch : too many characters\n" );
15+ else
16+ buf [bufp ++ ] = c ;
17+ }
18+
19+ void ungets (char s []) /* push string s back to input */ {
20+ int i = strlen (s );
21+ while (i > 0 ) ungetch (s [-- i ]);
22+ }
Original file line number Diff line number Diff line change 1+ #ifndef BUFFER_H
2+ #define BUFFER_H
3+
4+ int getch (void );
5+
6+ void ungetch (int );
7+
8+ void ungets (char []);
9+
10+ #endif
Original file line number Diff line number Diff line change 1+ #include <stdio.h>
2+ #include <stdlib.h>
3+ #include <string.h>
4+ #include <ctype.h>
5+ #include "buffer.h"
6+ #include "io.h"
7+
8+ char * _strdup (char * s ) { /* make a duplicate of s */
9+ char * p ;
10+
11+ p = (char * ) malloc (strlen (s ) + 1 ); /* +1 for '\0' */
12+ if (p != NULL )
13+ strcpy (p , s );
14+ return p ;
15+ }
16+
17+ /* _strndup: make a duplicate of at most 6 characters of s*/
18+ char * _strndup (char * s , int n ) {
19+ char * p ;
20+
21+ p = (char * ) malloc (n + 1 );
22+ if (p != NULL )
23+ strncpy (p , s , n );
24+ return p ;
25+ }
26+
27+ /* getword: get next word or character from input */
28+ int getword (char * word , int lim ) {
29+ int c ;
30+ char * w = word ;
31+
32+ /* skip the whitespaces */
33+ while (isspace (c = getch ()))
34+ ;
35+ if (c != EOF )
36+ * w ++ = c ;
37+ if (!isalpha (c )) {
38+ * w = '\0' ;
39+ return c ;
40+ }
41+
42+ while (-- lim ) {
43+ if (!isalnum (* w = getch ())) {
44+ ungetch (* w );
45+ break ;
46+ }
47+ w ++ ;
48+ }
49+ * w = '\0' ;
50+ return word [0 ];
51+ }
Original file line number Diff line number Diff line change 1+ #ifndef IO_H
2+ #define IO_H
3+
4+ char * _strdup (char * s );
5+ char * _strndup (char * s , int n );
6+
7+ int getword (char * word , int lim );
8+
9+ #endif
Original file line number Diff line number Diff line change 1+ #include <stdio.h>
2+ #include <ctype.h>
3+ #include <stdlib.h>
4+ #include <string.h>
5+ #include "tree.h"
6+ #include "io.h"
7+
8+ #define MAXWORD 100
9+
10+ int getword (char * , int );
11+
12+ /* word frequency count */
13+ int main (int argc , char * * argv ) {
14+ struct tnode * root ;
15+ char word [MAXWORD ];
16+
17+ root = NULL ;
18+ while (getword (word , MAXWORD ) != EOF )
19+ if (isalpha (word [0 ]))
20+ root = addtree (root , word );
21+ treeprint (root );
22+ return 0 ;
23+ }
Original file line number Diff line number Diff line change 1+ #include <stdio.h>
2+ #include <string.h>
3+ #include <stdlib.h>
4+ #include "io.h"
5+ #include "tree.h"
6+
7+ /* addtree: add a node with w, at or below p */
8+ struct tnode * addtree (struct tnode * p , char * w ) {
9+ int cond ;
10+
11+ if (p == NULL ) { /* a new word has arrived */
12+ p = talloc ();
13+ p -> word = _strdup (w );
14+ p -> count = 1 ;
15+ p -> left = p -> right = NULL ;
16+ } else if ((cond = strcmp (w , p -> word )) == 0 )
17+ p -> count ++ ; /* repeated word */
18+ else if (cond < 0 )
19+ p -> left = addtree (p -> left , w );
20+ else
21+ p -> right = addtree (p -> right , w );
22+ return p ;
23+ }
24+
25+ /* treeprint: in-order print of tree p */
26+ void treeprint (struct tnode * p ) {
27+ if (p != NULL ) {
28+ treeprint (p -> left );
29+ printf ("%4d %s\n" , p -> count , p -> word );
30+ treeprint (p -> right );
31+ }
32+ }
33+
34+ /* talloc: make a tnode */
35+ struct tnode * talloc (void ) {
36+ return (struct tnode * ) malloc (sizeof (struct tnode ));
37+ }
Original file line number Diff line number Diff line change 1+ #ifndef TREE_H
2+ #define TREE_H
3+
4+ struct tnode { /* the tree node: */
5+ char * word ; /* points to the text */
6+ int count ; /* number of occurences */
7+ struct tnode * left ; /* left child */
8+ struct tnode * right ; /* right child */
9+ };
10+
11+ struct tnode * addtree (struct tnode * , char * );
12+
13+ void treeprint (struct tnode * p );
14+
15+ struct tnode * talloc (void );
16+
17+ #endif
You can’t perform that action at this time.
0 commit comments