This commit is contained in:
Deinerovych 2024-11-06 10:02:55 +01:00
parent b19a7bfeac
commit 21c800482f

View File

@ -50,11 +50,11 @@ int count_products(Node *node) {
void run_expert_system(Node *node) { void run_expert_system(Node *node) {
while (node) { while (node) {
if (!node->yes && !node->no) { if (!node->yes && !node->no) {
printf("The expert knows: %s.\n", node->text); printf("Expert z bufetu to vie: %s.\n", node->text);
return; return;
} }
printf("%s\n", node->text); printf("%s\n", node->text);
printf("Answer 'a' for the first option or 'n' for the second option: "); printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost: ");
char answer; char answer;
scanf(" %c", &answer); scanf(" %c", &answer);
if (answer == 'a') { if (answer == 'a') {
@ -62,12 +62,20 @@ void run_expert_system(Node *node) {
} else if (answer == 'n') { } else if (answer == 'n') {
node = node->no; node = node->no;
} else { } else {
printf("Invalid answer. Exiting.\n"); printf("Neplatná odpoveď. Koniec.\n");
return; return;
} }
} }
} }
void free_tree(Node *node) {
if (node) {
free_tree(node->yes);
free_tree(node->no);
free(node);
}
}
void create_default_knowledge_base() { void create_default_knowledge_base() {
FILE *file = fopen("knowledge_base.txt", "w"); FILE *file = fopen("knowledge_base.txt", "w");
if (!file) { if (!file) {
@ -78,13 +86,11 @@ void create_default_knowledge_base() {
fprintf(file, "*Jablko\n"); fprintf(file, "*Jablko\n");
fprintf(file, "*Mrkva\n"); fprintf(file, "*Mrkva\n");
fclose(file); fclose(file);
printf("Default knowledge base file 'knowledge_base.txt' has been created.\n");
} }
int main() { int main() {
FILE *file = fopen("knowledge_base.txt", "r"); FILE *file = fopen("knowledge_base.txt", "r");
if (!file) { if (!file) {
printf("Knowledge base file not found. Creating default file.\n");
create_default_knowledge_base(); create_default_knowledge_base();
file = fopen("knowledge_base.txt", "r"); file = fopen("knowledge_base.txt", "r");
if (!file) { if (!file) {
@ -97,16 +103,16 @@ int main() {
fclose(file); fclose(file);
if (!root) { if (!root) {
printf("The knowledge base could not be loaded.\n"); printf("Báza znalostí sa nedá načítať.\n");
return 1; return 1;
} }
int product_count = count_products(root); int product_count = count_products(root);
printf("The system knows about %d types of fruits and vegetables.\n", product_count); printf("Pozna %d druhov ovocia a zeleniny.\n", product_count);
run_expert_system(root); run_expert_system(root);
free(root); free_tree(root);
return 0; return 0;
} }