This commit is contained in:
Your Name 2025-11-20 15:08:15 +01:00
parent d53ab280c9
commit 3e011eb80f

View File

@ -6,49 +6,54 @@
#define SIZE 256 #define SIZE 256
struct tree struct tree {
{
char value[SIZE]; char value[SIZE];
struct tree* left; struct tree* left;
struct tree* right; struct tree* right;
}; };
void trim_whitespace(char* str) // Funkcia na odstránenie bieleho miesta z reťazca
{ void trim_whitespace(char* str) {
int i=strlen(str)-1; int i = strlen(str) - 1;
while (i>=0 && isspace(str[i])) while (i >= 0 && isspace(str[i])) {
{ str[i] = '\0';
str[i]='\0';
i--; i--;
} }
} }
struct tree* read_tree() // Funkcia na načítanie stromu z pravidiel
{ struct tree* read_tree() {
char buffer[SIZE]; char buffer[SIZE];
if (fgets(buffer, SIZE, stdin)==NULL)
{ // Načítanie riadku
if (fgets(buffer, SIZE, stdin) == NULL) {
return NULL; return NULL;
} }
if (buffer[0]=='\n')
{ // Ak je prázdny riadok, vráť NULL
if (buffer[0] == '\n') {
return NULL; return NULL;
} }
// Odstráň biele miesto
trim_whitespace(buffer); trim_whitespace(buffer);
struct tree* node=(struct tree*)calloc(1, sizeof(struct tree));
// Vytvorenie nového uzla
struct tree* node = (struct tree*)calloc(1, sizeof(struct tree));
strcpy(node->value, buffer); strcpy(node->value, buffer);
if (buffer[0]!='*')
{ // Ak nie je listový uzol (nezačína sa *), rekurzívne načítaj ľavého a pravého syna
node->left=read_tree(); if (buffer[0] != '*') {
node->right=read_tree(); node->left = read_tree();
node->right = read_tree();
} }
return node; return node;
} }
void destroy_tree(struct tree* tree) // Funkcia na uvoľnenie pamäte stromu
{ void destroy_tree(struct tree* tree) {
if (tree==NULL) if (tree == NULL) {
{
return; return;
} }
destroy_tree(tree->left); destroy_tree(tree->left);
@ -56,86 +61,89 @@ void destroy_tree(struct tree* tree)
free(tree); free(tree);
} }
int count_answers(struct tree* tree) // Funkcia na spočítanie listových uzlov (odpovedí)
{ int count_answers(struct tree* tree) {
if (tree==NULL) if (tree == NULL) {
{
return 0; return 0;
} }
if (tree->value[0]=='*')
{ // Ak je listový uzol (odpoveď)
if (tree->value[0] == '*') {
return 1; return 1;
} }
return count_answers(tree->left)+count_answers(tree->right);
// Inak rekurzívne spočítaj odpovede z podstromov
return count_answers(tree->left) + count_answers(tree->right);
} }
void run_knowledge_system(struct tree* node) // Funkcia na spustenie znalostného systému
{ void run_knowledge_system(struct tree* node) {
if (node==NULL) if (node == NULL) {
{
return; return;
} }
if (node->value[0]=='*')
{ // Vypíš otázku/odpoveď
printf("%s\n", node->value); printf("%s\n", node->value);
}
else // Ak je to listový uzol (odpoveď), skonči
{ if (node->value[0] == '*') {
printf("%s\n", node->value);
}
if (node->value[0]=='*')
{
return; return;
} }
if (node->left!=NULL || node->right!=NULL)
{ // Ak má potomkov, pokračuj podľa odpovede používateľa
if (node->left != NULL || node->right != NULL) {
char answer[10]; char answer[10];
if (fgets(answer, sizeof(answer), stdin)==NULL)
{ // Načítaj odpoveď používateľa
if (fgets(answer, sizeof(answer), stdin) == NULL) {
printf("Koniec\n"); printf("Koniec\n");
return; return;
} }
if (answer[0]=='a')
{ // Spracuj odpoveď
if (answer[0] == 'a') {
run_knowledge_system(node->left); run_knowledge_system(node->left);
} } else if (answer[0] == 'n') {
else if (answer[0]=='n')
{
run_knowledge_system(node->right); run_knowledge_system(node->right);
} } else {
else
{
printf("Nespravny vstup.\n"); printf("Nespravny vstup.\n");
return; return;
} }
} }
printf("Koniec\n"); printf("Koniec\n");
} }
int main() int main() {
{ // Načítanie bázy pravidiel
struct tree* knowledge_base=read_tree(); struct tree* knowledge_base = read_tree();
// Overenie, či nasleduje prázdny riadok
char empty_line[SIZE]; char empty_line[SIZE];
if (fgets(empty_line, SIZE, stdin)==NULL || empty_line[0]!='\n') if (fgets(empty_line, SIZE, stdin) == NULL || empty_line[0] != '\n') {
{
printf("Nespravna baza pravidiel.\n"); printf("Nespravna baza pravidiel.\n");
if (knowledge_base!=NULL) if (knowledge_base != NULL) {
{
destroy_tree(knowledge_base); destroy_tree(knowledge_base);
} }
return 1; return 1;
} }
if (knowledge_base==NULL)
{ // Overenie platnosti bázy pravidiel
if (knowledge_base == NULL) {
printf("Nespravna baza pravidiel.\n"); printf("Nespravna baza pravidiel.\n");
return 1; return 1;
} }
int answer_count=count_answers(knowledge_base);
printf("Expert_x_bufetu_to_vie.\n"); // Spočítanie a výpis počtu tovarov
printf("Pozna_%d_druhov_ovocia_a_zeleniny.\n", answer_count); int answer_count = count_answers(knowledge_base);
printf("Odpovedajte_'a'_pre_prvu_moznost_alebo_'n'_pre_druhu_moznost.\n"); printf("Expert z bufetu to vie.\n"); // Medzery namiesto podtržníkov
printf("Pozna %d druhov ovocia a zeleniny.\n", answer_count);
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
// Spustenie znalostného systému
run_knowledge_system(knowledge_base); run_knowledge_system(knowledge_base);
// Vyčistenie pamäte
destroy_tree(knowledge_base); destroy_tree(knowledge_base);
return 0; return 0;
} }