commit 6628779373bdf04c6131a4776e82185bea127cb0 Author: zayd.sadik Date: Mon Mar 4 15:55:22 2024 +0100 mon fichier diff --git a/projet calculatrice zayd.py b/projet calculatrice zayd.py new file mode 100644 index 0000000..b1f9f89 --- /dev/null +++ b/projet calculatrice zayd.py @@ -0,0 +1,61 @@ +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] + +import tkinter as tk + +def evaluate_expression(): + expression_text = entry.get() + npi_list = expression_text.split() + expression_tree = npi2tree(npi_list) + result.set(expression_tree.evaluate()) + +root = tk.Tk() +root.configure(bg="orange") +root.title("Mini Calculatrice") + +expression_label = tk.Label(root, text="Expression:") +expression_label.pack() + +expression_label.configure(bg="beige") + +entry = tk.Entry(root) +entry.pack() + +calculate_button = tk.Button(root, text="Calculer", command=evaluate_expression) +calculate_button.configure(bg="beige") +calculate_button.pack() + +result = tk.StringVar() +result_label = tk.Label(root, textvariable=result) +result_label.configure(bg="orange") +result_label.pack() + +root.mainloop() \ No newline at end of file