diff --git a/cv7/program.c b/cv7/program.c index e69de29..965c8bf 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -0,0 +1,77 @@ +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 + +class KnowledgeSystem: + def __init__(self): + self.root = None + self.num_answers = 0 + + def load_rules(self, lines): + # Initialize index for lines + index = 0 + + # 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 + + if not line: + return None + + is_answer = line.startswith("*") + text = line[1:].strip() if is_answer else line + + 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 + + # Parse the tree from the first line + self.root = parse_node() + + 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 + +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) + + # 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) + +if __name__ == "__main__": + main() \ No newline at end of file