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