diff --git a/du6/program.c b/du6/program.c index 7dbc15c..543056c 100644 --- a/du6/program.c +++ b/du6/program.c @@ -10,14 +10,6 @@ typedef struct tree { struct tree *right; } Tree; -int valid_db = 1; - -//odstranit koncovy znak -void strip_newline(char *s) { - int len = strlen(s); - if (len > 0 && s[len-1] == '\n') - s[len-1] = '\0'; -} // rekurzivne nacitanie stromu v preorder Tree* read_tree() { @@ -30,110 +22,92 @@ Tree* read_tree() { if (strcmp(buffer, "\n") == 0) return NULL; - strip_newline(buffer); - Tree *node = calloc(1, sizeof(Tree)); - if (!node) exit(1); - strcpy(node->value, buffer); // odpoved -> list if (buffer[0] == '*') { - return node; + node->left = NULL; + node->right = NULL; + } else { + node->left = read_tree(); + node->right = read_tree(); } - - // inak nacitat oboch potomkov - node->left = read_tree(); - if (!node->left) { - valid_db = 0; - free(node); - return NULL; - } - - node->right = read_tree(); - if (!node->right) { - free(node->left); - valid_db = 0; - free(node); - return NULL; - } - return node; } -// uvolnenie pamate -void destroy_tree(Tree *t) { - if (!t) return; - destroy_tree(t->left); - destroy_tree(t->right); - free(t); +void destroy_tree(Tree *tree) { + if (!tree) return; + destroy_tree(tree->left); + destroy_tree(tree->right); + free(tree); } - -// pocet listov = pocet tovarov -int count_leaves(Tree *t) { - if (!t) return 0; - if (!t->left && !t->right) + +int count_leaves(Tree *tree) { + if (!tree) return 0; + if (!tree->left && !tree->right) return 1; return count_leaves(t->left) + count_leaves(t->right); } // spustenie znalostneho systemu -void run_system(Tree *node) { - if (!node) return; +int run_system(Tree *node) { + if (node == NULL){ + return 1; + } - printf("%s\n", node->value); + if (node->value[0] == '*'){ + printf("%s\n", node->value); + return 1; + } // list -> koniec if (!node->left && !node->right) { - printf("Koniec\n"); - return; + printf("Koniec vstupu\n"); + return 0; } int c; - - do { - c = getchar(); - if (c == EOF) { - printf("Koniec vstupu\n"); - return; - } - } while (c=='\n' || c=='\r' || c==' ' || c=='\t'); - if (c == 'a') { - run_system(node->left); + if (node->left != NULL){ + return run_system(node->left); + } } else if (c == 'n') { - run_system(node->right); + if (node->right != NULL){ + return run_system(node->right); + } } else { printf("Nerozumiem\n"); + return 0; } + return 1; } int main() { Tree *root = read_tree(); - - // nepodarilo sa nacitat koren -> chyba - if (!root || !valid_db) { - printf("Expert z bufetu to vie.\n"); - printf("Chybna databaza\n"); - return 0; + printf("Expert z bufetu to vie.\n"); + + if (root == NULL){ + printf("Chybna databaza\n"); + return 0; } // skontrolovat prazdny riadok char buffer[SIZE]; - if (!fgets(buffer, SIZE, stdin) || strcmp(buffer, "\n") != 0) { - printf("Koniec vstupu\n"); + if (!fgets(buffer, SIZE, stdin) || strlen(buffer) > 1) { + printf("Chybna databaza\n"); destroy_tree(root); return 0; } int leaves = count_leaves(root); - printf("Expert z bufetu to vie.\n"); printf("Pozna %d druhov ovocia a zeleniny.\n", leaves); printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); - - run_system(root); - + int continueworking = run_system(root); + if (continueworking){ + printf("Koniec\n"); + } destroy_tree(root); return 0; }