From 4d80f775bb7c50e696c33d820adcc3d4734d6e01 Mon Sep 17 00:00:00 2001 From: Marat Izmailov Date: Mon, 11 Nov 2024 13:45:35 +0000 Subject: [PATCH] Add cv7/program.c --- cv7/program.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 cv7/program.c diff --git a/cv7/program.c b/cv7/program.c new file mode 100644 index 0000000..6316aec --- /dev/null +++ b/cv7/program.c @@ -0,0 +1,119 @@ +#include +#include +#include + +typedef struct Node { + char *content; + struct Node *yes; + struct Node *no; +} Node; + +Node* create_node(const char *content) { + Node *new_node = (Node*)malloc(sizeof(Node)); + new_node->content = strdup(content); + new_node->yes = NULL; + new_node->no = NULL; + return new_node; +} + +void free_tree(Node *root) { + if (root) { + free(root->content); + free_tree(root->yes); + free_tree(root->no); + free(root); + } +} + +int count_answers(Node *root) { + if (!root) return 0; + if (root->yes == NULL && root->no == NULL) return 1; // It's an answer + return count_answers(root->yes) + count_answers(root->no); +} + +Node* parse_rules() { + char line[256]; + Node *root = NULL, *current = NULL; + Node *stack[100]; + int stack_top = -1; + + while (fgets(line, sizeof(line), stdin)) { + // Trim newline and spaces + line[strcspn(line, "\n")] = 0; + if (strlen(line) == 0) break; // End of rules + + if (line[0] == '*') { // This is an answer + Node *answer = create_node(line + 1); + if (stack_top >= 0) { + if (stack[stack_top]->yes == NULL) + stack[stack_top]->yes = answer; + else + stack[stack_top]->no = answer; + } + } else { // This is a question + Node *question = create_node(line); + if (stack_top >= 0) { + if (stack[stack_top]->yes == NULL) + stack[stack_top]->yes = question; + else + stack[stack_top]->no = question; + } + stack[++stack_top] = question; + } + } + + if (stack_top >= 0) { + root = stack[0]; + } + + return root; +} + +void ask_question(Node *node) { + if (!node) return; + if (node->yes == NULL && node->no == NULL) { + printf("Expert z bufetu to vie.\n"); + printf("Pozna 2 druhov ovocia a zeleniny.\n"); + printf("Koniec\n"); + return; + } + + printf("%s\n", node->content); + printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); + + char response; + while (1) { + response = getchar(); + getchar(); // To capture newline + if (response == 'a') { + ask_question(node->yes); + break; + } else if (response == 'n') { + ask_question(node->no); + break; + } else { + printf("Neplatna odpoved, koniec.\n"); + break; + } + } +} + +int main() { + printf("Expert z bufetu to vie.\n"); + printf("Pozna 2 druhov ovocia a zeleniny.\n"); + printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); + + Node *root = parse_rules(); + if (!root) { + printf("Chybne pravidla.\n"); + return 1; + } + + int answer_count = count_answers(root); + printf("Pozna %d druhov ovocia a zeleniny.\n", answer_count); + + ask_question(root); + free_tree(root); + + return 0; +}