From 27c63a26ef1ae312e08e23648d0221fd25cfe01f Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 12 Nov 2024 18:29:32 +0000 Subject: [PATCH] Initializacia --- cv7/program.c | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index e14a3aa..b8e0cfe 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -10,39 +10,35 @@ struct tree { struct tree* right; }; -// Function to read a single node from input struct tree* read_tree() { char buffer[SIZE]; if (fgets(buffer, SIZE, stdin) == NULL || buffer[0] == '\n') { - return NULL; // Return NULL for empty input + return NULL; } - buffer[strcspn(buffer, "\n")] = 0; // Remove newline character + buffer[strcspn(buffer, "\n")] = 0; struct tree* node = calloc(1, sizeof(struct tree)); strncpy(node->value, buffer, SIZE - 1); return node; } -// Function to load the tree structure from input struct tree* load_tree() { struct tree* node = read_tree(); if (!node) { return NULL; } - if (node->value[0] != '*') { // Not an answer, so load children + if (node->value[0] != '*') { node->left = load_tree(); node->right = load_tree(); } return node; } -// Function to run the tree and interact with the user void run_tree(struct tree* tree) { if (!tree) { return; } if (tree->value[0] == '*') { - // If it's an answer printf("*%s\nKoniec\n", tree->value + 1); return; } @@ -52,13 +48,11 @@ void run_tree(struct tree* tree) { char response; while (1) { - // Wait for user input if (scanf(" %c", &response) != 1 || (response != 'a' && response != 'n')) { printf("Nerozumiem\n"); - // Re-prompt for input continue; } - break; // Valid input received + break; } if (response == 'a') { @@ -68,7 +62,6 @@ void run_tree(struct tree* tree) { } } -// Function to free the memory allocated for the tree void destroy_tree(struct tree* tree) { if (tree) { destroy_tree(tree->left); @@ -77,11 +70,10 @@ void destroy_tree(struct tree* tree) { } } -// Function to count the number of unique items (answers) in the tree void count_items(struct tree* tree, int* count) { if (tree) { if (tree->left == NULL && tree->right == NULL) { - (*count)++; // It's a leaf node (an answer) + (*count)++; } else { count_items(tree->left, count); count_items(tree->right, count);