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;
|
|
|
|
|
|
2024-11-08 10:05:41 +00:00
|
|
|
|
// Создает новый узел с заданным текстом
|
2024-11-06 08:44:29 +00:00
|
|
|
|
Node* create_node(const char* text) {
|
|
|
|
|
Node *node = (Node*)malloc(sizeof(Node));
|
|
|
|
|
if (!node) {
|
2024-11-06 08:53:54 +00:00
|
|
|
|
fprintf(stderr, "Memory allocation error.\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 10:05:41 +00:00
|
|
|
|
// Создает заранее заданное дерево знаний
|
2024-11-08 09:59:05 +00:00
|
|
|
|
Node* build_knowledge_tree() {
|
|
|
|
|
// Создаем корневой узел
|
|
|
|
|
Node *root = create_node("Rastie to na strome?");
|
|
|
|
|
|
2024-11-08 10:05:41 +00:00
|
|
|
|
// Добавляем варианты ответов
|
2024-11-08 09:59:05 +00:00
|
|
|
|
root->yes = create_node("Jablko");
|
|
|
|
|
root->no = create_node("Rastie to pod zemou?");
|
|
|
|
|
|
2024-11-08 10:05:41 +00:00
|
|
|
|
// Узлы для "Rastie to pod zemou?"
|
2024-11-08 09:59:05 +00:00
|
|
|
|
root->no->yes = create_node("Mrkva");
|
|
|
|
|
root->no->no = create_node("Šalát");
|
|
|
|
|
|
|
|
|
|
return root;
|
2024-11-06 08:44:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-08 10:05:41 +00:00
|
|
|
|
// Подсчитывает количество листовых узлов (продуктов)
|
2024-11-06 08:44:29 +00:00
|
|
|
|
int count_products(Node *node) {
|
|
|
|
|
if (!node) return 0;
|
2024-11-08 10:05:41 +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 10:05:41 +00:00
|
|
|
|
// Запускает экспертную систему
|
|
|
|
|
void run_expert_system(Node *node) {
|
2024-11-06 08:44:29 +00:00
|
|
|
|
while (node) {
|
2024-11-07 13:57:36 +00:00
|
|
|
|
if (!node->yes && !node->no) {
|
2024-11-08 09:27:18 +00:00
|
|
|
|
printf("*%s\n", node->text);
|
2024-11-07 13:35:10 +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("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
|
|
|
|
printf("%s\n", node->text);
|
|
|
|
|
|
2024-11-06 08:44:29 +00:00
|
|
|
|
char answer;
|
2024-11-08 10:05:41 +00:00
|
|
|
|
if (scanf(" %c", &answer) != 1) {
|
2024-11-08 09:27:18 +00:00
|
|
|
|
printf("Koniec\n");
|
2024-11-07 13:35:10 +00:00
|
|
|
|
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 09:42:02 +00:00
|
|
|
|
printf("Nespravny vstup.\nKoniec\n");
|
2024-11-08 08:50:36 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2024-11-06 08:44:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-08 10:05:41 +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 10:05:41 +00:00
|
|
|
|
// Создаем дерево знаний вручную
|
2024-11-08 09:59:05 +00:00
|
|
|
|
Node *root = build_knowledge_tree();
|
2024-11-06 08:44:29 +00:00
|
|
|
|
|
|
|
|
|
if (!root) {
|
2024-11-06 09:02:55 +00:00
|
|
|
|
printf("Báza znalostí sa nedá načítať.\n");
|
2024-11-08 09:27:18 +00:00
|
|
|
|
return 1;
|
2024-11-06 08:44:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-06 11:06:15 +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 10:05:41 +00:00
|
|
|
|
run_expert_system(root);
|
2024-11-06 08:44:29 +00:00
|
|
|
|
|
2024-11-06 09:02:55 +00:00
|
|
|
|
free_tree(root);
|
2024-11-08 09:27:18 +00:00
|
|
|
|
return 0;
|
2024-11-06 08:44:29 +00:00
|
|
|
|
}
|
|
|
|
|
|