From 55853490b25834957e6408113794b8250c993f89 Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 12 Nov 2024 15:48:07 +0000 Subject: [PATCH] Initializacia --- cv7/program.c | 42 +++++++++++------------------------------- 1 file changed, 11 insertions(+), 31 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index 657f883..266a371 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -13,47 +13,29 @@ struct tree { struct tree* read_tree() { char buffer[SIZE]; - - while (fgets(buffer, SIZE, stdin)) { - buffer[strcspn(buffer, "\n")] = 0; - - if (buffer[0] == '\0') { - continue; - } - - struct tree* node = calloc(1, sizeof(struct tree)); - strncpy(node->value, buffer, SIZE - 1); - return node; + memset(buffer, 0, SIZE); + char* r = fgets(buffer, SIZE, stdin); + if (!r || buffer[0] == '\n') { + return NULL; } - - 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] != '*') { + if (node->value[0] != '*') { node->left = load_tree(); node->right = load_tree(); - - if (!node->left || !node->right) { - destroy_tree(node); - return NULL; - } } - return node; } - - - - void run_tree(struct tree* tree) { if (!tree) { return; @@ -104,8 +86,7 @@ void count_items(struct tree* tree, int* count) { int main() { struct tree* root = load_tree(); printf("Expert z bufetu to vie.\n"); - - if (!root) { + if (!root) { printf("Chybna databaza\n"); return 0; } @@ -116,8 +97,7 @@ int main() { printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); run_tree(root); + destroy_tree(root); return 0; } - -