From 0cc2266abf44138cf6237aa749bc131a240fd252 Mon Sep 17 00:00:00 2001 From: Anton Dolozin Date: Sun, 9 Nov 2025 16:46:59 +0100 Subject: [PATCH] Trying --- du6/program.c | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 du6/program.c diff --git a/du6/program.c b/du6/program.c new file mode 100644 index 0000000..60c0f54 --- /dev/null +++ b/du6/program.c @@ -0,0 +1,132 @@ +#include +#include +#include +#include +#include +#define SIZE 100 + + +struct tree{ + char value[SIZE]; + struct tree* left; + struct tree* right; +}; + + +void destroy_tree(struct tree* tree){ + if(tree->left){ + destroy_tree(tree->left); + } + if(tree->right){ + destroy_tree(tree->right); + } + free(tree); +} + +struct tree* read_tree(){ + char buffer[SIZE]; + memset(buffer,0,SIZE); + char* r = fgets(buffer,SIZE,stdin); + r[strcspn(r, "\n")] = '\0'; + assert(r); + struct tree* node = calloc(1,sizeof(struct tree)); + memcpy(node->value,buffer,SIZE); + if(buffer[0] == '*'){ + node->left = NULL; + node->right = NULL; + return node; + + } + node->left = read_tree(); + node->right = read_tree(); + // Ak je nacitany riadok listovy uzol ,tak netreba robit nic + // inak rekurzivne nacitanie laveho syna + // a rekurzivne nacitanie praveho syna + return node; +} +void print_tree(struct tree* tree,int offset){ + for (int i = 0; i < offset; i++){ + printf(" "); + } + printf("%s",tree->value); + if (tree->left){ + print_tree(tree->left,offset +3); + print_tree(tree->right,offset +3); + } +} +int count_leaves(struct tree* tree){ + int count = 0; + if(tree == NULL){ + return count; + } + if(tree->left == NULL && tree->right == NULL && tree){ + return count +1; + } + else{ + return count + count_leaves(tree->left) + count_leaves(tree->right); + + } +} + +int main(void){ + bool whitespace = false; + + struct tree* tree = read_tree(); + char line[SIZE]; + + if(!tree){ + printf("Bazu sa nedalo nacitat'.\n"); + return 1; + } + while( fgets(line, sizeof(line), stdin)){ + line[strcspn(line, "\n")] = '\0'; + if(line[0] == '\0'){ + whitespace = true; + break; + } + + } + if(!whitespace){ + printf("No whitespace after database\n"); + return 1; + } + int numOfLeaves = count_leaves(tree); + char answer[SIZE]; + printf("Expert z bufetu to vie\n"); + printf("Pozna %d druhov ovocia a zeleniny.\n", numOfLeaves); + printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost."); + while(tree){ + printf("\n%s", tree->value); + fgets(answer, sizeof(answer), stdin); + answer[strcspn(answer, "\n")] = '\0'; + + if(strcmp(answer, "n") == 0){ + tree = tree->left; + if(tree->value[0] == '*'){ + printf("\n%s\n", tree->value); + printf("Koniec\n"); + return 0; + } + printf("%s\n", tree->value); + fgets(answer, sizeof(answer), stdin); + + } + else if(strcmp(answer, "a") == 0){ + tree = tree->right; + if(tree->value[0] == '*'){ + printf("\n%s\n", tree->value); + printf("Koniec\n"); + + return 0; + } + fgets(answer, sizeof(answer), stdin); + + } + else{ + printf("%s\n", answer); + return 1; + } + } + destroy_tree(tree); +return 0; +} \ No newline at end of file