From 48b502fcf446d566b7f6677f2567fcf4c4e84595 Mon Sep 17 00:00:00 2001 From: vafan Date: Thu, 16 Apr 2026 20:51:45 +0000 Subject: [PATCH] ls --- du5/README.md | 0 du5/program.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 du5/README.md create mode 100644 du5/program.c diff --git a/du5/README.md b/du5/README.md new file mode 100644 index 0000000..e69de29 diff --git a/du5/program.c b/du5/program.c new file mode 100644 index 0000000..2b3def1 --- /dev/null +++ b/du5/program.c @@ -0,0 +1,123 @@ +#include +#include +#include +#include + +#define LINE_SIZE 256 + +typedef struct Node { + char *text; + int is_answer; + struct Node *yes; + struct Node *no; +} Node; + +Node* make_node(const char *text, int is_answer) { + Node *node = (Node*)malloc(sizeof(Node)); + node->text = (char*)malloc(strlen(text) + 1); + strcpy(node->text, text); + node->is_answer = is_answer; + node->yes = NULL; + node->no = NULL; + return node; +} + +int how_many_answers(Node *node) { + if (node == NULL) return 0; + int count = 0; + if (node->is_answer) count = 1; + count += how_many_answers(node->yes); + count += how_many_answers(node->no); + return count; +} + +Node* build_tree() { + char line[LINE_SIZE]; + + if (fgets(line, sizeof(line), stdin) == NULL) { + return NULL; + } + + line[strcspn(line, "\n\r")] = '\0'; + + if (strlen(line) == 0) { + return NULL; + } + + int is_answer = 0; + char *text = line; + + if (line[0] == '*') { + is_answer = 1; + text = line + 1; + } + + Node *node = make_node(text, is_answer); + + if (!is_answer) { + node->yes = build_tree(); + node->no = build_tree(); + } + + return node; +} + +void chat_with_user(Node *root, int answer_count) { + printf("Expert z bufetu to vie.\n"); + printf("Pozna %d druhov ovocia a zeleniny.\n", answer_count); + printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); + + Node *current = root; + char input[LINE_SIZE]; + + while (current != NULL && !current->is_answer) { + printf("%s\n", current->text); + + if (fgets(input, sizeof(input), stdin) == NULL) { + printf("Koniec\n"); + return; + } + + input[strcspn(input, "\n\r")] = '\0'; + + if (strcmp(input, "a") == 0) { + current = current->yes; + } else if (strcmp(input, "n") == 0) { + current = current->no; + } else { + printf("Koniec\n"); + return; + } + } + + if (current != NULL && current->is_answer) { + printf("* %s\n", current->text); + } + + printf("Koniec\n"); +} + +void cleanup_tree(Node *node) { + if (node == NULL) return; + cleanup_tree(node->yes); + cleanup_tree(node->no); + free(node->text); + free(node); +} + +int main() { + Node *root = build_tree(); + + if (root == NULL) { + printf("Nepodarilo sa nacitat bazu pravidiel\n"); + return 1; + } + + int answer_count = how_many_answers(root); + + chat_with_user(root, answer_count); + + cleanup_tree(root); + + return 0; +}