2024-11-11 13:45:35 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
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;
|
2024-11-13 21:17:06 +00:00
|
|
|
if (root->yes == NULL && root->no == NULL) return 1;
|
2024-11-11 13:45:35 +00:00
|
|
|
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)) {
|
|
|
|
line[strcspn(line, "\n")] = 0;
|
2024-11-13 21:17:06 +00:00
|
|
|
if (strlen(line) == 0) break;
|
2024-11-11 13:45:35 +00:00
|
|
|
|
2024-11-13 21:17:06 +00:00
|
|
|
if (line[0] == '*') {
|
2024-11-11 13:45:35 +00:00
|
|
|
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;
|
|
|
|
}
|
2024-11-13 21:17:06 +00:00
|
|
|
} else {
|
2024-11-11 13:45:35 +00:00
|
|
|
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) {
|
2024-11-13 21:22:03 +00:00
|
|
|
printf("* %s\n", node->content);
|
2024-11-11 13:45:35 +00:00
|
|
|
printf("Koniec\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-11-13 21:17:06 +00:00
|
|
|
printf("%s\n", node->content);
|
2024-11-11 13:51:51 +00:00
|
|
|
|
2024-11-11 13:45:35 +00:00
|
|
|
char response;
|
|
|
|
while (1) {
|
|
|
|
response = getchar();
|
2024-11-13 21:19:19 +00:00
|
|
|
getchar(); // Capture newline
|
2024-11-13 21:17:06 +00:00
|
|
|
if (response == 'a' && node->yes) {
|
2024-11-11 13:45:35 +00:00
|
|
|
ask_question(node->yes);
|
|
|
|
break;
|
2024-11-13 21:17:06 +00:00
|
|
|
} else if (response == 'n' && node->no) {
|
2024-11-11 13:45:35 +00:00
|
|
|
ask_question(node->no);
|
|
|
|
break;
|
|
|
|
} else {
|
2024-11-13 21:22:03 +00:00
|
|
|
printf("Nerozumiem\n");
|
2024-11-11 13:45:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
printf("Expert z bufetu to vie.\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);
|
2024-11-13 21:19:19 +00:00
|
|
|
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
2024-11-11 13:45:35 +00:00
|
|
|
|
2024-11-13 21:17:06 +00:00
|
|
|
if (root) {
|
|
|
|
ask_question(root);
|
|
|
|
}
|
|
|
|
|
2024-11-11 13:45:35 +00:00
|
|
|
free_tree(root);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|