diff --git a/cv7/program.c b/cv7/program.c index 72a2de3..3e21483 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -10,7 +10,7 @@ struct tree { struct tree* right; }; -struct tree* read_tree(){ +struct tree* read_tree(int counter){ char buffer[SIZE]; memset(buffer, 0, SIZE); char* r = fgets(buffer, SIZE, stdin); @@ -18,24 +18,57 @@ struct tree* read_tree(){ return NULL; } assert(r); - struct tree* node = calloc(1, sizeof(struct tree)); - node->value = malloc(strlen(buffer) + 1); - assert(node->value); - node->left = read_tree(); - node->right = read_tree(); - return node; + struct tree* tree = calloc(1, sizeof(struct tree)); + tree->value = malloc(strlen(buffer) + 1); + strcpy(tree->value,r); + assert(tree->value); + if (buffer[0] == '*') { + counter++; + return tree; + } + tree->left = read_tree(counter); + tree->right = read_tree(counter); + return tree; } -void destroy_tree(struct tree* node) { - if (node == NULL) return; - destroy_tree(node->left); - destroy_tree(node->right); - free(node); +void destroy_tree(struct tree* tree) { + if (tree == NULL) return; + destroy_tree(tree->left); + destroy_tree(tree->right); + free(tree->value); + free(tree); +} + +void print_tree(struct tree* tree,int offset){ + char buffer[SIZE]; + memset(buffer, 0, SIZE); + for (int i = 0; i < offset && tree->value[0] != '*'; i++){ + printf(" "); + } + printf("%s",tree->value); + if(tree->value[0] == '*'){ + printf("Koniec\n"); + exit(0); + } + char r = getchar(); + if (r == 'a') { + print_tree(tree->left, offset + 3); + } else if (r == 'n') { + print_tree(tree->right, offset + 3); + } else { + printf("CHYBOVY VSTUP!\n"); + exit(0); + } } int main(){ - struct tree* root = read_tree(); - read_tree(); + int counter = 0; + int offset = 0; + struct tree* root = read_tree(counter); + printf("Expert z bufetu to vie.\n"); + printf("Pozna %d druhov ovocia a zeleniny.\n",counter); + printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); + print_tree(root,offset); destroy_tree(root); return 0; } \ No newline at end of file diff --git a/cv7/program.exe b/cv7/program.exe deleted file mode 100644 index e93aa93..0000000 Binary files a/cv7/program.exe and /dev/null differ