Update cv7/program.c

This commit is contained in:
Viktor Daniv 2024-11-15 00:42:02 +00:00
parent e6b4b31cf3
commit 3717629634

View File

@ -1,77 +1,103 @@
class Node:
def __init__(self, text, is_answer=False):
self.text = text
self.is_answer = is_answer
self.yes_branch = None
self.no_branch = None
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
class KnowledgeSystem:
def __init__(self):
self.root = None
self.num_answers = 0
// Štruktúra pre uzol v binárnom strome
typedef struct Node {
char text[100];
int is_answer; // 1 pre odpoveď, 0 pre otázku
struct Node *yes_branch;
struct Node *no_branch;
} Node;
def load_rules(self, lines):
# Initialize index for lines
index = 0
// Vytvára nový uzol s textom a typom (odpoveď alebo otázka)
Node *create_node(const char *text, int is_answer) {
Node *node = malloc(sizeof(Node));
if (!node) {
printf("Chyba: Nepodarilo sa alokovať pamäť.\n");
exit(1);
}
strcpy(node->text, text);
node->is_answer = is_answer;
node->yes_branch = NULL;
node->no_branch = NULL;
return node;
}
# Recursive function to parse lines into a binary tree
def parse_node():
nonlocal index
if index >= len(lines):
raise ValueError("Chyba: Neočakávaný koniec pravidiel.")
line = lines[index].strip()
index += 1
// Funkcia na načítanie bázy pravidiel do binárneho stromu
Node *load_rules(FILE *file, int *num_answers) {
char line[100];
if (!fgets(line, sizeof(line), file) || line[0] == '\n') {
return NULL; // Prázdny riadok, koniec pravidiel
}
if not line:
return None
int is_answer = line[0] == '*';
char *text = is_answer ? line + 1 : line;
text[strcspn(text, "\n")] = 0; // Odstráni znak nového riadka
is_answer = line.startswith("*")
text = line[1:].strip() if is_answer else line
Node *node = create_node(text, is_answer);
if (is_answer) {
(*num_answers)++;
return node;
}
node = Node(text, is_answer)
if is_answer:
self.num_answers += 1
return node
node.yes_branch = parse_node() # Process yes branch
node.no_branch = parse_node() # Process no branch
return node
node->yes_branch = load_rules(file, num_answers);
node->no_branch = load_rules(file, num_answers);
return node;
}
# Parse the tree from the first line
self.root = parse_node()
// Funkcia na kladenie otázok používateľovi
void ask_questions(Node *node) {
while (node) {
printf("%s\n", node->text);
if (node->is_answer) {
printf("Koniec.\n");
return;
}
def ask_questions(self):
current = self.root
while current:
print(current.text)
if current.is_answer:
print("Koniec.")
break
answer = input("Odpovedajte 'a' pre prvú možnosť alebo 'n' pre druhú možnosť: ").strip().lower()
if answer == 'a':
current = current.yes_branch
elif answer == 'n':
current = current.no_branch
else:
print("Neplatná odpoveď, koniec.")
break
char answer;
printf("Odpovedajte 'a' pre prvú možnosť alebo 'n' pre druhú možnosť: ");
scanf(" %c", &answer);
def main():
lines = []
print("Zadajte bázu pravidiel (prázdny riadok pre koniec):")
while True:
line = input()
if not line.strip():
break
lines.append(line)
if (answer == 'a') {
node = node->yes_branch;
} else if (answer == 'n') {
node = node->no_branch;
} else {
printf("Neplatná odpoveď, koniec.\n");
return;
}
}
}
# Initialize knowledge system and load rules
ks = KnowledgeSystem()
try:
ks.load_rules(lines)
print(f"Pozná {ks.num_answers} druhov ovocia a zeleniny.")
ks.ask_questions()
except ValueError as e:
print(e)
// Uvoľní pamäť pre všetky uzly v binárnom strome
void free_tree(Node *node) {
if (node) {
free_tree(node->yes_branch);
free_tree(node->no_branch);
free(node);
}
}
if __name__ == "__main__":
main()
int main() {
FILE *file = fopen("rules.txt", "r");
if (!file) {
printf("Chyba: Nepodarilo sa otvoriť súbor s pravidlami.\n");
return 1;
}
int num_answers = 0;
Node *root = load_rules(file, &num_answers);
fclose(file);
if (!root) {
printf("Chyba: Nepodarilo sa načítať bázu pravidiel.\n");
return 1;
}
printf("Pozná %d druhov ovocia a zeleniny.\n", num_answers);
ask_questions(root);
free_tree(root);
return 0;
}