From 16d121a8793d94774f3530c8c109d8859e9a522c Mon Sep 17 00:00:00 2001 From: "paulin.pommier" Date: Fri, 16 Feb 2024 14:30:00 +0100 Subject: [PATCH] mon fichier --- projet calculatrice.py | 62 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 projet calculatrice.py diff --git a/projet calculatrice.py b/projet calculatrice.py new file mode 100644 index 0000000..c83d1ea --- /dev/null +++ b/projet calculatrice.py @@ -0,0 +1,62 @@ +import tkinter as tk + +class Expression: + def __init__(self, root, left, right): + self.root = root + self.left = left + self.right = right + + def evaluate(self): + if self.root == '+': + return self.left.evaluate() + self.right.evaluate() + elif self.root == '*': + return self.left.evaluate() * self.right.evaluate() + else: + return int(self.root) + + def __str__(self): + if self.root in ['+', '*']: + return f'({self.left} {self.root} {self.right})' + else: + return str(self.root) + +def npi2tree(npi_list): + stack = [] + for item in npi_list: + if item in ['+', '*']: + right = stack.pop() + left = stack.pop() + stack.append(Expression(item, left, right)) + else: + stack.append(Expression(item, None, None)) + return stack[0] + +def evaluate_expression(): + expression_text = entry.get() + if expression_text: + npi_list = expression_text.split() + try: + expression_tree = npi2tree(npi_list) + result.set(expression_tree.evaluate()) + except Exception as e: + result.set("Erreur: " + str(e)) + else: + result.set("Veuillez entrer une expression") + +root = tk.Tk() +root.title("Mini Calculatrice") + +expression_label = tk.Label(root, text="Expression:") +expression_label.pack() + +entry = tk.Entry(root) +entry.pack() + +calculate_button = tk.Button(root, text="Calculer", command=evaluate_expression) +calculate_button.pack() + +result = tk.StringVar() +result_label = tk.Label(root, textvariable=result) +result_label.pack() + +root.mainloop()