#include #include #include #define SIZE 100 struct tree { char value[SIZE]; struct tree* left; struct tree* right; }; struct tree* read_tree() { char buffer[SIZE]; memset(buffer, 0, SIZE); char* r = fgets(buffer, SIZE, stdin); if (!r || buffer[0] == '\n') { return NULL; } buffer[strcspn(buffer, "\n")] = 0; struct tree* node = calloc(1, sizeof(struct tree)); strncpy(node->value, buffer, SIZE - 1); return node; } struct tree* load_tree() { struct tree* node = read_tree(); if (!node) { return NULL; } if (node->value[0] != '*') { node->left = load_tree(); node->right = load_tree(); } else if (node->left != NULL || node->right != NULL) { printf("Chybna databaza\n"); free(node); return NULL; } return node; } void run_tree(struct tree* tree) { if (!tree) { return; } if (tree->value[0] == '*') { printf("*%s\nKoniec\n", tree->value + 1); return; } printf("%s\n", tree->value); char response; if (scanf(" %c", &response) != 1) { printf("Koniec vstupu\n"); return; } else if (response != 'a' && response != 'n') { printf("Nerozumiem\n"); return; } if (response == 'a') { run_tree(tree->left); } else { run_tree(tree->right); } } void destroy_tree(struct tree* tree) { if (tree) { destroy_tree(tree->left); destroy_tree(tree->right); free(tree); } } void count_items(struct tree* tree, int* count) { if (tree) { if (tree->left == NULL && tree->right == NULL) { (*count)++; } else { count_items(tree->left, count); count_items(tree->right, count); } } } int main() { struct tree* root = load_tree(); printf("Expert z bufetu to vie.\n"); if (!root) { printf("Chybna databaza\n"); return 0; } int count = 0; count_items(root, &count); printf("Pozna %d druhov ovocia a zeleniny.\n", count); printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); run_tree(root); destroy_tree(root); return 0; }