128 lines
3.3 KiB
C
128 lines
3.3 KiB
C
#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) {
|
|
fprintf(stderr, "Memory allocation error.\n");
|
|
exit(1);
|
|
}
|
|
strncpy(node->text, text, MAX_LINE_LENGTH);
|
|
node->yes = NULL;
|
|
node->no = NULL;
|
|
return node;
|
|
}
|
|
|
|
Node* parse_knowledge_base(FILE *file) {
|
|
char line[MAX_LINE_LENGTH];
|
|
|
|
if (!fgets(line, sizeof(line), file)) {
|
|
return NULL;
|
|
}
|
|
|
|
line[strcspn(line, "\n")] = 0; // Remove newline character
|
|
|
|
if (line[0] == '*') {
|
|
return create_node(line + 1); // Create answer node, skip '*'
|
|
} else {
|
|
Node *node = create_node(line);
|
|
node->yes = parse_knowledge_base(file); // Recursively read "yes" answer
|
|
node->no = parse_knowledge_base(file); // Recursively read "no" answer
|
|
return node;
|
|
}
|
|
}
|
|
|
|
int count_products(Node *node) {
|
|
if (!node) return 0;
|
|
if (!node->yes && !node->no) return 1; // If node is a leaf (answer)
|
|
return count_products(node->yes) + count_products(node->no);
|
|
}
|
|
|
|
void run_expert_system(Node *node) {
|
|
while (node) {
|
|
if (!node->yes && !node->no) { // Если это листовой узел
|
|
printf("*%s\n", node->text); // Выводим только ответ
|
|
printf("Koniec\n");
|
|
return;
|
|
}
|
|
|
|
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
|
printf("%s\n", node->text);
|
|
|
|
char answer;
|
|
if (scanf(" %c", &answer) != 1) {
|
|
printf("Koniec\n"); // Неправильный ввод, завершение
|
|
return;
|
|
}
|
|
|
|
// Переход к следующему узлу в зависимости от ответа пользователя
|
|
if (answer == 'a') {
|
|
node = node->yes;
|
|
} else if (answer == 'n') {
|
|
node = node->no;
|
|
} else {
|
|
printf("Koniec\n"); // Неправильный ответ, завершение
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
void free_tree(Node *node) {
|
|
if (node) {
|
|
free_tree(node->yes);
|
|
free_tree(node->no);
|
|
free(node);
|
|
}
|
|
}
|
|
|
|
void create_default_knowledge_base() {
|
|
FILE *file = fopen("knowledge_base.txt", "w");
|
|
if (!file) {
|
|
perror("Failed to create the knowledge base file");
|
|
exit(1);
|
|
}
|
|
fprintf(file, "Je to ovocie alebo zelenina\n");
|
|
fprintf(file, "*Jablko\n");
|
|
fprintf(file, "*Mrkva\n");
|
|
fclose(file);
|
|
}
|
|
|
|
int main() {
|
|
FILE *file = fopen("knowledge_base.txt", "r");
|
|
if (!file) {
|
|
create_default_knowledge_base();
|
|
file = fopen("knowledge_base.txt", "r");
|
|
if (!file) {
|
|
perror("Failed to open the newly created knowledge base file");
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
Node *root = parse_knowledge_base(file);
|
|
fclose(file);
|
|
|
|
if (!root) {
|
|
printf("Báza znalostí sa nedá načítať.\n");
|
|
return 1;
|
|
}
|
|
|
|
printf("Expert z bufetu to vie.\n");
|
|
int product_count = count_products(root);
|
|
printf("Pozna %d druhov ovocia a zeleniny.\n", product_count);
|
|
|
|
run_expert_system(root);
|
|
|
|
free_tree(root);
|
|
return 0;
|
|
}
|
|
|