2024-11-10 15:36:06 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2024-11-09 21:40:16 +00:00
|
|
|
|
|
|
|
#define SIZE 100
|
|
|
|
|
2024-11-10 15:36:06 +00:00
|
|
|
struct tree {
|
2024-11-09 21:40:16 +00:00
|
|
|
char value[SIZE];
|
|
|
|
struct tree* left;
|
|
|
|
struct tree* right;
|
|
|
|
};
|
|
|
|
|
2024-11-10 15:36:06 +00:00
|
|
|
struct tree* read_tree() {
|
2024-11-09 21:40:16 +00:00
|
|
|
char buffer[SIZE];
|
2024-11-12 19:12:14 +00:00
|
|
|
memset(buffer, 0, SIZE);
|
|
|
|
char* r = fgets(buffer, SIZE, stdin);
|
|
|
|
if (!r || buffer[0] == '\n') {
|
2024-11-12 18:29:32 +00:00
|
|
|
return NULL;
|
2024-11-12 12:50:45 +00:00
|
|
|
}
|
2024-11-12 18:29:32 +00:00
|
|
|
buffer[strcspn(buffer, "\n")] = 0;
|
2024-11-12 16:12:28 +00:00
|
|
|
struct tree* node = calloc(1, sizeof(struct tree));
|
2024-11-12 16:09:51 +00:00
|
|
|
strncpy(node->value, buffer, SIZE - 1);
|
|
|
|
return node;
|
|
|
|
}
|
2024-11-12 16:05:03 +00:00
|
|
|
|
2024-11-09 21:50:22 +00:00
|
|
|
struct tree* load_tree() {
|
|
|
|
struct tree* node = read_tree();
|
|
|
|
if (!node) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2024-11-12 18:29:32 +00:00
|
|
|
if (node->value[0] != '*') {
|
2024-11-09 21:50:22 +00:00
|
|
|
node->left = load_tree();
|
|
|
|
node->right = load_tree();
|
2024-11-09 21:40:16 +00:00
|
|
|
}
|
2024-11-12 16:05:03 +00:00
|
|
|
return node;
|
2024-11-12 16:03:36 +00:00
|
|
|
}
|
2024-11-09 21:40:16 +00:00
|
|
|
|
2024-11-10 15:38:32 +00:00
|
|
|
void run_tree(struct tree* tree) {
|
2024-11-10 15:36:06 +00:00
|
|
|
if (!tree) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tree->value[0] == '*') {
|
2024-11-12 19:12:14 +00:00
|
|
|
// If it's an answer
|
|
|
|
printf("*%s\nKoniec\n", tree->value + 1);
|
2024-11-10 15:36:06 +00:00
|
|
|
return;
|
2024-11-09 21:40:16 +00:00
|
|
|
}
|
2024-11-10 15:36:06 +00:00
|
|
|
|
2024-11-12 19:12:14 +00:00
|
|
|
printf("%s\n", tree->value);
|
2024-11-10 15:36:06 +00:00
|
|
|
|
|
|
|
char response;
|
2024-11-12 19:12:14 +00:00
|
|
|
if (scanf(" %c", &response) != 1) {
|
|
|
|
printf("Koniec vstupu\n");
|
|
|
|
return;
|
|
|
|
} else if (response != 'a' && response != 'n') {
|
|
|
|
printf("Nerozumiem\n");
|
|
|
|
return;
|
2024-11-10 15:36:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (response == 'a') {
|
2024-11-10 15:38:32 +00:00
|
|
|
run_tree(tree->left);
|
2024-11-10 15:36:06 +00:00
|
|
|
} else {
|
2024-11-10 15:38:32 +00:00
|
|
|
run_tree(tree->right);
|
2024-11-09 21:40:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-10 15:36:06 +00:00
|
|
|
void destroy_tree(struct tree* tree) {
|
2024-11-12 17:55:09 +00:00
|
|
|
if (tree) {
|
2024-11-12 16:14:39 +00:00
|
|
|
destroy_tree(tree->left);
|
|
|
|
destroy_tree(tree->right);
|
2024-11-12 17:55:09 +00:00
|
|
|
free(tree);
|
2024-11-09 21:40:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-10 15:36:06 +00:00
|
|
|
void count_items(struct tree* tree, int* count) {
|
2024-11-12 17:55:09 +00:00
|
|
|
if (tree) {
|
|
|
|
if (tree->left == NULL && tree->right == NULL) {
|
2024-11-12 18:29:32 +00:00
|
|
|
(*count)++;
|
2024-11-12 17:55:09 +00:00
|
|
|
} else {
|
|
|
|
count_items(tree->left, count);
|
|
|
|
count_items(tree->right, count);
|
|
|
|
}
|
2024-11-09 21:40:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-10 15:08:07 +00:00
|
|
|
int main() {
|
2024-11-12 17:57:20 +00:00
|
|
|
struct tree* root = load_tree();
|
2024-11-12 19:12:14 +00:00
|
|
|
printf("Expert z bufetu to vie.\n");
|
2024-11-12 16:09:51 +00:00
|
|
|
if (!root) {
|
2024-11-10 15:55:33 +00:00
|
|
|
printf("Chybna databaza\n");
|
2024-11-10 15:54:09 +00:00
|
|
|
return 0;
|
2024-11-10 15:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
count_items(root, &count);
|
|
|
|
printf("Pozna %d druhov ovocia a zeleniny.\n", count);
|
2024-11-12 19:12:14 +00:00
|
|
|
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
2024-11-10 15:12:57 +00:00
|
|
|
|
2024-11-10 15:38:32 +00:00
|
|
|
run_tree(root);
|
2024-11-12 19:12:14 +00:00
|
|
|
|
2024-11-10 15:08:07 +00:00
|
|
|
destroy_tree(root);
|
|
|
|
return 0;
|
2024-11-12 16:22:00 +00:00
|
|
|
}
|