du5
This commit is contained in:
parent
b2208b5cad
commit
3510447854
@ -14,13 +14,10 @@ typedef struct Tree {
|
||||
Tree* readTree() {
|
||||
char line[SIZE];
|
||||
|
||||
if (fgets(line, SIZE, stdin) == NULL) return NULL;
|
||||
|
||||
if (!fgets(line, SIZE, stdin)) return NULL;
|
||||
line[strcspn(line, "\r\n")] = 0;
|
||||
|
||||
if (line[0] == '\0') return NULL;
|
||||
|
||||
Tree *node = (Tree*)malloc(sizeof(Tree));
|
||||
Tree *node = malloc(sizeof(Tree));
|
||||
node->yes = NULL;
|
||||
node->no = NULL;
|
||||
|
||||
@ -41,38 +38,40 @@ Tree* readTree() {
|
||||
return node;
|
||||
}
|
||||
|
||||
int countLeaves(Tree *node) {
|
||||
if (!node) return 0;
|
||||
if (node->isAnswer) return 1;
|
||||
return countLeaves(node->yes) + countLeaves(node->no);
|
||||
int count(Tree *n) {
|
||||
if (!n) return 0;
|
||||
if (n->isAnswer) return 1;
|
||||
return count(n->yes) + count(n->no);
|
||||
}
|
||||
|
||||
void freeTree(Tree *node) {
|
||||
if (!node) return;
|
||||
freeTree(node->yes);
|
||||
freeTree(node->no);
|
||||
free(node);
|
||||
void freeTree(Tree *n) {
|
||||
if (!n) return;
|
||||
freeTree(n->yes);
|
||||
freeTree(n->no);
|
||||
free(n);
|
||||
}
|
||||
|
||||
void start(Tree *node) {
|
||||
char input[SIZE];
|
||||
void start(Tree *n) {
|
||||
char in[SIZE];
|
||||
|
||||
while (node && !node->isAnswer) {
|
||||
printf("%s\n", node->text);
|
||||
while (n && !n->isAnswer) {
|
||||
printf("%s\n", n->text);
|
||||
|
||||
if (fgets(input, SIZE, stdin) == NULL) return;
|
||||
if (!fgets(in, SIZE, stdin)) return;
|
||||
|
||||
int i = 0;
|
||||
while (input[i] == ' ' || input[i] == '\t') i++;
|
||||
while (in[i] == ' ' || in[i] == '\t' || in[i] == '\r') i++;
|
||||
|
||||
if (input[i] == 'a')
|
||||
node = node->yes;
|
||||
if (in[i] == 'a')
|
||||
n = n->yes;
|
||||
else if (in[i] == 'n')
|
||||
n = n->no;
|
||||
else
|
||||
node = node->no;
|
||||
return;
|
||||
}
|
||||
|
||||
if (node && node->isAnswer) {
|
||||
printf("* %s\n", node->text);
|
||||
if (n) {
|
||||
printf("*%s\n", n->text);
|
||||
printf("Koniec\n");
|
||||
}
|
||||
}
|
||||
@ -87,7 +86,7 @@ int main() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
printf("Pozna %d druhov ovocia a zeleniny.\n", countLeaves(root));
|
||||
printf("Pozna %d druhov ovocia a zeleniny.\n", count(root));
|
||||
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
||||
|
||||
start(root);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user