diff --git a/cv8/program.c b/cv8/program.c index e69de29..828a2a7 100644 --- a/cv8/program.c +++ b/cv8/program.c @@ -0,0 +1,139 @@ +#include +#include +#include +#include + + +#define SIZE 256 + +struct tree { + char v[SIZE]; + struct tree* left; + struct tree* right; +}; + + +struct tree* read_tree(int* counter); +void destroy_tree(struct tree* tree); +void print_tree(struct tree* tree, int offset); +void print_dot(struct tree* node); +int count_leaves(struct tree* node); +int count_no_leaves(struct tree* node); +void knowledge_system(struct tree* node); + +struct tree* read_tree(int* counter) { + char buffer[SIZE]; + memset(buffer, 0, SIZE); + char* r = fgets(buffer, SIZE, stdin); + assert(r); + + struct tree* node = calloc(1, sizeof(struct tree)); + memcpy(node->v, buffer, SIZE); + + if (buffer[0] != '*') { + node->left = read_tree(counter); + (*counter)++; + node->right = read_tree(counter); + (*counter)++; + } + + return node; +} + +void destroy_tree(struct tree* tree) { + if (tree) { + destroy_tree(tree->left); + destroy_tree(tree->right); + free(tree); + } +} + +void print_tree(struct tree* tree, int offset) { + for (int i = 0; i < offset; i++) { + printf(" "); + } + printf("%s", tree->v); + if (tree->left) { + print_tree(tree->left, offset + 3); + print_tree(tree->right, offset + 3); + } +} + +void print_dot(struct tree* node) { + if (node) { + print_dot(node->left); + printf("%s [label=\"%s\"];\n", node->v, node->v); + if (node->left) { + printf("%s -> %s;\n", node->v, node->left->v); + } + if (node->right) { + printf("%s -> %s;\n", node->v, node->right->v); + } + print_dot(node->right); + } + printf("}\n"); +} + +int count_leaves(struct tree* node) { + if (node == NULL) { + return 0; + } + if (node->left == NULL && node->right == NULL) { + return 1; + } + return count_leaves(node->left) + count_leaves(node->right); +} + +int count_no_leaves(struct tree* node) { + if (node == NULL || (node->left == NULL && node->right == NULL)) { + return 0; + } + } + return 1 + count_n_leaves(node->left) + count_n_leaves(node->right); +} + +void knowledge_system(struct tree* node) { + if (node == NULL) { + return; + } + + printf("%s", node->value); + if (node->left == NULL && node->right == NULL) { + printf("Koniec\n"); + return; + } + + printf("\nOdpovedajte 'a' alebo 'n'\n"); + + char response; + do { + printf("%s (a/n) ?\n", node->value); + scanf(" %c", &response); + if (response != 'a' && response != 'n') { + printf("Nespravny vstup\n"); + return; + } + } while (response != 'a' && response != 'n'); + + if (response == 'a') { + knowledge_system(node->left); + } else { + knowledge_system(node->right); + } +} + +int main() { + int counter = 0; + struct tree* root = read_tree(&counter); + + if (root == NULL) { + printf("Chybove hlasenie: Nepodarilo sa nacitat bazu pravidiel.\n"); + return 1; + } + + knowledge_system(root); + + destroy_tree(root); + + return 0; +} \ No newline at end of file