usaa24/cv7/program.c

137 lines
3.3 KiB
C
Raw Normal View History

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-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;
}
Node* parse_knowledge_base(FILE *file) {
char line[MAX_LINE_LENGTH];
if (!fgets(line, sizeof(line), file)) {
return NULL;
}
line[strcspn(line, "\n")] = 0;
if (line[0] == '*') {
2024-11-06 08:53:54 +00:00
return create_node(line + 1);
2024-11-06 08:44:29 +00:00
} else {
Node *node = create_node(line);
node->yes = parse_knowledge_base(file);
node->no = parse_knowledge_base(file);
return node;
}
}
int count_products(Node *node) {
if (!node) return 0;
2024-11-06 08:53:54 +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);
}
void run_expert_system(Node *node) {
while (node) {
2024-11-06 09:31:09 +00:00
// Если достигли листа, выводим ответ и завершаем
2024-11-06 08:53:54 +00:00
if (!node->yes && !node->no) {
2024-11-06 09:05:45 +00:00
printf("*%s\n", node->text);
printf("Koniec\n");
2024-11-06 08:44:29 +00:00
return;
}
2024-11-06 09:31:09 +00:00
// Выводим текущий вопрос
2024-11-06 08:44:29 +00:00
printf("%s\n", node->text);
2024-11-06 09:34:12 +00:00
// Выводим приглашение для ответа
2024-11-06 09:31:09 +00:00
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
2024-11-06 08:44:29 +00:00
char answer;
2024-11-06 09:16:26 +00:00
if (scanf(" %c", &answer) != 1) {
2024-11-06 09:31:09 +00:00
// Если нет корректного ввода, завершаем
2024-11-06 09:16:26 +00:00
printf("Koniec\n");
return;
}
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-06 09:31:09 +00:00
// Если введен некорректный ответ, завершаем
2024-11-06 09:16:26 +00:00
printf("Koniec\n");
2024-11-06 08:44:29 +00:00
return;
}
}
}
2024-11-06 09:22:29 +00:00
2024-11-06 09:31:09 +00:00
2024-11-06 09:34:12 +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 09:00:29 +00:00
void create_default_knowledge_base() {
FILE *file = fopen("knowledge_base.txt", "w");
if (!file) {
perror("Failed to create the knowledge base file");
exit(1);
}
2024-11-06 09:09:19 +00:00
fprintf(file, "Je to ovocie alebo zelenina\n");
2024-11-06 09:00:29 +00:00
fprintf(file, "*Jablko\n");
fprintf(file, "*Mrkva\n");
fclose(file);
}
2024-11-06 08:44:29 +00:00
int main() {
FILE *file = fopen("knowledge_base.txt", "r");
if (!file) {
2024-11-06 09:00:29 +00:00
create_default_knowledge_base();
file = fopen("knowledge_base.txt", "r");
if (!file) {
perror("Failed to open the newly created knowledge base file");
return 1;
}
2024-11-06 08:44:29 +00:00
}
Node *root = parse_knowledge_base(file);
fclose(file);
if (!root) {
2024-11-06 09:02:55 +00:00
printf("Báza znalostí sa nedá načítať.\n");
2024-11-06 08:44:29 +00:00
return 1;
}
2024-11-06 09:12:28 +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-06 08:44:29 +00:00
run_expert_system(root);
2024-11-06 09:02:55 +00:00
free_tree(root);
2024-11-06 08:44:29 +00:00
return 0;
}