-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
138 lines (132 loc) · 2.77 KB
/
main.c
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "RBtree.h"
#include <stdio.h>
#include <ctype.h>
#define READFILE "RBinput.txt"
#define DRAWFILE "RBdrawing.svg"
void help() {
printf(
"Commands:\n"
"\tC - Create empty tree\n"
"\tR - Read tree from %s\n"
"\tW - Write tree to screen in preorder format\n"
"\tI n - Insert node with key `n' into tree\n"
"\tD n - Delete node with key `n' from tree\n"
"\tP - draw Picture in %s\n"
"\tH - Help\n"
"\tS - Stop\n",
READFILE, DRAWFILE);
}
int main(int argc, char *argv[]) {
rb_tree tree = NULL,
tmp = NULL;
int cmd = 0;
printf("Louis Wilson's CSE310 Project #2\n");
help();
while (cmd != EOF) {
int arg;
printf("$ ");
fflush(stdout);
/* Find the first non-whitespace character */
while (isspace((cmd = getchar()))) {
if (cmd == '\n') {
/* We delete up to end of line after the
* switch, so put the \n back on. */
ungetc(cmd, stdin);
break;
}
}
switch(cmd) {
case 'C':
case 'c':
/* If we already had a tree, we need to free up
* the memory. */
if (tree != NULL) {
printf("Tree already exists - clearing.\n");
RBfree(tree);
}
tree = RBcreate();
break;
case 'R':
case 'r':
tmp = RBread(READFILE);
/* If there was an error, just keep the tree we
* have. */
if (tmp != NULL) {
if (tree != NULL) {
printf("Non-empty tree - overwriting.\n");
RBfree(tree);
}
tree = tmp;
}
break;
case 'W':
case 'w':
if (tree == NULL) {
fprintf(stderr, "Error: no tree loaded, cannot write.\n");
} else {
RBwrite(tree);
}
break;
case 'I':
case 'i':
if (tree == NULL) {
printf("No tree loaded - creating empty one.\n");
tree = RBcreate();
}
if (scanf("%d", &arg) != 1) {
fprintf(stderr, "Error: must specify integer key to insert.\n");
} else {
RBinsert(tree, arg);
}
break;
case 'D':
case 'd':
if (tree == NULL) {
fprintf(stderr, "Error: no tree loaded, cannot delete.\n");
} else {
if (scanf("%d", &arg) != 1) {
fprintf(stderr, "Error: must specify integer key to delete.\n");
} else {
RBdelete(tree, arg);
}
}
break;
case 'P':
case 'p':
if (tree == NULL) {
fprintf(stderr, "Error: no tree loaded, cannot draw.\n");
} else {
RBdraw(tree, DRAWFILE);
}
break;
case 'H':
case 'h':
help();
break;
case EOF:
/* Make the shell not return on the same line */
putchar('\n');
case 'S':
case 's':
cmd = EOF;
break;
/* Corresponds to an empty command */
case '\n':
break;
default:
fprintf(stderr, "Error: unknown command `%c'.\n", cmd);
help();
break;
}
if (cmd != EOF) {
/* Delete the rest of the line. */
while ((cmd = getchar()) != '\n');
}
}
/* We need to free the tree. */
if (tree != NULL) {
RBfree(tree);
}
RBcleanup();
return 0;
}