1 changed files with 120 additions and 0 deletions
@ -0,0 +1,120 @@ |
|||||
|
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()) |
||||
Loading…
Reference in new issue