-
Notifications
You must be signed in to change notification settings - Fork 0
/
A2_interactive_driver.c
157 lines (139 loc) · 4.03 KB
/
A2_interactive_driver.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* CSC A48 - Intro to Computer Science II, Summer 2020
*
* This file provides an interactive test driver to help you check your
* work as you work on A2.
*
* This is provided *only* with the intent of helping you test. It's up
* to you to use this to thoroughly check that everything works in your
* code.
*
* Remember: You are expected to test for *correctness*, that means
* it's not enough for your functions to produce the right result,
* they have to do so by manipulating all the relevant data correctly.
* You should check for structure and correctness at every step.
*
* (c) 2020 Mustafa Quraish
*/
#include "Quad.c"
#include <string.h>
void getInt(char *prompt, int *var) {
printf("%s = ", prompt);
scanf("%d", var);
getchar();
}
void getStr(char *prompt, char *var) {
printf("%s = ", prompt);
fgets(&var[0], 1024, stdin);
int i = strlen(&var[0]);
if (var[i - 1] == '\n')
var[i - 1] = '\0';
}
int main() {
int tx, ty, w, h, wsplit, choice, i;
int sx = 512, threshold, mode;
Quad *root = NULL;
Quad *new_note = NULL;
Quad *t = NULL;
Image *im = NULL, *im2;
char name[1024];
choice = 0;
while (choice != 9) {
if (im == NULL) {
printf(" No image loaded, default sx = %d\n", sx);
} else {
printf(" Image loaded, sx = %d\n", sx);
}
printf("Please select from among the following options:\n");
printf("0 - Insert new Quad\n");
printf("1 - Search for Quad\n");
printf("2 - Delete Quad\n");
printf("3 - Print Quads in order\n");
printf("4 - Print Quads in pre-order\n");
printf("5 - Print Quads in post-order\n");
printf("6 - Load Image\n");
printf("7 - Split Tree\n");
printf("8 - Save Image\n");
printf("9 - Delete BST and exit\n");
getInt("Enter choice", &choice);
printf("------------------------------------------------\n");
if (choice == 0) {
getInt("tx", &tx);
getInt("ty", &ty);
getInt("w", &w);
getInt("h", &h);
getInt("wsplit (0/1)", &wsplit);
new_note = new_Quad(tx, ty, w, h, wsplit, sx);
root = BST_insert(root, new_note);
}
if (choice == 1) {
getInt("tx", &tx);
getInt("ty", &ty);
t = BST_search(root, tx, ty);
if (t != NULL) {
printf("Found Quad at tx:ty (%d:%d), w=%d, h=%d, wsplit=%d\n", tx, ty,
t->w, t->h, t->wsplit);
} else {
printf("No such Quad found in the tree\n");
}
}
if (choice == 2) {
getInt("tx", &tx);
getInt("ty", &ty);
root = BST_delete(root, tx, ty);
}
if (choice == 3) {
BST_inorder(root, 0);
}
if (choice == 4) {
BST_preorder(root, 0);
}
if (choice == 5) {
BST_postorder(root, 0);
}
if (choice == 6) {
printf("Note: BST will be reset\n");
getStr("Name of the image (in .pgm format)", name);
im = readPGMimage(name);
if (im != NULL) {
delete_BST(root);
root = NULL;
printf("Image loaded with size = %d x %d\n", im->sx, im->sy);
sx = im->sx;
}
}
if (choice == 7) {
if (im == NULL) {
printf("Please load an image first.\n");
} else {
getInt("threshold [0-255]", &threshold);
getInt("Number of times to split", &h);
for (i = 0; i < h; i++)
root = split_tree(im, root, threshold);
}
}
if (choice == 8) {
if (im == NULL) {
printf("Please load an image first.\n");
} else {
printf("Modes: 0 - outlines\n");
printf(" 1 - outlines with expected colour\n");
printf(" 2 - expected colour\n");
getInt("mode (0/1/2)", &mode);
im2 = copyImage(im);
if (mode > 0) {
save_Quad(im2, root);
}
if (mode < 2) {
drawOutline(im2, root, 128);
}
imageOutput(im2, "output.pgm");
deleteImage(im2);
}
}
printf("------------------------------------------------\n");
} // Enf while (choice!=9)
delete_BST(root);
deleteImage(im);
return 0;
}