You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
142 lines
4.0 KiB
142 lines
4.0 KiB
"""
|
|
Mauvaise utilisation de git (un seul véritable commit !)
|
|
Fonctions non documentées (un commentaire n'est pas une documentation)
|
|
|
|
Dans npi2tree, vous ne gérez pas le cas ou la liste ne correspond pas à une expression valide.
|
|
|
|
Pour la partie tkintter :
|
|
|
|
Vous avez testé votre code ?? Vous n''avez pas remarqué qu'on ne voit pas le résultat ?!?!
|
|
|
|
Mauvaise utilisation du bloc try except.
|
|
Utilisation trop compliquée de fonctions lambda.
|
|
Avant de vouloir vous lancer dans des choses compliquées, il aurait été intéressant de
|
|
faire uniquement ce qui était demandé dans le cahier des charges ! Par exemple, que se
|
|
passe-t-il si je tapes 2 2 = ?
|
|
Pourquoi vous acharnez-vous à faire faire votre travail
|
|
par une IA sans chercher à comprendre le résultat ?!?
|
|
|
|
Note : 5 / 10
|
|
"""
|
|
|
|
|
|
import tkinter as tk
|
|
|
|
class Expression:
|
|
def __init__(self, valeur, gauche, droite):
|
|
self.valeur = valeur
|
|
self.gauche = gauche
|
|
self.droite = droite
|
|
|
|
def evalue(self):
|
|
# si c'est un entier on le renvoie
|
|
if isinstance(self.valeur, int):
|
|
return self.valeur
|
|
|
|
# sinon c’est un opérateur
|
|
if self.valeur == '+':
|
|
return self.gauche.evalue() + self.droite.evalue()
|
|
|
|
if self.valeur == '*':
|
|
return self.gauche.evalue() * self.droite.evalue()
|
|
|
|
def __str__(self):
|
|
# si c’est un nombre
|
|
if isinstance(self.valeur, int):
|
|
return str(self.valeur)
|
|
|
|
# sinon on affiche avec parenthèses
|
|
return "(" + str(self.gauche) + self.valeur + str(self.droite) + ")"
|
|
|
|
def npi2tree(liste):
|
|
pile = []
|
|
|
|
for element in liste:
|
|
if element == '+' or element == '*':
|
|
droite = pile.pop()
|
|
gauche = pile.pop()
|
|
|
|
exp = Expression(element, gauche, droite)
|
|
|
|
pile.append(exp)
|
|
|
|
else:
|
|
|
|
nombre = int(element)
|
|
pile.append(Expression(nombre, None, None))
|
|
|
|
return pile.pop()
|
|
|
|
def ajouter(valeur):
|
|
entree.insert(tk.END, valeur)
|
|
|
|
def effacer():
|
|
entree.delete(0, tk.END)
|
|
label_resultat.config(text="Résultat :")
|
|
|
|
def calculer():
|
|
try:
|
|
expression = entree.get()
|
|
liste = expression.split()
|
|
arbre = npi2tree(liste)
|
|
resultat = arbre.evalue()
|
|
label_resultat.config(text="Résultat : " + str(resultat))
|
|
except:
|
|
label_resultat.config(text="Erreur")
|
|
|
|
|
|
fenetre = tk.Tk()
|
|
fenetre.title("Calculatrice NPI")
|
|
fenetre.geometry("320x450")
|
|
fenetre.resizable(False, False)
|
|
|
|
entree = tk.Entry(fenetre, font=("Arial", 16), justify="right")
|
|
entree.pack(padx=10, pady=10, fill="x")
|
|
|
|
frame = tk.Frame(fenetre)
|
|
frame.pack()
|
|
|
|
# boutons chiffres 1 à 9
|
|
for i in range(1, 10):
|
|
tk.Button(frame,
|
|
text=str(i),
|
|
width=5,
|
|
height=2,
|
|
command=lambda x=str(i): ajouter(x)
|
|
).grid(row=(i-1)//3, column=(i-1)%3, padx=5, pady=5)
|
|
|
|
# bouton 0
|
|
tk.Button(frame,
|
|
text="0",
|
|
width=5,
|
|
height=2,
|
|
command=lambda: ajouter("0")
|
|
).grid(row=3, column=1, padx=5, pady=5)
|
|
|
|
tk.Button(frame, text="+", width=5, height=2,
|
|
command=lambda: ajouter(" + ")).grid(row=4, column=0, padx=5, pady=5)
|
|
|
|
tk.Button(frame, text="*", width=5, height=2,
|
|
command=lambda: ajouter(" * ")).grid(row=4, column=1, padx=5, pady=5)
|
|
|
|
tk.Button(frame, text="Espace", width=5, height=2,
|
|
command=lambda: ajouter(" ")).grid(row=4, column=2, padx=5, pady=5)
|
|
|
|
# bouton clear
|
|
tk.Button(frame, text="C", width=5, height=2,
|
|
command=effacer).grid(row=5, column=0, padx=5, pady=5)
|
|
|
|
# bouton égal
|
|
tk.Button(frame, text="=", width=12, height=2,
|
|
command=calculer).grid(row=5, column=1, columnspan=2, padx=5, pady=5)
|
|
|
|
# resultat
|
|
label_resultat = tk.Label(fenetre, text="Résultat :", font=("Arial", 14))
|
|
label_resultat.pack(pady=20)
|
|
|
|
fenetre.mainloop()
|
|
|
|
#exp = "6 4 3 + *"
|
|
#calcule = (npi2tree(exp))
|
|
#print(calcule)
|
|
#print(calcule.evalue())
|