-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimal2.c
157 lines (136 loc) · 3.73 KB
/
animal2.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
#ifndef ANIMAL_H
#include "animal.h"
#endif
#define MAXSTRLEN 80
#define MAXNUMQS 32
#define FALSE 0
#define TRUE !FALSE
struct treeStruct {
char *string;
struct treeStruct *left, *right;
};
struct positionStruct {
struct treeStruct* node;
};
TreeType addNode(TreeType parent, char *str, boolean left){
TreeType child = malloc(sizeof(TreeType)+50);
child->string = calloc(MAXSTRLEN,1);
child->right = NULL; child->left = NULL;
strcpy(child->string,str);
if (left){
parent->left = child;
} else{
parent->right = child;
}
return child;
}
TreeType InitTree (char *file) {
TreeType tree = malloc(sizeof(TreeType));
tree->string = calloc(MAXSTRLEN,1);
strcpy(tree->string,"Is it furry?");
tree->right = NULL; tree->left = NULL;
TreeType one = addNode(tree,"Does it meow?", true);
TreeType two = addNode(tree,"Does it have tusks?", false);
addNode(one,"a cat", true);
addNode(one,"a dog", false);
addNode(two,"a rhino", true);
addNode(two,"a gecko", false);
return tree;
}
void PrintTree(TreeType tree){
printf("%s\n",tree->string);
if (tree->left !=NULL){
PrintTree(tree->left);
}
if (tree->right!=NULL){
PrintTree(tree->right);
}
return;
}
PositionType Top (TreeType tree){
PositionType pos = malloc(sizeof(PositionType)+30);
pos->node = tree;
return pos;
}
boolean IsLeaf(TreeType tree, PositionType pos){
if (!(pos->node->left || pos->node->right)){
return true;
} else {
return false;
}
}
boolean Answer (char *question){
if (question != NULL){
printf("%s\n",question);
char * answer;
answer = getUserInput();
if (answer[0] == 'y'){
return TRUE;
} else {
return FALSE;
}
} else {
printf("Passed in a null question! Returning False\n");
return FALSE;
}
}
char * getUserInput(){
char * str = calloc(MAXSTRLEN,sizeof(char));
int i = 0; int c;
while ((c = getchar())!= '\n'){
str[i] = c;
i++;
}
return str;
}
char *Question (TreeType tree, PositionType pos){
if (strlen(pos->node->string) >0){
char * question;
question = pos->node->string;
return question;
} else {
printf("Tried to get question but failed\n");
PrintTree(tree);
return NULL;
}
}
char *Guess (TreeType tree, PositionType pos){
char *guess = calloc(MAXSTRLEN,sizeof(char));
if ((strlen(pos->node->string) != 0)){
sprintf(guess, "%s%s%s", "is it ",pos->node->string,"?");
return guess;
} else {
printf("Called guess on leaf w/ length 0\n");
return guess;
}
}
PositionType YesNode (TreeType tree, PositionType pos){
PositionType newpos = malloc(sizeof(PositionType) + 20);
newpos->node = pos->node->left;
return newpos;
}
PositionType NoNode (TreeType tree, PositionType pos){
PositionType newpos = malloc(sizeof(PositionType) + 20);
newpos->node = pos->node->right;
return newpos;
}
void ReplaceNode (TreeType tree, PositionType pos, char *newA, char *newQ){
char * old = pos->node->string;
//tree->nodes[pos->nodeIndex] = calloc(MAXSTRLEN,sizeof(char));
pos->node->string = newQ;
addNode(pos->node, old, true);
addNode(pos->node, newA, false);
return;
}
void GetNewInfo (TreeType tree, PositionType pos, char **newA, char **newQ){
char * old;
printf("I give up. What is it?");
*newA = getUserInput();
old = pos->node->string;//tree->nodes[pos->nodeIndex];
printf("Provide a question whose answer is yes for %s and no for %s\n",old,*newA);
*newQ = getUserInput();
return;
}
void WriteTree(TreeType tree, char *file){
return;
}