2024-11-15 00:42:02 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2024-11-15 00:40:40 +00:00
|
|
|
|
2024-11-15 00:42:02 +00:00
|
|
|
// Štruktúra pre uzol v binárnom strome
|
|
|
|
typedef struct Node {
|
|
|
|
char text[100];
|
|
|
|
int is_answer; // 1 pre odpoveď, 0 pre otázku
|
|
|
|
struct Node *yes_branch;
|
|
|
|
struct Node *no_branch;
|
|
|
|
} Node;
|
2024-11-15 00:40:40 +00:00
|
|
|
|
2024-11-15 00:42:02 +00:00
|
|
|
// Vytvára nový uzol s textom a typom (odpoveď alebo otázka)
|
|
|
|
Node *create_node(const char *text, int is_answer) {
|
|
|
|
Node *node = malloc(sizeof(Node));
|
|
|
|
if (!node) {
|
|
|
|
printf("Chyba: Nepodarilo sa alokovať pamäť.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
strcpy(node->text, text);
|
|
|
|
node->is_answer = is_answer;
|
|
|
|
node->yes_branch = NULL;
|
|
|
|
node->no_branch = NULL;
|
|
|
|
return node;
|
|
|
|
}
|
2024-11-15 00:40:40 +00:00
|
|
|
|
2024-11-15 00:42:02 +00:00
|
|
|
// Funkcia na načítanie bázy pravidiel do binárneho stromu
|
|
|
|
Node *load_rules(FILE *file, int *num_answers) {
|
|
|
|
char line[100];
|
|
|
|
if (!fgets(line, sizeof(line), file) || line[0] == '\n') {
|
|
|
|
return NULL; // Prázdny riadok, koniec pravidiel
|
|
|
|
}
|
2024-11-15 00:40:40 +00:00
|
|
|
|
2024-11-15 00:42:02 +00:00
|
|
|
int is_answer = line[0] == '*';
|
|
|
|
char *text = is_answer ? line + 1 : line;
|
|
|
|
text[strcspn(text, "\n")] = 0; // Odstráni znak nového riadka
|
2024-11-15 00:40:40 +00:00
|
|
|
|
2024-11-15 00:42:02 +00:00
|
|
|
Node *node = create_node(text, is_answer);
|
|
|
|
if (is_answer) {
|
|
|
|
(*num_answers)++;
|
|
|
|
return node;
|
|
|
|
}
|
2024-11-15 00:40:40 +00:00
|
|
|
|
2024-11-15 00:42:02 +00:00
|
|
|
node->yes_branch = load_rules(file, num_answers);
|
|
|
|
node->no_branch = load_rules(file, num_answers);
|
|
|
|
return node;
|
|
|
|
}
|
2024-11-15 00:40:40 +00:00
|
|
|
|
2024-11-15 00:42:02 +00:00
|
|
|
// Funkcia na kladenie otázok používateľovi
|
|
|
|
void ask_questions(Node *node) {
|
|
|
|
while (node) {
|
|
|
|
printf("%s\n", node->text);
|
|
|
|
if (node->is_answer) {
|
|
|
|
printf("Koniec.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char answer;
|
|
|
|
printf("Odpovedajte 'a' pre prvú možnosť alebo 'n' pre druhú možnosť: ");
|
|
|
|
scanf(" %c", &answer);
|
2024-11-15 00:40:40 +00:00
|
|
|
|
2024-11-15 00:42:02 +00:00
|
|
|
if (answer == 'a') {
|
|
|
|
node = node->yes_branch;
|
|
|
|
} else if (answer == 'n') {
|
|
|
|
node = node->no_branch;
|
|
|
|
} else {
|
|
|
|
printf("Neplatná odpoveď, koniec.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-15 00:40:40 +00:00
|
|
|
|
2024-11-15 00:42:02 +00:00
|
|
|
// Uvoľní pamäť pre všetky uzly v binárnom strome
|
|
|
|
void free_tree(Node *node) {
|
|
|
|
if (node) {
|
|
|
|
free_tree(node->yes_branch);
|
|
|
|
free_tree(node->no_branch);
|
|
|
|
free(node);
|
|
|
|
}
|
|
|
|
}
|
2024-11-15 00:40:40 +00:00
|
|
|
|
2024-11-15 00:42:02 +00:00
|
|
|
int main() {
|
|
|
|
FILE *file = fopen("rules.txt", "r");
|
|
|
|
if (!file) {
|
|
|
|
printf("Chyba: Nepodarilo sa otvoriť súbor s pravidlami.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2024-11-15 00:40:40 +00:00
|
|
|
|
2024-11-15 00:42:02 +00:00
|
|
|
int num_answers = 0;
|
|
|
|
Node *root = load_rules(file, &num_answers);
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
if (!root) {
|
|
|
|
printf("Chyba: Nepodarilo sa načítať bázu pravidiel.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Pozná %d druhov ovocia a zeleniny.\n", num_answers);
|
|
|
|
ask_questions(root);
|
|
|
|
free_tree(root);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|