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;
}
def ask_questions(self): char answer;
current = self.root printf("Odpovedajte 'a' pre prvú možnosť alebo 'n' pre druhú možnosť: ");
while current: scanf(" %c", &answer);
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
def main(): if (answer == 'a') {
lines = [] node = node->yes_branch;
print("Zadajte bázu pravidiel (prázdny riadok pre koniec):") } else if (answer == 'n') {
while True: node = node->no_branch;
line = input() } else {
if not line.strip(): printf("Neplatná odpoveď, koniec.\n");
break return;
lines.append(line) }
}
}
# Initialize knowledge system and load rules // Uvoľní pamäť pre všetky uzly v binárnom strome
ks = KnowledgeSystem() void free_tree(Node *node) {
try: if (node) {
ks.load_rules(lines) free_tree(node->yes_branch);
print(f"Pozná {ks.num_answers} druhov ovocia a zeleniny.") free_tree(node->no_branch);
ks.ask_questions() free(node);
except ValueError as e: }
print(e) }
if __name__ == "__main__": int main() {
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;
}