2024-11-14 16:46:13 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2024-11-14 17:25:22 +00:00
|
|
|
typedef struct Node {
|
2024-11-14 16:46:13 +00:00
|
|
|
char *text;
|
2024-11-14 17:25:22 +00:00
|
|
|
int is_answer;
|
|
|
|
struct Node *yes;
|
|
|
|
struct Node *no;
|
|
|
|
} Node;
|
|
|
|
|
|
|
|
char *read_line(void) {
|
|
|
|
size_t size = 100;
|
|
|
|
char *buffer = malloc(size);
|
|
|
|
if (!buffer) return NULL;
|
2024-11-14 16:46:13 +00:00
|
|
|
|
2024-11-14 17:25:22 +00:00
|
|
|
int c;
|
|
|
|
size_t len = 0;
|
|
|
|
while ((c = getchar()) != EOF && c != '\n') {
|
|
|
|
if (len == size - 1) {
|
|
|
|
size *= 2;
|
|
|
|
char *new_buffer = realloc(buffer, size);
|
|
|
|
if (!new_buffer) {
|
|
|
|
free(buffer);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
buffer = new_buffer;
|
|
|
|
}
|
|
|
|
buffer[len++] = c;
|
|
|
|
}
|
|
|
|
if (len == 0 && c == EOF) {
|
|
|
|
free(buffer);
|
2024-11-14 16:46:13 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2024-11-14 17:25:22 +00:00
|
|
|
buffer[len] = '\0';
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
int read_knowledge_base(char ***lines_ptr, int *num_lines) {
|
|
|
|
int capacity = 100;
|
|
|
|
char **lines = malloc(capacity * sizeof(char *));
|
|
|
|
if (!lines) return -1;
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
while (1) {
|
|
|
|
char *line = read_line();
|
2024-11-14 17:30:30 +00:00
|
|
|
if (!line || line[0] == '\0') break;
|
2024-11-14 17:25:22 +00:00
|
|
|
|
|
|
|
if (count == capacity) {
|
|
|
|
capacity *= 2;
|
|
|
|
char **new_lines = realloc(lines, capacity * sizeof(char *));
|
|
|
|
if (!new_lines) {
|
|
|
|
for (int i = 0; i < count; i++) free(lines[i]);
|
|
|
|
free(lines);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
lines = new_lines;
|
|
|
|
}
|
|
|
|
lines[count++] = line;
|
2024-11-14 16:46:13 +00:00
|
|
|
}
|
2024-11-14 17:25:22 +00:00
|
|
|
*lines_ptr = lines;
|
|
|
|
*num_lines = count;
|
|
|
|
return 0;
|
2024-11-14 16:46:13 +00:00
|
|
|
}
|
|
|
|
|
2024-11-14 17:25:22 +00:00
|
|
|
Node *build_tree(char **lines, int *index, int num_lines, int *count, int *error) {
|
|
|
|
if (*index >= num_lines) {
|
|
|
|
*error = 1;
|
|
|
|
return NULL;
|
2024-11-14 17:15:33 +00:00
|
|
|
}
|
2024-11-14 17:01:44 +00:00
|
|
|
|
2024-11-14 17:25:22 +00:00
|
|
|
Node *node = malloc(sizeof(Node));
|
|
|
|
if (!node) {
|
|
|
|
*error = 1;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->text = lines[(*index)++];
|
|
|
|
node->yes = node->no = NULL;
|
|
|
|
node->is_answer = (node->text[0] == '*');
|
|
|
|
|
|
|
|
if (node->is_answer) {
|
|
|
|
(*count)++;
|
|
|
|
} else {
|
|
|
|
node->yes = build_tree(lines, index, num_lines, count, error);
|
2024-11-14 17:30:30 +00:00
|
|
|
if (*error) return NULL;
|
|
|
|
|
2024-11-14 17:25:22 +00:00
|
|
|
node->no = build_tree(lines, index, num_lines, count, error);
|
2024-11-14 17:30:30 +00:00
|
|
|
if (*error) return NULL;
|
2024-11-14 17:25:22 +00:00
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
void run_knowledge_system(Node *root, int count) {
|
|
|
|
printf("Expert z bufetu to vie.\n");
|
|
|
|
if (!root) {
|
|
|
|
printf("Chybna databaza\n");
|
2024-11-14 17:15:33 +00:00
|
|
|
return;
|
|
|
|
}
|
2024-11-14 17:07:03 +00:00
|
|
|
|
2024-11-14 17:25:22 +00:00
|
|
|
printf("Pozna %d druhov ovocia a zeleniny.\n", count);
|
2024-11-14 17:15:33 +00:00
|
|
|
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
2024-11-14 17:11:48 +00:00
|
|
|
|
2024-11-14 17:25:22 +00:00
|
|
|
Node *node = root;
|
|
|
|
while (node) {
|
|
|
|
printf("%s\n", node->text);
|
|
|
|
if (node->is_answer) {
|
|
|
|
printf("Koniec\n");
|
|
|
|
return;
|
2024-11-14 17:22:44 +00:00
|
|
|
}
|
|
|
|
|
2024-11-14 17:25:22 +00:00
|
|
|
char input[10];
|
|
|
|
if (!fgets(input, sizeof(input), stdin)) {
|
|
|
|
printf("Koniec vstupu\n");
|
|
|
|
return;
|
2024-11-14 17:22:44 +00:00
|
|
|
}
|
2024-11-14 17:25:22 +00:00
|
|
|
|
|
|
|
input[strcspn(input, "\n")] = 0;
|
|
|
|
if (strcmp(input, "a") == 0) {
|
|
|
|
node = node->yes;
|
|
|
|
} else if (strcmp(input, "n") == 0) {
|
|
|
|
node = node->no;
|
|
|
|
} else {
|
|
|
|
printf("Nerozumiem\n");
|
|
|
|
return;
|
2024-11-14 17:11:48 +00:00
|
|
|
}
|
2024-11-14 16:46:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-14 17:25:22 +00:00
|
|
|
void free_tree(Node *node) {
|
|
|
|
if (!node) return;
|
|
|
|
if (!node->is_answer) {
|
|
|
|
free_tree(node->yes);
|
|
|
|
free_tree(node->no);
|
|
|
|
}
|
|
|
|
free(node);
|
2024-11-14 16:46:13 +00:00
|
|
|
}
|
|
|
|
|
2024-11-14 17:25:22 +00:00
|
|
|
int main(void) {
|
|
|
|
char **lines;
|
|
|
|
int num_lines;
|
|
|
|
|
|
|
|
if (read_knowledge_base(&lines, &num_lines) != 0 || num_lines == 0) {
|
|
|
|
printf("Expert z bufetu to vie.\n");
|
|
|
|
printf("Chybna databaza\n");
|
|
|
|
return 0;
|
2024-11-14 16:46:13 +00:00
|
|
|
}
|
2024-11-14 17:25:22 +00:00
|
|
|
|
|
|
|
int index = 0, count = 0, error = 0;
|
|
|
|
Node *root = build_tree(lines, &index, num_lines, &count, &error);
|
|
|
|
|
|
|
|
if (error || index != num_lines || count == 0) {
|
|
|
|
printf("Expert z bufetu to vie.\n");
|
|
|
|
printf("Chybna databaza\n");
|
2024-11-14 17:28:17 +00:00
|
|
|
for (int i = 0; i < num_lines; i++) {
|
|
|
|
free(lines[i]);
|
|
|
|
}
|
|
|
|
free(lines);
|
|
|
|
free_tree(root);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
run_knowledge_system(root, count);
|
|
|
|
free_tree(root);
|
|
|
|
for (int i = 0; i < num_lines; i++) {
|
|
|
|
free(lines[i]);
|
|
|
|
}
|
|
|
|
free(lines);
|
|
|
|
return 0;
|
2024-11-14 17:30:30 +00:00
|
|
|
}
|