2024-11-06 07:18:40 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2024-11-06 08:29:17 +00:00
|
|
|
#include <assert.h>
|
2024-11-06 07:18:40 +00:00
|
|
|
|
2024-11-06 08:18:46 +00:00
|
|
|
#define SIZE 256
|
|
|
|
|
2024-11-06 08:29:17 +00:00
|
|
|
// Štruktúra pre uzol binárneho stromu
|
|
|
|
typedef struct TreeNode {
|
|
|
|
char value[SIZE]; // Otázka alebo odpoveď
|
|
|
|
struct TreeNode* yes; // Ukazovateľ na "áno" vetvu
|
|
|
|
struct TreeNode* no; // Ukazovateľ na "nie" vetvu
|
|
|
|
} TreeNode;
|
2024-11-06 07:18:40 +00:00
|
|
|
|
2024-11-06 08:29:17 +00:00
|
|
|
// Funkcia na načítanie stromu z pravidiel v súbore
|
|
|
|
TreeNode* read_tree(FILE* file) {
|
2024-11-06 08:18:46 +00:00
|
|
|
char buffer[SIZE];
|
2024-11-06 08:29:17 +00:00
|
|
|
if (fgets(buffer, SIZE, file) == NULL || buffer[0] == '\n') {
|
|
|
|
return NULL; // Koniec súboru alebo prázdny riadok
|
2024-11-06 08:18:46 +00:00
|
|
|
}
|
2024-11-06 07:44:04 +00:00
|
|
|
|
2024-11-06 08:29:17 +00:00
|
|
|
// Vytvoríme nový uzol a skopírujeme hodnotu
|
|
|
|
TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode));
|
|
|
|
strncpy(node->value, buffer, SIZE);
|
2024-11-06 07:44:04 +00:00
|
|
|
|
2024-11-06 08:29:17 +00:00
|
|
|
// Ak je to odpoveď (začína hviezdičkou), uzol je listový
|
2024-11-06 08:18:46 +00:00
|
|
|
if (buffer[0] == '*') {
|
2024-11-06 08:29:17 +00:00
|
|
|
node->yes = NULL;
|
|
|
|
node->no = NULL;
|
|
|
|
} else {
|
|
|
|
// Inak načítame rekurzívne "áno" a "nie" podstromy
|
|
|
|
node->yes = read_tree(file);
|
|
|
|
node->no = read_tree(file);
|
2024-11-06 07:18:40 +00:00
|
|
|
}
|
2024-11-06 08:18:46 +00:00
|
|
|
return node;
|
2024-11-06 07:18:40 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 08:29:17 +00:00
|
|
|
// Funkcia na uvoľnenie stromu z pamäte
|
|
|
|
void destroy_tree(TreeNode* node) {
|
|
|
|
if (node == NULL) return;
|
|
|
|
destroy_tree(node->yes);
|
|
|
|
destroy_tree(node->no);
|
|
|
|
free(node);
|
2024-11-06 07:18:40 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 08:29:17 +00:00
|
|
|
// Funkcia na výpis stromu s odsadením (na ladenie)
|
|
|
|
void print_tree(TreeNode* node, int offset) {
|
2024-11-06 08:18:46 +00:00
|
|
|
if (node == NULL) return;
|
|
|
|
|
2024-11-06 08:29:17 +00:00
|
|
|
for (int i = 0; i < offset; i++) {
|
|
|
|
printf(" ");
|
|
|
|
}
|
2024-11-06 08:18:46 +00:00
|
|
|
printf("%s", node->value);
|
|
|
|
|
2024-11-06 08:29:17 +00:00
|
|
|
// Rekurzívne vypíšeme "áno" a "nie" podstromy
|
|
|
|
if (node->yes) print_tree(node->yes, offset + 3);
|
|
|
|
if (node->no) print_tree(node->no, offset + 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Funkcia na spočítanie listových uzlov (tovarov)
|
|
|
|
int count_leaves(TreeNode* node) {
|
|
|
|
if (node == NULL) return 0;
|
|
|
|
if (node->yes == NULL && node->no == NULL) return 1; // Je to listový uzol
|
|
|
|
return count_leaves(node->yes) + count_leaves(node->no);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Funkcia na spustenie znalostného systému
|
|
|
|
void run_system(TreeNode* node) {
|
|
|
|
if (node == NULL) return;
|
|
|
|
|
|
|
|
// Ak je uzol listový (odpoveď), vypíšeme odpoveď a skončíme
|
|
|
|
if (node->yes == NULL && node->no == NULL) {
|
|
|
|
printf("MUDrC to vie.\n");
|
|
|
|
printf("%s", node->value);
|
2024-11-06 08:18:46 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-11-06 08:29:17 +00:00
|
|
|
// Zobrazíme otázku a čakáme na vstup používateľa
|
|
|
|
printf("%s", node->value);
|
2024-11-06 08:18:46 +00:00
|
|
|
char answer;
|
|
|
|
scanf(" %c", &answer);
|
|
|
|
|
2024-11-06 08:29:17 +00:00
|
|
|
// Podľa odpovede pokračujeme v "áno" alebo "nie" vetve
|
2024-11-06 08:18:46 +00:00
|
|
|
if (answer == 'a') {
|
2024-11-06 08:29:17 +00:00
|
|
|
run_system(node->yes);
|
2024-11-06 08:18:46 +00:00
|
|
|
} else if (answer == 'n') {
|
2024-11-06 08:29:17 +00:00
|
|
|
run_system(node->no);
|
2024-11-06 08:18:46 +00:00
|
|
|
} else {
|
2024-11-06 08:29:17 +00:00
|
|
|
printf("Odpovedajte 'a' alebo 'n'\n");
|
2024-11-06 07:18:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-06 08:29:17 +00:00
|
|
|
// Hlavná funkcia
|
2024-11-06 07:18:40 +00:00
|
|
|
int main() {
|
2024-11-06 08:29:17 +00:00
|
|
|
FILE* file = fopen("baza.txt", "r");
|
|
|
|
if (!file) {
|
|
|
|
fprintf(stderr, "Nepodarilo sa otvoriť súbor\n");
|
|
|
|
return 1;
|
2024-11-06 07:18:40 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 08:29:17 +00:00
|
|
|
// Načítanie stromu z pravidiel v súbore
|
|
|
|
TreeNode* root = read_tree(file);
|
2024-11-06 07:18:40 +00:00
|
|
|
fclose(file);
|
|
|
|
|
2024-11-06 08:29:17 +00:00
|
|
|
// Skontrolujeme, či je súbor správne načítaný
|
2024-11-06 08:18:46 +00:00
|
|
|
if (root == NULL) {
|
2024-11-06 08:29:17 +00:00
|
|
|
printf("Chyba: neplatná báza pravidiel\n");
|
|
|
|
return 1;
|
2024-11-06 07:18:40 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 08:29:17 +00:00
|
|
|
// Spočítame listové uzly (tovary)
|
|
|
|
int leaves_count = count_leaves(root);
|
|
|
|
printf("Pozná %d druhov ovocia a zeleniny.\n", leaves_count);
|
2024-11-06 07:18:40 +00:00
|
|
|
|
2024-11-06 08:29:17 +00:00
|
|
|
// Spustíme znalostný systém
|
|
|
|
run_system(root);
|
2024-11-06 07:18:40 +00:00
|
|
|
|
2024-11-06 08:29:17 +00:00
|
|
|
// Uvoľníme pamäť
|
|
|
|
destroy_tree(root);
|
2024-11-06 07:18:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|