2024-11-06 08:44:29 +00:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#define MAX_LINE_LENGTH 100
|
|
|
|
|
|
|
|
|
|
typedef struct Node {
|
|
|
|
|
char text[MAX_LINE_LENGTH];
|
|
|
|
|
struct Node *yes;
|
|
|
|
|
struct Node *no;
|
|
|
|
|
} Node;
|
|
|
|
|
|
|
|
|
|
Node* create_node(const char* text) {
|
|
|
|
|
Node *node = (Node*)malloc(sizeof(Node));
|
|
|
|
|
if (!node) {
|
2024-11-08 12:41:41 +00:00
|
|
|
|
fprintf(stderr, "Ошибка выделения памяти.\n");
|
2024-11-06 08:44:29 +00:00
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
strncpy(node->text, text, MAX_LINE_LENGTH);
|
|
|
|
|
node->yes = NULL;
|
|
|
|
|
node->no = NULL;
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-08 12:41:41 +00:00
|
|
|
|
Node* parse_knowledge_base(char lines[][MAX_LINE_LENGTH], int *index, int line_count) {
|
|
|
|
|
if (*index >= line_count || lines[*index][0] == '\0') {
|
2024-11-08 10:09:32 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-08 12:41:41 +00:00
|
|
|
|
char *line = lines[*index];
|
2024-11-08 12:59:10 +00:00
|
|
|
|
(*index)++;
|
2024-11-08 10:09:32 +00:00
|
|
|
|
|
2024-11-08 10:12:56 +00:00
|
|
|
|
Node *node = NULL;
|
2024-11-08 10:09:32 +00:00
|
|
|
|
if (line[0] == '*') {
|
2024-11-08 12:59:10 +00:00
|
|
|
|
node = create_node(line + 1);
|
2024-11-08 10:09:32 +00:00
|
|
|
|
} else {
|
2024-11-08 12:59:10 +00:00
|
|
|
|
node = create_node(line);
|
|
|
|
|
node->yes = parse_knowledge_base(lines, index, line_count);
|
|
|
|
|
node->no = parse_knowledge_base(lines, index, line_count);
|
2024-11-08 10:09:32 +00:00
|
|
|
|
}
|
|
|
|
|
return node;
|
2024-11-06 08:44:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-08 13:33:17 +00:00
|
|
|
|
int validate_tree_structure(char lines[][MAX_LINE_LENGTH], int line_count) {
|
|
|
|
|
int leaf_count = 0;
|
|
|
|
|
int question_count = 0;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < line_count; i++) {
|
|
|
|
|
if (lines[i][0] == '*') {
|
|
|
|
|
leaf_count++;
|
|
|
|
|
} else {
|
|
|
|
|
question_count++;
|
|
|
|
|
if (i + 2 < line_count && lines[i + 1][0] == '*' && lines[i + 2][0] == '*') {
|
|
|
|
|
i += 2; // Skip the two leaves as expected
|
|
|
|
|
} else if (i + 1 < line_count && lines[i + 1][0] != '*') {
|
|
|
|
|
return 0; // Обнаружена некорректная структура (более двух дочерних узлов у вопроса)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return question_count >= leaf_count;
|
2024-11-08 13:16:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-06 08:44:29 +00:00
|
|
|
|
int count_products(Node *node) {
|
|
|
|
|
if (!node) return 0;
|
2024-11-08 12:59:10 +00:00
|
|
|
|
if (!node->yes && !node->no) return 1;
|
2024-11-06 08:44:29 +00:00
|
|
|
|
return count_products(node->yes) + count_products(node->no);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-08 12:41:41 +00:00
|
|
|
|
void run_expert_system(Node *node) {
|
2024-11-08 12:59:10 +00:00
|
|
|
|
int first_question = 1;
|
2024-11-08 12:55:47 +00:00
|
|
|
|
|
2024-11-06 08:44:29 +00:00
|
|
|
|
while (node) {
|
2024-11-08 12:59:10 +00:00
|
|
|
|
if (first_question) {
|
|
|
|
|
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
|
|
|
|
first_question = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!node->yes && !node->no) {
|
2024-11-08 09:27:18 +00:00
|
|
|
|
printf("*%s\n", node->text);
|
2024-11-08 12:55:47 +00:00
|
|
|
|
printf("Koniec\n");
|
2024-11-07 13:37:47 +00:00
|
|
|
|
return;
|
2024-11-06 10:56:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-08 09:13:34 +00:00
|
|
|
|
printf("%s\n", node->text);
|
|
|
|
|
|
2024-11-06 08:44:29 +00:00
|
|
|
|
char answer;
|
2024-11-08 12:41:41 +00:00
|
|
|
|
if (scanf(" %c", &answer) != 1) {
|
2024-11-08 12:59:10 +00:00
|
|
|
|
printf("Koniec vstupu\n");
|
|
|
|
|
return;
|
2024-11-06 09:16:26 +00:00
|
|
|
|
}
|
2024-11-06 09:31:09 +00:00
|
|
|
|
|
2024-11-06 08:44:29 +00:00
|
|
|
|
if (answer == 'a') {
|
|
|
|
|
node = node->yes;
|
|
|
|
|
} else if (answer == 'n') {
|
|
|
|
|
node = node->no;
|
|
|
|
|
} else {
|
2024-11-08 12:48:23 +00:00
|
|
|
|
printf("Nerozumiem\n");
|
2024-11-08 08:50:36 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2024-11-06 08:44:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-06 09:02:55 +00:00
|
|
|
|
void free_tree(Node *node) {
|
|
|
|
|
if (node) {
|
|
|
|
|
free_tree(node->yes);
|
|
|
|
|
free_tree(node->no);
|
|
|
|
|
free(node);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-06 08:44:29 +00:00
|
|
|
|
int main() {
|
2024-11-08 12:41:41 +00:00
|
|
|
|
char lines[100][MAX_LINE_LENGTH];
|
|
|
|
|
int line_count = 0;
|
|
|
|
|
|
|
|
|
|
while (fgets(lines[line_count], sizeof(lines[line_count]), stdin)) {
|
2024-11-08 12:59:10 +00:00
|
|
|
|
lines[line_count][strcspn(lines[line_count], "\n")] = 0;
|
|
|
|
|
if (lines[line_count][0] == '\0') {
|
2024-11-08 12:41:41 +00:00
|
|
|
|
break;
|
2024-11-08 10:09:32 +00:00
|
|
|
|
}
|
2024-11-08 12:41:41 +00:00
|
|
|
|
line_count++;
|
2024-11-08 10:09:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-08 13:33:17 +00:00
|
|
|
|
if (line_count == 0 || !validate_tree_structure(lines, line_count)) {
|
2024-11-08 13:01:39 +00:00
|
|
|
|
printf("Expert z bufetu to vie.\n");
|
|
|
|
|
printf("Chybna databaza\n");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-08 12:41:41 +00:00
|
|
|
|
int index = 0;
|
|
|
|
|
Node *root = parse_knowledge_base(lines, &index, line_count);
|
|
|
|
|
|
2024-11-08 13:33:17 +00:00
|
|
|
|
if (!root) {
|
2024-11-08 13:16:17 +00:00
|
|
|
|
printf("Expert z bufetu to vie.\n");
|
|
|
|
|
printf("Chybna databaza\n");
|
|
|
|
|
return 0;
|
2024-11-06 08:44:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-08 12:43:23 +00:00
|
|
|
|
printf("Expert z bufetu to vie.\n");
|
2024-11-06 08:44:29 +00:00
|
|
|
|
int product_count = count_products(root);
|
2024-11-06 09:02:55 +00:00
|
|
|
|
printf("Pozna %d druhov ovocia a zeleniny.\n", product_count);
|
2024-11-08 09:13:34 +00:00
|
|
|
|
|
2024-11-08 12:41:41 +00:00
|
|
|
|
run_expert_system(root);
|
2024-11-08 10:09:32 +00:00
|
|
|
|
|
2024-11-08 10:12:56 +00:00
|
|
|
|
free_tree(root);
|
2024-11-08 12:41:41 +00:00
|
|
|
|
|
2024-11-08 09:27:18 +00:00
|
|
|
|
return 0;
|
2024-11-06 08:44:29 +00:00
|
|
|
|
}
|
|
|
|
|
|