Skip to content

Commit 725a186

Browse files
committed
added Exercise 6-3
1 parent 08020f8 commit 725a186

File tree

6 files changed

+51
-15
lines changed

6 files changed

+51
-15
lines changed

Chapter-6/Exercise 6-3/io.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ int getword(char *word, int lim) {
3030
char *w = word;
3131

3232
/* skip the whitespaces */
33-
while (isspace(c = getch()))
33+
/*while (isspace(c = getch()))
34+
;*/
35+
while ((c = getch()) == ' ' || c == '\t')
3436
;
37+
//printf(" #%d# ", word[0]);
3538
if (c != EOF)
3639
*w++ = c;
3740
if (!isalpha(c)) {

Chapter-6/Exercise 6-3/itoa.c

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
int itoa(char *s) {
2-
int c, num = 0;
3-
int *np = s;
1+
#include <stdio.h>
42

5-
/* skip the whitespaces */
6-
while ((c = *np++) == ' ' || c == '\t')
7-
;
3+
void itoa(int a, char *s) {
4+
static int i = 0;
5+
if (a < 0) {
6+
s[i++] = '-';
7+
a *= -1;
8+
}
9+
if (a > 0) {
10+
itoa(a/10, s);
11+
s[i++] = '0' + a%10;
12+
//printf("%d %d\n", a, i++);
13+
}
814
}

Chapter-6/Exercise 6-3/itoa.h

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef ITOA_H
2+
#define ITOA_H
3+
4+
void itoa(int, char*);
5+
6+
#endif

Chapter-6/Exercise 6-3/main.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <string.h>
55
#include "tree.h"
66
#include "io.h"
7+
//#include "itoa.h"
78

89
#define MAXWORD 100
910

@@ -19,8 +20,9 @@ int main(int argc, char **argv) {
1920
root = NULL;
2021
while (getword(word, MAXWORD) != EOF) {
2122
if (isalpha(word[0]))
22-
root = addtree(root, word, itoa(lnum));
23+
root = addtree(root, word, lnum);
2324

25+
//printf(" *%s* ", word);
2426
if (word[0] == '\n')
2527
lnum++;
2628
}

Chapter-6/Exercise 6-3/tree.c

+21-5
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,26 @@
44
#include "io.h"
55
#include "tree.h"
66

7-
/* addtree: add a node with w, at or below p */
8-
struct tnode *addtree(struct tnode *p, char *w) {
7+
/* addtree: add a node with w, line n, at or below p */
8+
struct tnode *addtree(struct tnode *p, char *w, int n) {
99
int cond;
1010

1111
if (p == NULL) { /* a new word has arrived */
1212
p = talloc();
1313
p -> word = _strdup(w);
1414
p -> count = 1;
1515
p -> left = p -> right = NULL;
16-
} else if ((cond = strcmp(w, p -> word)) == 0)
16+
p -> lsize = 0;
17+
p -> lnum [p -> lsize ++] = n;
18+
} else if ((cond = strcmp(w, p -> word)) == 0) {
1719
p -> count++; /* repeated word */
20+
if (lsearch(p -> lnum, n, p -> lsize) < 0)
21+
p -> lnum[p -> lsize ++] = n;
22+
}
1823
else if (cond < 0)
19-
p -> left = addtree(p -> left, w);
24+
p -> left = addtree(p -> left, w, n);
2025
else
21-
p -> right = addtree(p -> right, w);
26+
p -> right = addtree(p -> right, w, n);
2227
return p;
2328
}
2429

@@ -27,6 +32,8 @@ void treeprint(struct tnode *p) {
2732
if (p != NULL) {
2833
treeprint(p -> left);
2934
printf("%4d %s\n", p -> count, p -> word);
35+
for (int i = 0 ; i < p -> lsize ; i++)
36+
printf("\t%4d\n", p -> lnum[i]);
3037
treeprint(p -> right);
3138
}
3239
}
@@ -35,3 +42,12 @@ void treeprint(struct tnode *p) {
3542
struct tnode *talloc(void) {
3643
return (struct tnode *) malloc(sizeof(struct tnode));
3744
}
45+
46+
/* lsearch: linear search for ele in arr[0] ... arr[size] */
47+
int lsearch(int *arr, int ele, int size) {
48+
for (int i = 0 ; i < size ; i++)
49+
if (arr[i] == ele)
50+
return i;
51+
/* ele not found in arr */
52+
return -1;
53+
}

Chapter-6/Exercise 6-3/tree.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@
66
struct tnode { /* the tree node: */
77
char *word; /* points to the text */
88
int count; /* number of occurences */
9-
struct tnode *lnum ; /* list of line number on which it occurs */
9+
int lnum[MAXLNUMBERS]; /* list of line number on which it occurs */
10+
int lsize; /* number of lines wheresword occurs */
1011
struct tnode *left; /* left child */
1112
struct tnode *right; /* right child */
1213
};
1314

14-
struct tnode *addtree(struct tnode *, char *);
15+
struct tnode *addtree(struct tnode *, char *, int);
1516

1617
void treeprint(struct tnode *p);
1718

1819
struct tnode *talloc(void);
1920

21+
int lsearch(int *, int, int);
22+
2023
#endif

0 commit comments

Comments
 (0)