2024-11-14 20:29:44 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
typedef struct Node {
|
|
|
|
char *text;
|
|
|
|
struct Node *yes;
|
|
|
|
struct Node *no;
|
|
|
|
} Node;
|
|
|
|
|
|
|
|
Node* vytvor_uzol(char *text) {
|
|
|
|
Node *uzol = (Node*)malloc(sizeof(Node));
|
|
|
|
uzol->text = strdup(text);
|
|
|
|
uzol->yes = NULL;
|
|
|
|
uzol->no = NULL;
|
|
|
|
return uzol;
|
|
|
|
}
|
|
|
|
|
|
|
|
void uvolni(Node *uzol) {
|
|
|
|
if (uzol) {
|
|
|
|
free(uzol->text);
|
|
|
|
uvolni(uzol->yes);
|
|
|
|
uvolni(uzol->no);
|
|
|
|
free(uzol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void naloz_bazu(Node **root, FILE *file) {
|
|
|
|
char line[256];
|
|
|
|
if (fgets(line, sizeof(line), file) == NULL || line[0] == '\n') {
|
|
|
|
*root = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line[0] == '*') {
|
|
|
|
*root = vytvor_uzol(line + 1); // odstránenie '*'
|
|
|
|
} else {
|
|
|
|
*root = vytvor_uzol(line);
|
|
|
|
naloz_bazu(&((*root)->yes), file);
|
|
|
|
naloz_bazu(&((*root)->no), file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int spocitaj_druhy(Node *uzol) {
|
|
|
|
if (!uzol) return 0;
|
|
|
|
if (!uzol->yes && !uzol->no) return 1; // odpoveď
|
|
|
|
return spocitaj_druhy(uzol->yes) + spocitaj_druhy(uzol->no);
|
|
|
|
}
|
|
|
|
|
|
|
|
void znalostny_system(Node *uzol) {
|
|
|
|
char odpoved;
|
|
|
|
|
|
|
|
while (uzol) {
|
|
|
|
if (!uzol->yes && !uzol->no) {
|
|
|
|
printf("Expert z bufetu to vie: %s\n", uzol->text);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%s (a/n): ", uzol->text);
|
|
|
|
scanf(" %c", &odpoved);
|
|
|
|
|
|
|
|
if (odpoved == 'a') {
|
|
|
|
uzol = uzol->yes;
|
|
|
|
} else if (odpoved == 'n') {
|
|
|
|
uzol = uzol->no;
|
|
|
|
} else {
|
|
|
|
printf("Nespravny vstup, program konci.\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-14 20:31:59 +00:00
|
|
|
int main(int argc, char *argv[]) {
|
2024-11-14 20:33:26 +00:00
|
|
|
const char *filename = "baza.txt";
|
|
|
|
if (argc > 1) {
|
|
|
|
filename = argv[1];
|
2024-11-14 20:31:59 +00:00
|
|
|
}
|
|
|
|
|
2024-11-14 20:33:26 +00:00
|
|
|
FILE *file = fopen(filename, "r");
|
2024-11-14 20:29:44 +00:00
|
|
|
if (!file) {
|
|
|
|
printf("Chyba pri otváraní súboru.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node *root = NULL;
|
|
|
|
naloz_bazu(&root, file);
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
if (root == NULL) {
|
|
|
|
printf("Chyba: nepodarilo sa načítať bázu pravidiel.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int druhy = spocitaj_druhy(root);
|
|
|
|
printf("Expert z bufetu to vie.\nPozna %d druhov ovocia a zeleniny.\n", druhy);
|
|
|
|
|
|
|
|
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
|
|
|
znalostny_system(root);
|
|
|
|
|
|
|
|
uvolni(root);
|
|
|
|
return 0;
|
|
|
|
}
|
2024-11-14 20:31:59 +00:00
|
|
|
|
2024-11-14 20:33:26 +00:00
|
|
|
|