-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisptree.c
More file actions
86 lines (76 loc) · 1.32 KB
/
disptree.c
File metadata and controls
86 lines (76 loc) · 1.32 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#define SIZ 100
int getword(char *word)
{
int i = 0;
char c = ' ';
while (c != EOF && c <= ' ')
c = getc(stdin);
if (c == EOF)
return 0;
while (c != EOF && c > ' ') {
if (i >= SIZ-1)
return -1;
i++;
*word++ = c;
if (c == '(' || c == ')') break;
c = getc(stdin);
if (c == '(' || c == ')') {
ungetc(c, stdin);
break;
}
}
*word = '\0';
return 1;
}
void err(char *mess)
{
printf("%s\n", mess);
exit(0);
}
indent(int n)
{
int d;
for (d=0; d<n; d++)
putchar(' ');
}
void tree(int depth)
{
int i = 0;
char word[SIZ];
i = getword(word);
if (i < 1) err("no tree");
if (strcmp(word, "(") == 0)
err("badly formed tree");
indent(depth);
printf("%s\n", word);
while (1) {
i = getword(word);
if (i < 1) break;
if (strcmp(word, "(") == 0)
tree(depth+1);
else
if (strcmp(word, ")") == 0)
break;
else {
indent(depth+1);
printf("%s\n", word);
}
}
if (i < 0) err("token too long");
if (i = 0) err("badly formed tree");
}
main(int argc, char *argv[])
{
int i = 0;
char word[SIZ];
while (1) {
i = getword(word);
if (i < 0) err("token too long");
if (i = 0) err("no tree");
if (strcmp(word, "(") == 0) break;
}
tree(0);
}