From 00d8213551cf348a2cc36efc11ea874d09a2a7ed Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 10 Nov 2024 15:12:57 +0000 Subject: [PATCH 01/51] Initializacia --- cv7/program.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index cbda220..a237407 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -64,9 +64,7 @@ void count_items(struct tree* tree, int* count){ } int main() { - printf("Nacitanie baze pravidiel:\n"); struct tree* root = load_tree(); - if (!root) { printf("Chyba: Nepodarilo sa nacitat bazu pravidiel.\n"); return 1; @@ -75,8 +73,9 @@ int main() { int count = 0; count_items(root, &count); printf("Pozna %d druhov ovocia a zeleniny.\n", count); - printf("Strom pre ladenie:\n"); - print_tree(root, 0); + printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); + + run_tree(root); destroy_tree(root); return 0; From 7a96dc2c179ff634266558e1f1ff10b2cadbe89c Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 10 Nov 2024 15:14:53 +0000 Subject: [PATCH 02/51] Initializacia --- cv7/program.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cv7/program.c b/cv7/program.c index a237407..c363c36 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -75,7 +75,7 @@ int main() { printf("Pozna %d druhov ovocia a zeleniny.\n", count); printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); - run_tree(root); + print_tree(root); destroy_tree(root); return 0; From ea6d529fad064f53da29a6fc8d6fc3f691c92b33 Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 10 Nov 2024 15:36:06 +0000 Subject: [PATCH 03/51] Initializacia --- cv7/program.c | 67 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 25 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index c363c36..970b30e 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -1,23 +1,26 @@ -#include -#include -#include -#include +#include +#include +#include +#include #define SIZE 100 -struct tree{ +struct tree { char value[SIZE]; struct tree* left; struct tree* right; }; -struct tree* read_tree(){ +struct tree* read_tree() { 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->value,buffer,SIZE); + memset(buffer, 0, SIZE); + char* r = fgets(buffer, SIZE, stdin); + if (!r || buffer[0] == '\n') { + return NULL; + } + buffer[strcspn(buffer, "\n")] = 0; + struct tree* node = calloc(1, sizeof(struct tree)); + strncpy(node->value, buffer, SIZE - 1); return node; } @@ -33,33 +36,47 @@ struct tree* load_tree() { return node; } -void print_tree(struct tree* tree, int offset){ - for (int i = 0; i < offset; i++){ - printf(" "); +void print_tree(struct tree* tree, int offset) { + if (!tree) { + return; } - printf("%s", tree->value); - if (tree->left){ + + if (tree->value[0] == '*') { + printf("Expert z bufetu to vie.\n%s\nKoniec\n", tree->value + 1); + return; + } + + printf("%s\n", tree->value); + + char response; + if (scanf(" %c", &response) != 1 || (response != 'a' && response != 'n')) { + printf("Nespravny vstup. Koniec.\n"); + return; + } + + if (response == 'a') { print_tree(tree->left, offset + 3); + } else { print_tree(tree->right, offset + 3); } } -void destroy_tree(struct tree* tree){ - if(tree->left != NULL){ +void destroy_tree(struct tree* tree) { + if (tree->left != NULL) { destroy_tree(tree->left); } - if(tree->right != NULL){ + if (tree->right != NULL) { destroy_tree(tree->right); } free(tree); } -void count_items(struct tree* tree, int* count){ - if(tree->left == NULL && tree->right == NULL){ +void count_items(struct tree* tree, int* count) { + if (tree->left == NULL && tree->right == NULL) { (*count)++; - }else{ - count_items(tree->left, count); - count_items(tree->right, count); + } else { + count_items(tree->left, count); + count_items(tree->right, count); } } @@ -75,7 +92,7 @@ int main() { printf("Pozna %d druhov ovocia a zeleniny.\n", count); printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); - print_tree(root); + print_tree(root, 0); destroy_tree(root); return 0; From 9e1a60c36d8c34adad3b99caa8ad0b392b83b800 Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 10 Nov 2024 15:38:32 +0000 Subject: [PATCH 04/51] Initializacia --- cv7/program.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index 970b30e..2b9e617 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -36,13 +36,14 @@ struct tree* load_tree() { return node; } -void print_tree(struct tree* tree, int offset) { +void run_tree(struct tree* tree) { if (!tree) { return; } if (tree->value[0] == '*') { printf("Expert z bufetu to vie.\n%s\nKoniec\n", tree->value + 1); + printf("*"); return; } @@ -55,9 +56,9 @@ void print_tree(struct tree* tree, int offset) { } if (response == 'a') { - print_tree(tree->left, offset + 3); + run_tree(tree->left); } else { - print_tree(tree->right, offset + 3); + run_tree(tree->right); } } @@ -92,7 +93,7 @@ int main() { printf("Pozna %d druhov ovocia a zeleniny.\n", count); printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); - print_tree(root, 0); + run_tree(root); destroy_tree(root); return 0; From 9ba09a9a3a9cb09248383b3e16ec51ca6fb31c57 Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 10 Nov 2024 15:40:39 +0000 Subject: [PATCH 05/51] Initializacia --- cv7/program.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index 2b9e617..491f52f 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -42,8 +42,7 @@ void run_tree(struct tree* tree) { } if (tree->value[0] == '*') { - printf("Expert z bufetu to vie.\n%s\nKoniec\n", tree->value + 1); - printf("*"); + printf("Expert z bufetu to vie.\n*%s\nKoniec\n", tree->value + 1); return; } From 7f3b45475686e84aedcd316cf7cc785fbeef2785 Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 10 Nov 2024 15:43:02 +0000 Subject: [PATCH 06/51] Initializacia --- cv7/program.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cv7/program.c b/cv7/program.c index 491f52f..31ed927 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -42,7 +42,7 @@ void run_tree(struct tree* tree) { } if (tree->value[0] == '*') { - printf("Expert z bufetu to vie.\n*%s\nKoniec\n", tree->value + 1); + printf("*%s\nKoniec\n", tree->value + 1); return; } @@ -89,6 +89,7 @@ int main() { int count = 0; count_items(root, &count); + printf("Expert z bufetu to vie.\n"); printf("Pozna %d druhov ovocia a zeleniny.\n", count); printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); From a7794811d7b794a09163a948e7291a72c1422f0b Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 10 Nov 2024 15:43:59 +0000 Subject: [PATCH 07/51] Initializacia --- cv7/program.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cv7/program.c b/cv7/program.c index 31ed927..b18b7b2 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -50,7 +50,7 @@ void run_tree(struct tree* tree) { char response; if (scanf(" %c", &response) != 1 || (response != 'a' && response != 'n')) { - printf("Nespravny vstup. Koniec.\n"); + printf("Nerozumiem\n"); return; } From f9bc3d6d6e39885334f7dfafebc4a92e4ff8250b Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 10 Nov 2024 15:51:00 +0000 Subject: [PATCH 08/51] Initializacia --- cv7/program.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cv7/program.c b/cv7/program.c index b18b7b2..72fed70 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -49,10 +49,14 @@ void run_tree(struct tree* tree) { printf("%s\n", tree->value); char response; - if (scanf(" %c", &response) != 1 || (response != 'a' && response != 'n')) { + if (scanf(" %c", &response) != 1) { + printf("Koniec vstupu\n"); + return; + if else(response != 'a' && response != 'n'){ printf("Nerozumiem\n"); return; } + } if (response == 'a') { run_tree(tree->left); From cda4f030d6b84e6f57456d958274b326469a8f4e Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 10 Nov 2024 15:51:54 +0000 Subject: [PATCH 09/51] Initializacia --- cv7/program.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cv7/program.c b/cv7/program.c index 72fed70..2549acb 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -52,7 +52,7 @@ void run_tree(struct tree* tree) { if (scanf(" %c", &response) != 1) { printf("Koniec vstupu\n"); return; - if else(response != 'a' && response != 'n'){ + else if(response != 'a' && response != 'n'){ printf("Nerozumiem\n"); return; } From 1b9c8f4a2eb09e657f8732017fd70a7e99563d80 Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 10 Nov 2024 15:53:15 +0000 Subject: [PATCH 10/51] Initializacia --- cv7/program.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index 2549acb..bbb96d5 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -52,11 +52,10 @@ void run_tree(struct tree* tree) { if (scanf(" %c", &response) != 1) { printf("Koniec vstupu\n"); return; - else if(response != 'a' && response != 'n'){ + } else if (response != 'a' && response != 'n') { printf("Nerozumiem\n"); return; } - } if (response == 'a') { run_tree(tree->left); From e94c43f798674960c6836a81173ebfa7c2245468 Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 10 Nov 2024 15:54:09 +0000 Subject: [PATCH 11/51] Initializacia --- cv7/program.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cv7/program.c b/cv7/program.c index bbb96d5..cd0c7bd 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -87,7 +87,7 @@ int main() { struct tree* root = load_tree(); if (!root) { printf("Chyba: Nepodarilo sa nacitat bazu pravidiel.\n"); - return 1; + return 0; } int count = 0; From 57556018ea5ef7bfb395f9f392fd4050525f942c Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 10 Nov 2024 15:55:33 +0000 Subject: [PATCH 12/51] Initializacia --- cv7/program.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index cd0c7bd..266a371 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -85,14 +85,14 @@ void count_items(struct tree* tree, int* count) { int main() { struct tree* root = load_tree(); + printf("Expert z bufetu to vie.\n"); if (!root) { - printf("Chyba: Nepodarilo sa nacitat bazu pravidiel.\n"); + printf("Chybna databaza\n"); return 0; } int count = 0; count_items(root, &count); - printf("Expert z bufetu to vie.\n"); printf("Pozna %d druhov ovocia a zeleniny.\n", count); printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); From 8109d836ab3ffa9744d0e2a7991cb711d86bdfbc Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 10 Nov 2024 15:59:31 +0000 Subject: [PATCH 13/51] Initializacia --- cv7/program.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index 266a371..ce7a8a8 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -1,6 +1,5 @@ #include #include -#include #include #define SIZE 100 @@ -11,6 +10,8 @@ struct tree { struct tree* right; }; +int error_flag = 0; + struct tree* read_tree() { char buffer[SIZE]; memset(buffer, 0, SIZE); @@ -20,6 +21,11 @@ struct tree* read_tree() { } buffer[strcspn(buffer, "\n")] = 0; struct tree* node = calloc(1, sizeof(struct tree)); + if (buffer[0] == '*') { + if (strlen(buffer) <= 1) { + error_flag = 1; + } + } strncpy(node->value, buffer, SIZE - 1); return node; } @@ -29,7 +35,7 @@ struct tree* load_tree() { if (!node) { return NULL; } - if (node->value[0] != '*') { + if (error_flag || node->value[0] != '*') { node->left = load_tree(); node->right = load_tree(); } @@ -65,16 +71,18 @@ void run_tree(struct tree* tree) { } void destroy_tree(struct tree* tree) { - if (tree->left != NULL) { - destroy_tree(tree->left); - } - if (tree->right != NULL) { - destroy_tree(tree->right); + if (!tree) { + return; } + destroy_tree(tree->left); + destroy_tree(tree->right); free(tree); } void count_items(struct tree* tree, int* count) { + if (!tree) { + return; + } if (tree->left == NULL && tree->right == NULL) { (*count)++; } else { @@ -86,8 +94,9 @@ 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 || error_flag) { printf("Chybna databaza\n"); + destroy_tree(root); return 0; } From bdb10349f2af75105386d06c0f9b4b2b8fa644e7 Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 10 Nov 2024 16:02:46 +0000 Subject: [PATCH 14/51] Initializacia --- cv7/program.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index ce7a8a8..6261790 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -20,12 +20,12 @@ struct tree* read_tree() { return NULL; } buffer[strcspn(buffer, "\n")] = 0; - struct tree* node = calloc(1, sizeof(struct tree)); - if (buffer[0] == '*') { - if (strlen(buffer) <= 1) { - error_flag = 1; - } + + if (buffer[0] == '*' && buffer[1] == ' ') { + error_flag = 1; // Invalid format if there's a space after '*' } + + struct tree* node = calloc(1, sizeof(struct tree)); strncpy(node->value, buffer, SIZE - 1); return node; } From f256260acafe10b79dcbfabb906c6a76db50be1c Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 10 Nov 2024 16:12:30 +0000 Subject: [PATCH 15/51] Initializacia --- cv7/program.c | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index 6261790..1394895 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -21,10 +21,6 @@ struct tree* read_tree() { } buffer[strcspn(buffer, "\n")] = 0; - if (buffer[0] == '*' && buffer[1] == ' ') { - error_flag = 1; // Invalid format if there's a space after '*' - } - struct tree* node = calloc(1, sizeof(struct tree)); strncpy(node->value, buffer, SIZE - 1); return node; @@ -35,7 +31,7 @@ struct tree* load_tree() { if (!node) { return NULL; } - if (error_flag || node->value[0] != '*') { + if (node->value[0] != '*') { node->left = load_tree(); node->right = load_tree(); } @@ -55,11 +51,8 @@ void run_tree(struct tree* tree) { printf("%s\n", tree->value); char response; - if (scanf(" %c", &response) != 1) { - printf("Koniec vstupu\n"); - return; - } else if (response != 'a' && response != 'n') { - printf("Nerozumiem\n"); + if (scanf(" %c", &response) != 1 || (response != 'a' && response != 'n')) { + error_flag = 1; return; } @@ -94,9 +87,8 @@ void count_items(struct tree* tree, int* count) { int main() { struct tree* root = load_tree(); printf("Expert z bufetu to vie.\n"); - if (!root || error_flag) { + if (!root) { printf("Chybna databaza\n"); - destroy_tree(root); return 0; } @@ -107,6 +99,10 @@ int main() { run_tree(root); + if (error_flag) { + printf("Chybna databaza\n"); + } + destroy_tree(root); return 0; } From 9f4c3725473824f2b6f9f3c97ba5fdc13df8386a Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 10 Nov 2024 16:16:10 +0000 Subject: [PATCH 16/51] Initializacia --- cv7/program.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index 1394895..0f33b8b 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -51,9 +51,11 @@ void run_tree(struct tree* tree) { printf("%s\n", tree->value); char response; - if (scanf(" %c", &response) != 1 || (response != 'a' && response != 'n')) { - error_flag = 1; + if (scanf(" %c", &response) != 1) { + printf("Chybna databaza\n"); return; + } else if (response != 'a' && response != 'n'){ + printf("Nerozumiem\n"); } if (response == 'a') { @@ -99,10 +101,6 @@ int main() { run_tree(root); - if (error_flag) { - printf("Chybna databaza\n"); - } - destroy_tree(root); return 0; } From 8509c1119ce210a87dd657a3a1d02304e8bda4d2 Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 10 Nov 2024 16:17:12 +0000 Subject: [PATCH 17/51] Initializacia --- cv7/program.c | 1 + 1 file changed, 1 insertion(+) diff --git a/cv7/program.c b/cv7/program.c index 0f33b8b..d618bfd 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -56,6 +56,7 @@ void run_tree(struct tree* tree) { return; } else if (response != 'a' && response != 'n'){ printf("Nerozumiem\n"); + return; } if (response == 'a') { From ef2e4486204f478d423e775a2cae82cf0ce020df Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 10 Nov 2024 16:20:02 +0000 Subject: [PATCH 18/51] Initializacia --- cv7/program.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cv7/program.c b/cv7/program.c index d618bfd..4521557 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -52,7 +52,7 @@ void run_tree(struct tree* tree) { char response; if (scanf(" %c", &response) != 1) { - printf("Chybna databaza\n"); + printf("Koniec vstupu\n"); return; } else if (response != 'a' && response != 'n'){ printf("Nerozumiem\n"); From 3d40f833d61ba468a2d20cf2a828dc12e2c553dc Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 10 Nov 2024 16:23:37 +0000 Subject: [PATCH 19/51] Initializacia --- cv7/program.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index 4521557..5f06f21 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -54,11 +54,16 @@ void run_tree(struct tree* tree) { if (scanf(" %c", &response) != 1) { printf("Koniec vstupu\n"); return; - } else if (response != 'a' && response != 'n'){ - printf("Nerozumiem\n"); + }else if (response != 'a' && response != 'n') { + if (feof(stdin)) { + printf("Chybna databaza\n"); + } else { + printf("Nerozumiem\n"); + } return; } + if (response == 'a') { run_tree(tree->left); } else { From c015a633492d91fea7cfe18fe322aa8c38f68637 Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 10 Nov 2024 16:31:51 +0000 Subject: [PATCH 20/51] Initializacia --- cv7/program.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cv7/program.c b/cv7/program.c index 5f06f21..9ef112d 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -55,7 +55,7 @@ void run_tree(struct tree* tree) { printf("Koniec vstupu\n"); return; }else if (response != 'a' && response != 'n') { - if (feof(stdin)) { + if (scanf(" %c", &response) != 1) { printf("Chybna databaza\n"); } else { printf("Nerozumiem\n"); From 72e5b20347189c73c68d8ed1c2c29d1274461e38 Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 12 Nov 2024 12:09:54 +0000 Subject: [PATCH 21/51] Initializacia --- cv7/program.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cv7/program.c b/cv7/program.c index 9ef112d..5f06f21 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -55,7 +55,7 @@ void run_tree(struct tree* tree) { printf("Koniec vstupu\n"); return; }else if (response != 'a' && response != 'n') { - if (scanf(" %c", &response) != 1) { + if (feof(stdin)) { printf("Chybna databaza\n"); } else { printf("Nerozumiem\n"); From 2b3c70feacf0df737d3b54ded91aa21e636af10b Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 12 Nov 2024 12:20:05 +0000 Subject: [PATCH 22/51] Initializacia --- cv7/program.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index 5f06f21..cb99ef6 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -29,15 +29,18 @@ struct tree* read_tree() { struct tree* load_tree() { struct tree* node = read_tree(); if (!node) { + error_flag = 1; return NULL; } if (node->value[0] != '*') { node->left = load_tree(); + if (error_flag) return node; node->right = load_tree(); } return node; } + void run_tree(struct tree* tree) { if (!tree) { return; @@ -55,11 +58,7 @@ void run_tree(struct tree* tree) { printf("Koniec vstupu\n"); return; }else if (response != 'a' && response != 'n') { - if (feof(stdin)) { - printf("Chybna databaza\n"); - } else { - printf("Nerozumiem\n"); - } + printf("Nerozumiem\n"); return; } @@ -95,8 +94,9 @@ 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 || error_flag) { printf("Chybna databaza\n"); + destroy_tree(root); return 0; } @@ -106,7 +106,7 @@ int main() { printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); run_tree(root); - destroy_tree(root); return 0; } + From 2da0b7a3e5cf2192573382ce44c769ba19dac965 Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 12 Nov 2024 12:23:34 +0000 Subject: [PATCH 23/51] Initializacia --- cv7/program.c | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index cb99ef6..266a371 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -1,5 +1,6 @@ #include #include +#include #include #define SIZE 100 @@ -10,8 +11,6 @@ struct tree { struct tree* right; }; -int error_flag = 0; - struct tree* read_tree() { char buffer[SIZE]; memset(buffer, 0, SIZE); @@ -20,7 +19,6 @@ struct tree* read_tree() { return NULL; } buffer[strcspn(buffer, "\n")] = 0; - struct tree* node = calloc(1, sizeof(struct tree)); strncpy(node->value, buffer, SIZE - 1); return node; @@ -29,18 +27,15 @@ struct tree* read_tree() { struct tree* load_tree() { struct tree* node = read_tree(); if (!node) { - error_flag = 1; return NULL; } if (node->value[0] != '*') { node->left = load_tree(); - if (error_flag) return node; node->right = load_tree(); } return node; } - void run_tree(struct tree* tree) { if (!tree) { return; @@ -57,12 +52,11 @@ void run_tree(struct tree* tree) { if (scanf(" %c", &response) != 1) { printf("Koniec vstupu\n"); return; - }else if (response != 'a' && response != 'n') { + } else if (response != 'a' && response != 'n') { printf("Nerozumiem\n"); return; } - if (response == 'a') { run_tree(tree->left); } else { @@ -71,18 +65,16 @@ void run_tree(struct tree* tree) { } void destroy_tree(struct tree* tree) { - if (!tree) { - return; + if (tree->left != NULL) { + destroy_tree(tree->left); + } + if (tree->right != NULL) { + destroy_tree(tree->right); } - destroy_tree(tree->left); - destroy_tree(tree->right); free(tree); } void count_items(struct tree* tree, int* count) { - if (!tree) { - return; - } if (tree->left == NULL && tree->right == NULL) { (*count)++; } else { @@ -94,9 +86,8 @@ void count_items(struct tree* tree, int* count) { int main() { struct tree* root = load_tree(); printf("Expert z bufetu to vie.\n"); - if (!root || error_flag) { + if (!root) { printf("Chybna databaza\n"); - destroy_tree(root); return 0; } @@ -106,7 +97,7 @@ int main() { printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); run_tree(root); + destroy_tree(root); return 0; } - From b701fb7fdcf5b19750ab030d473fcc394f97e154 Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 12 Nov 2024 12:47:48 +0000 Subject: [PATCH 24/51] Initializacia --- cv7/program.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cv7/program.c b/cv7/program.c index 266a371..5b330c1 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -32,10 +32,16 @@ struct tree* load_tree() { 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; From 5501d3b44c891bcbbc25e446e332cd6a570aa414 Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 12 Nov 2024 12:50:45 +0000 Subject: [PATCH 25/51] Initializacia --- cv7/program.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index 5b330c1..9b3f929 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -13,23 +13,28 @@ struct tree { struct tree* read_tree() { char buffer[SIZE]; - memset(buffer, 0, SIZE); - char* r = fgets(buffer, SIZE, stdin); - if (!r || buffer[0] == '\n') { + if (!fgets(buffer, SIZE, stdin)) { return NULL; } - buffer[strcspn(buffer, "\n")] = 0; + buffer[strcspn(buffer, "\n")] = 0; + + if (buffer[0] == '\0') { + return NULL; + } + 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(); @@ -38,10 +43,12 @@ struct tree* load_tree() { return NULL; } } + return node; } + void run_tree(struct tree* tree) { if (!tree) { return; @@ -92,7 +99,8 @@ 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; } @@ -103,7 +111,7 @@ int main() { printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); run_tree(root); - destroy_tree(root); return 0; } + From ee9e324e9ff05ffb0dce076463e2ff397377ce11 Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 12 Nov 2024 12:53:51 +0000 Subject: [PATCH 26/51] Initializacia --- cv7/program.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cv7/program.c b/cv7/program.c index 9b3f929..3c7eabe 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -42,6 +42,13 @@ struct tree* load_tree() { destroy_tree(node); return NULL; } + } else { + struct tree* extra = read_tree(); + if (extra) { + free(extra); + destroy_tree(node); + return NULL; + } } return node; @@ -49,6 +56,7 @@ struct tree* load_tree() { + void run_tree(struct tree* tree) { if (!tree) { return; @@ -115,3 +123,4 @@ int main() { return 0; } + From cf685571c59235be81d71bb69a940eda0b9bd1e5 Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 12 Nov 2024 12:56:52 +0000 Subject: [PATCH 27/51] Initializacia --- cv7/program.c | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index 3c7eabe..657f883 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -13,21 +13,24 @@ struct tree { struct tree* read_tree() { char buffer[SIZE]; - if (!fgets(buffer, SIZE, stdin)) { - return NULL; + + 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; } - buffer[strcspn(buffer, "\n")] = 0; - - if (buffer[0] == '\0') { - return NULL; - } - - struct tree* node = calloc(1, sizeof(struct tree)); - strncpy(node->value, buffer, SIZE - 1); - return node; + + return NULL; } + struct tree* load_tree() { struct tree* node = read_tree(); if (!node) { @@ -38,14 +41,7 @@ struct tree* load_tree() { node->left = load_tree(); node->right = load_tree(); - if (!node->left || !node->right) { - destroy_tree(node); - return NULL; - } - } else { - struct tree* extra = read_tree(); - if (extra) { - free(extra); + if (!node->left || !node->right) { destroy_tree(node); return NULL; } @@ -57,6 +53,7 @@ struct tree* load_tree() { + void run_tree(struct tree* tree) { if (!tree) { return; From d2efa5d900af6170a24b0589c2205dd08c04169a Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 12 Nov 2024 13:00:16 +0000 Subject: [PATCH 28/51] Initializacia --- cv7/program.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index 657f883..551776a 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -37,11 +37,18 @@ struct tree* load_tree() { return NULL; } - if (node->value[0] != '*') { + if (node->value[0] != '*') { node->left = load_tree(); node->right = load_tree(); - if (!node->left || !node->right) { + if (!node->left || !node->right) { + destroy_tree(node); + return NULL; + } + } else { + struct tree* extra = read_tree(); + if (extra) { + free(extra); destroy_tree(node); return NULL; } @@ -54,6 +61,7 @@ struct tree* load_tree() { + void run_tree(struct tree* tree) { if (!tree) { return; @@ -105,7 +113,7 @@ int main() { struct tree* root = load_tree(); printf("Expert z bufetu to vie.\n"); - if (!root) { + if (!root) { printf("Chybna databaza\n"); return 0; } @@ -121,3 +129,4 @@ int main() { } + From 724940804f4cc0fefc3eef232553b0b10d80187d Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 12 Nov 2024 13:01:20 +0000 Subject: [PATCH 29/51] Initializacia --- cv7/program.c | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index 551776a..657f883 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -37,18 +37,11 @@ struct tree* load_tree() { 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; - } - } else { - struct tree* extra = read_tree(); - if (extra) { - free(extra); + if (!node->left || !node->right) { destroy_tree(node); return NULL; } @@ -61,7 +54,6 @@ struct tree* load_tree() { - void run_tree(struct tree* tree) { if (!tree) { return; @@ -113,7 +105,7 @@ int main() { struct tree* root = load_tree(); printf("Expert z bufetu to vie.\n"); - if (!root) { + if (!root) { printf("Chybna databaza\n"); return 0; } @@ -129,4 +121,3 @@ int main() { } - From 55853490b25834957e6408113794b8250c993f89 Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 12 Nov 2024 15:48:07 +0000 Subject: [PATCH 30/51] 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; } - - From 36f9d01893e420881bab9b09385881d6b68e7aa3 Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 12 Nov 2024 15:56:01 +0000 Subject: [PATCH 31/51] Initializacia --- cv7/program.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index 266a371..de4cd79 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -29,9 +29,13 @@ struct tree* load_tree() { if (!node) { return NULL; } - if (node->value[0] != '*') { + + if (node->value[0] != '*') { node->left = load_tree(); node->right = load_tree(); + } else { + node->left = NULL; + node->right = NULL; } return node; } @@ -42,7 +46,7 @@ void run_tree(struct tree* tree) { } if (tree->value[0] == '*') { - printf("*%s\nKoniec\n", tree->value + 1); + printf("%s\nKoniec\n", tree->value + 1); return; } @@ -78,8 +82,8 @@ void count_items(struct tree* tree, int* count) { if (tree->left == NULL && tree->right == NULL) { (*count)++; } else { - count_items(tree->left, count); - count_items(tree->right, count); + if (tree->left) count_items(tree->left, count); + if (tree->right) count_items(tree->right, count); } } From 8d07713b655a67f563a4ca3435c72f1ba3f4cfd6 Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 12 Nov 2024 16:02:43 +0000 Subject: [PATCH 32/51] Initializacia --- cv7/program.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index de4cd79..4ce932f 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -1,6 +1,5 @@ #include #include -#include #include #define SIZE 100 @@ -18,7 +17,7 @@ struct tree* read_tree() { if (!r || buffer[0] == '\n') { return NULL; } - buffer[strcspn(buffer, "\n")] = 0; + buffer[strcspn(buffer, "\n")] = 0; struct tree* node = calloc(1, sizeof(struct tree)); strncpy(node->value, buffer, SIZE - 1); return node; @@ -30,15 +29,11 @@ struct tree* load_tree() { return NULL; } - if (node->value[0] != '*') { + if (node->value[0] != '*') { node->left = load_tree(); node->right = load_tree(); - } else { - node->left = NULL; - node->right = NULL; } - return node; -} + return node; void run_tree(struct tree* tree) { if (!tree) { @@ -46,11 +41,11 @@ void run_tree(struct tree* tree) { } if (tree->value[0] == '*') { - printf("%s\nKoniec\n", tree->value + 1); + printf("%s\nKoniec\n", tree->value + 1); return; } - printf("%s\n", tree->value); + printf("%s\n", tree->value); char response; if (scanf(" %c", &response) != 1) { @@ -79,7 +74,9 @@ void destroy_tree(struct tree* tree) { } void count_items(struct tree* tree, int* count) { - if (tree->left == NULL && tree->right == NULL) { + if (!tree) return; + + if (tree->value[0] == '*') { (*count)++; } else { if (tree->left) count_items(tree->left, count); @@ -90,6 +87,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) { printf("Chybna databaza\n"); return 0; From 8a8974112a0c26858d64977ece3e06f7bf76d514 Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 12 Nov 2024 16:03:36 +0000 Subject: [PATCH 33/51] Initializacia --- cv7/program.c | 1 + 1 file changed, 1 insertion(+) diff --git a/cv7/program.c b/cv7/program.c index 4ce932f..ef8b955 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -34,6 +34,7 @@ struct tree* load_tree() { node->right = load_tree(); } return node; +} void run_tree(struct tree* tree) { if (!tree) { From 5001be183c02545fe47c972325ca4773eacca607 Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 12 Nov 2024 16:05:03 +0000 Subject: [PATCH 34/51] Initializacia --- cv7/program.c | 53 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index ef8b955..53b94ec 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -1,5 +1,6 @@ #include #include +#include #include #define SIZE 100 @@ -12,41 +13,56 @@ struct tree { struct tree* read_tree() { char buffer[SIZE]; - memset(buffer, 0, SIZE); - char* r = fgets(buffer, SIZE, stdin); - if (!r || buffer[0] == '\n') { - return NULL; + + 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; } - buffer[strcspn(buffer, "\n")] = 0; - struct tree* node = calloc(1, sizeof(struct tree)); - strncpy(node->value, buffer, SIZE - 1); - return node; + + return NULL; } + + 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(); + } else { + node->left = NULL; + node->right = NULL; } - return node; + + return node; } + + + + void run_tree(struct tree* tree) { if (!tree) { return; } if (tree->value[0] == '*') { - printf("%s\nKoniec\n", tree->value + 1); + printf("*%s\nKoniec\n", tree->value + 1); return; } - printf("%s\n", tree->value); + printf("%s\n", tree->value); char response; if (scanf(" %c", &response) != 1) { @@ -75,13 +91,11 @@ void destroy_tree(struct tree* tree) { } void count_items(struct tree* tree, int* count) { - if (!tree) return; - - if (tree->value[0] == '*') { + if (tree->left == NULL && tree->right == NULL) { (*count)++; } else { - if (tree->left) count_items(tree->left, count); - if (tree->right) count_items(tree->right, count); + count_items(tree->left, count); + count_items(tree->right, count); } } @@ -89,7 +103,7 @@ int main() { struct tree* root = load_tree(); printf("Expert z bufetu to vie.\n"); - if (!root) { + if (!root) { printf("Chybna databaza\n"); return 0; } @@ -100,7 +114,8 @@ int main() { printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); run_tree(root); - destroy_tree(root); return 0; } + + From 6ec70db7995b9b897dd1fc0a3f62eb465a8de69d Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 12 Nov 2024 16:09:51 +0000 Subject: [PATCH 35/51] Initializacia --- cv7/program.c | 60 ++++++++++++++++++++++----------------------------- 1 file changed, 26 insertions(+), 34 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index 53b94ec..0c69c32 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -1,6 +1,5 @@ #include #include -#include #include #define SIZE 100 @@ -13,52 +12,41 @@ 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); + + if (!fgets(buffer, SIZE, stdin) || buffer[0] == '\n') { + return NULL; } - - return NULL; + + buffer[strcspn(buffer, "\n")] = 0; + + struct tree* node = (struct tree*)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(); - } else { - node->left = NULL; - node->right = NULL; } - + return node; } - - - - void run_tree(struct tree* tree) { if (!tree) { return; } if (tree->value[0] == '*') { - printf("*%s\nKoniec\n", tree->value + 1); + printf("%s\nKoniec\n", tree->value + 1); return; } @@ -81,16 +69,20 @@ void run_tree(struct tree* tree) { } void destroy_tree(struct tree* tree) { - if (tree->left != NULL) { - destroy_tree(tree->left); - } - if (tree->right != NULL) { - destroy_tree(tree->right); + if (!tree) { + return; } + + destroy_tree(tree->left); + destroy_tree(tree->right); free(tree); } void count_items(struct tree* tree, int* count) { + if (!tree) { + return; + } + if (tree->left == NULL && tree->right == NULL) { (*count)++; } else { @@ -101,9 +93,10 @@ 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; } @@ -114,8 +107,7 @@ int main() { printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); run_tree(root); + destroy_tree(root); return 0; } - - From 3ecf1439928c98912f7e6cb3390363d1efdd7322 Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 12 Nov 2024 16:12:28 +0000 Subject: [PATCH 36/51] Initializacia --- cv7/program.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index 0c69c32..8ebd22a 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -13,16 +13,13 @@ struct tree { struct tree* read_tree() { char buffer[SIZE]; memset(buffer, 0, SIZE); - - if (!fgets(buffer, SIZE, stdin) || buffer[0] == '\n') { + char* r = fgets(buffer, SIZE, stdin); + if (!r || buffer[0] == '\n') { return NULL; } - - buffer[strcspn(buffer, "\n")] = 0; - - struct tree* node = (struct tree*)calloc(1, sizeof(struct tree)); + buffer[strcspn(buffer, "\n")] = 0; + struct tree* node = calloc(1, sizeof(struct tree)); strncpy(node->value, buffer, SIZE - 1); - return node; } From f533cab35cb1d2f0300e6fbb3b5f4edc7185a7e7 Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 12 Nov 2024 16:13:45 +0000 Subject: [PATCH 37/51] Initializacia --- cv7/program.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cv7/program.c b/cv7/program.c index 8ebd22a..6cfe83f 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -43,7 +43,7 @@ void run_tree(struct tree* tree) { } if (tree->value[0] == '*') { - printf("%s\nKoniec\n", tree->value + 1); + printf("*%s\nKoniec\n", tree->value + 1); return; } From e14c23ba016c568b5ffd6c8d93a95d2cfcaeefec Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 12 Nov 2024 16:14:39 +0000 Subject: [PATCH 38/51] Initializacia --- cv7/program.c | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index 6cfe83f..266a371 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -1,5 +1,6 @@ #include #include +#include #include #define SIZE 100 @@ -17,7 +18,7 @@ struct tree* read_tree() { if (!r || buffer[0] == '\n') { return NULL; } - buffer[strcspn(buffer, "\n")] = 0; + buffer[strcspn(buffer, "\n")] = 0; struct tree* node = calloc(1, sizeof(struct tree)); strncpy(node->value, buffer, SIZE - 1); return node; @@ -28,12 +29,10 @@ struct tree* load_tree() { if (!node) { return NULL; } - if (node->value[0] != '*') { node->left = load_tree(); node->right = load_tree(); } - return node; } @@ -66,20 +65,16 @@ void run_tree(struct tree* tree) { } void destroy_tree(struct tree* tree) { - if (!tree) { - return; + if (tree->left != NULL) { + destroy_tree(tree->left); + } + if (tree->right != NULL) { + destroy_tree(tree->right); } - - destroy_tree(tree->left); - destroy_tree(tree->right); free(tree); } void count_items(struct tree* tree, int* count) { - if (!tree) { - return; - } - if (tree->left == NULL && tree->right == NULL) { (*count)++; } else { @@ -90,9 +85,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) { printf("Chybna databaza\n"); return 0; From 42ef7de47eaaad9f2e5d4babcdf5b08d310931dc Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 12 Nov 2024 16:20:56 +0000 Subject: [PATCH 39/51] Initializacia --- cv7/program.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/cv7/program.c b/cv7/program.c index 266a371..835db5e 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -42,7 +42,13 @@ void run_tree(struct tree* tree) { } if (tree->value[0] == '*') { - printf("*%s\nKoniec\n", tree->value + 1); + // If the node starts with *, check for children + if (tree->left || tree->right) { + printf("Expert z bufetu to vie.\nChybna databaza\n"); + } else { + // If no children, it's the end of a branch + printf("*%s\nKoniec\n", tree->value + 1); + } return; } @@ -64,6 +70,7 @@ void run_tree(struct tree* tree) { } } + void destroy_tree(struct tree* tree) { if (tree->left != NULL) { destroy_tree(tree->left); From ca43f2f98281f24748ab21ec3474b4dd783ff618 Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 12 Nov 2024 16:22:00 +0000 Subject: [PATCH 40/51] Initializacia --- cv7/program.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index 835db5e..e7ae045 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -42,13 +42,7 @@ void run_tree(struct tree* tree) { } if (tree->value[0] == '*') { - // If the node starts with *, check for children - if (tree->left || tree->right) { - printf("Expert z bufetu to vie.\nChybna databaza\n"); - } else { - // If no children, it's the end of a branch - printf("*%s\nKoniec\n", tree->value + 1); - } + printf("*%s\nKoniec\n", tree->value + 1); return; } @@ -70,7 +64,6 @@ void run_tree(struct tree* tree) { } } - void destroy_tree(struct tree* tree) { if (tree->left != NULL) { destroy_tree(tree->left); @@ -107,4 +100,4 @@ int main() { destroy_tree(root); return 0; -} +} \ No newline at end of file From 745926af828ac87df09d27ca3526af94d71b2f8f Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 12 Nov 2024 17:55:09 +0000 Subject: [PATCH 41/51] Initializacia --- cv7/program.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index e7ae045..e4fdfe0 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -1,6 +1,5 @@ #include #include -#include #include #define SIZE 100 @@ -42,6 +41,7 @@ void run_tree(struct tree* tree) { } if (tree->value[0] == '*') { + // If it's an answer printf("*%s\nKoniec\n", tree->value + 1); return; } @@ -65,21 +65,21 @@ void run_tree(struct tree* tree) { } void destroy_tree(struct tree* tree) { - if (tree->left != NULL) { + if (tree) { destroy_tree(tree->left); - } - if (tree->right != NULL) { destroy_tree(tree->right); + free(tree); } - free(tree); } void count_items(struct tree* tree, int* count) { - if (tree->left == NULL && tree->right == NULL) { - (*count)++; - } else { - count_items(tree->left, count); - count_items(tree->right, count); + if (tree) { + if (tree->left == NULL && tree->right == NULL) { + (*count)++; + } else { + count_items(tree->left, count); + count_items(tree->right, count); + } } } From dd1da65481730d3581db4d047804fd85c98bf327 Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 12 Nov 2024 17:57:20 +0000 Subject: [PATCH 42/51] Initializacia --- cv7/program.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index e4fdfe0..61cc1d4 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -10,31 +10,32 @@ struct tree { struct tree* right; }; +// Function to read a single node from input struct tree* read_tree() { char buffer[SIZE]; - memset(buffer, 0, SIZE); - char* r = fgets(buffer, SIZE, stdin); - if (!r || buffer[0] == '\n') { + if (fgets(buffer, SIZE, stdin) == NULL || buffer[0] == '\n') { return NULL; } - buffer[strcspn(buffer, "\n")] = 0; + buffer[strcspn(buffer, "\n")] = 0; // Remove newline character 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] != '*') { + if (node->value[0] != '*') { // Not an answer, so load children 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; @@ -47,6 +48,7 @@ void run_tree(struct tree* tree) { } printf("%s\n", tree->value); + printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); char response; if (scanf(" %c", &response) != 1) { @@ -64,6 +66,7 @@ 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); @@ -72,10 +75,11 @@ 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)++; + (*count)++; // It's a leaf node (an answer) } else { count_items(tree->left, count); count_items(tree->right, count); @@ -84,8 +88,9 @@ void count_items(struct tree* tree, int* count) { } int main() { - struct tree* root = load_tree(); printf("Expert z bufetu to vie.\n"); + + struct tree* root = load_tree(); if (!root) { printf("Chybna databaza\n"); return 0; @@ -94,10 +99,8 @@ int main() { int count = 0; count_items(root, &count); printf("Pozna %d druhov ovocia a zeleniny.\n", count); - printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); run_tree(root); - destroy_tree(root); return 0; } \ No newline at end of file From 302f4d4987f9b773256d20d62fc9da002e7b4a13 Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 12 Nov 2024 17:59:15 +0000 Subject: [PATCH 43/51] Initializacia --- cv7/program.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cv7/program.c b/cv7/program.c index 61cc1d4..c056c10 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -14,7 +14,7 @@ struct tree { struct tree* read_tree() { char buffer[SIZE]; if (fgets(buffer, SIZE, stdin) == NULL || buffer[0] == '\n') { - return NULL; + return NULL; // Return NULL for empty input } buffer[strcspn(buffer, "\n")] = 0; // Remove newline character struct tree* node = calloc(1, sizeof(struct tree)); From b7e46be38d161ae37609c6fbf4b65d6e138182d4 Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 12 Nov 2024 18:05:10 +0000 Subject: [PATCH 44/51] Initializacia --- cv7/program.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index c056c10..e14a3aa 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -51,12 +51,14 @@ void run_tree(struct tree* tree) { printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); char response; - if (scanf(" %c", &response) != 1) { - printf("Koniec vstupu\n"); - return; - } else if (response != 'a' && response != 'n') { - printf("Nerozumiem\n"); - return; + 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 } if (response == 'a') { From 27c63a26ef1ae312e08e23648d0221fd25cfe01f Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 12 Nov 2024 18:29:32 +0000 Subject: [PATCH 45/51] 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); From fe7326837f23b9bc040cd24ced1a3fe64aab58e7 Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 12 Nov 2024 19:10:28 +0000 Subject: [PATCH 46/51] Initializacia --- cv7/program.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index b8e0cfe..63ac077 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -38,12 +38,13 @@ void run_tree(struct tree* tree) { return; } + printf("%s\n", tree->value); + if (tree->value[0] == '*') { - printf("*%s\nKoniec\n", tree->value + 1); + printf("Koniec\n"); return; } - printf("%s\n", tree->value); printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); char response; From 0e5b2261b024d3ce15372aea900891a82b813444 Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 12 Nov 2024 19:12:14 +0000 Subject: [PATCH 47/51] Initializacia --- cv7/program.c | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index 63ac077..e4fdfe0 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -12,7 +12,9 @@ struct tree { struct tree* read_tree() { char buffer[SIZE]; - if (fgets(buffer, SIZE, stdin) == NULL || buffer[0] == '\n') { + memset(buffer, 0, SIZE); + char* r = fgets(buffer, SIZE, stdin); + if (!r || buffer[0] == '\n') { return NULL; } buffer[strcspn(buffer, "\n")] = 0; @@ -38,22 +40,21 @@ void run_tree(struct tree* tree) { return; } - printf("%s\n", tree->value); - if (tree->value[0] == '*') { - printf("Koniec\n"); + // If it's an answer + printf("*%s\nKoniec\n", tree->value + 1); return; } - printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); + printf("%s\n", tree->value); char response; - while (1) { - if (scanf(" %c", &response) != 1 || (response != 'a' && response != 'n')) { - printf("Nerozumiem\n"); - continue; - } - break; + if (scanf(" %c", &response) != 1) { + printf("Koniec vstupu\n"); + return; + } else if (response != 'a' && response != 'n') { + printf("Nerozumiem\n"); + return; } if (response == 'a') { @@ -83,9 +84,8 @@ void count_items(struct tree* tree, int* count) { } int main() { - printf("Expert z bufetu to vie.\n"); - struct tree* root = load_tree(); + printf("Expert z bufetu to vie.\n"); if (!root) { printf("Chybna databaza\n"); return 0; @@ -94,8 +94,10 @@ int main() { int count = 0; count_items(root, &count); printf("Pozna %d druhov ovocia a zeleniny.\n", count); + printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); run_tree(root); + destroy_tree(root); return 0; } \ No newline at end of file From e548847f2e57f9dbb7d94949003d72b91b182bc8 Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 12 Nov 2024 19:15:40 +0000 Subject: [PATCH 48/51] Initializacia --- cv7/program.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index e4fdfe0..0d71563 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -31,6 +31,11 @@ struct tree* load_tree() { if (node->value[0] != '*') { node->left = load_tree(); node->right = load_tree(); + } else if (node->left != NULL || node->right != NULL) { + printf("Expert z bufetu to vie.\n"); + printf("Chybna databaza\n"); + free(node); + return NULL; } return node; } @@ -41,7 +46,6 @@ void run_tree(struct tree* tree) { } if (tree->value[0] == '*') { - // If it's an answer printf("*%s\nKoniec\n", tree->value + 1); return; } @@ -85,12 +89,12 @@ void count_items(struct tree* tree, int* count) { int main() { struct tree* root = load_tree(); - printf("Expert z bufetu to vie.\n"); if (!root) { - printf("Chybna databaza\n"); return 0; } + printf("Expert z bufetu to vie.\n"); + int count = 0; count_items(root, &count); printf("Pozna %d druhov ovocia a zeleniny.\n", count); From 192ea219b45f42ab69f4697066b21ce1814f667a Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 12 Nov 2024 19:19:17 +0000 Subject: [PATCH 49/51] Initializacia --- cv7/program.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/cv7/program.c b/cv7/program.c index 0d71563..7d216e0 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -33,7 +33,6 @@ struct tree* load_tree() { node->right = load_tree(); } else if (node->left != NULL || node->right != NULL) { printf("Expert z bufetu to vie.\n"); - printf("Chybna databaza\n"); free(node); return NULL; } @@ -89,12 +88,22 @@ void count_items(struct tree* tree, int* count) { int main() { struct tree* root = load_tree(); + if (!root) { + printf("Chybna databaza\n"); return 0; } printf("Expert z bufetu to vie.\n"); + // Check if the input is empty after loading the tree + char buffer[SIZE]; + if (fgets(buffer, SIZE, stdin) == NULL || buffer[0] == '\n') { + printf("Chybna databaza\n"); + destroy_tree(root); + return 0; + } + int count = 0; count_items(root, &count); printf("Pozna %d druhov ovocia a zeleniny.\n", count); From 3f0bc73577be262bb65297141b152420bda0c819 Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 12 Nov 2024 19:22:34 +0000 Subject: [PATCH 50/51] Initializacia --- cv7/program.c | 8 -------- 1 file changed, 8 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index 7d216e0..44d7cff 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -96,14 +96,6 @@ int main() { printf("Expert z bufetu to vie.\n"); - // Check if the input is empty after loading the tree - char buffer[SIZE]; - if (fgets(buffer, SIZE, stdin) == NULL || buffer[0] == '\n') { - printf("Chybna databaza\n"); - destroy_tree(root); - return 0; - } - int count = 0; count_items(root, &count); printf("Pozna %d druhov ovocia a zeleniny.\n", count); From 68b117c30456e5df4378269fa30c9d9940758610 Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 12 Nov 2024 19:24:07 +0000 Subject: [PATCH 51/51] Initializacia --- cv7/program.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cv7/program.c b/cv7/program.c index 44d7cff..4ba61f1 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -89,12 +89,13 @@ void count_items(struct tree* tree, int* count) { int main() { struct tree* root = load_tree(); + printf("Expert z bufetu to vie.\n"); + if (!root) { printf("Chybna databaza\n"); return 0; } - printf("Expert z bufetu to vie.\n"); int count = 0; count_items(root, &count);