4 changed files with 160 additions and 59 deletions
@ -1,39 +1,55 @@ |
|||||
from tkinter import * |
from tkinter import * |
||||
|
from modules.calculette import Calculette |
||||
|
|
||||
fenetre = Tk() |
|
||||
|
|
||||
label = Label(fenetre, text="Calculatrice") |
|
||||
label.grid(row=0, column=0) |
|
||||
|
|
||||
# Creating the buttons |
|
||||
bouton1 = Button(fenetre, text="1") |
|
||||
bouton1.grid(row=1, column=0) |
|
||||
|
|
||||
bouton2 = Button(fenetre, text="2") |
|
||||
bouton2.grid(row=1, column=1) |
|
||||
|
|
||||
bouton3 = Button(fenetre, text="3") |
|
||||
bouton3.grid(row=1, column=2) |
|
||||
|
|
||||
bouton4 = Button(fenetre, text="4") |
def ajouter_au_calcul(symbol): |
||||
bouton4.grid(row=2, column=0) |
'''Ajoute un symbole à la saisie''' |
||||
|
current_text = calc.get() |
||||
|
calc.set(current_text + symbol) |
||||
|
|
||||
bouton5 = Button(fenetre, text="5") |
def calculer_expression(): |
||||
bouton5.grid(row=2, column=1) |
'''Calcule l'expression saisie''' |
||||
|
expression = calc.get() |
||||
|
resultat = calculatrice.calculer(expression) |
||||
|
result_label.config(text="Résultat: " + str(resultat)) |
||||
|
|
||||
bouton6 = Button(fenetre, text="6") |
def effacer(): |
||||
bouton6.grid(row=2, column=2) |
'''Efface la saisie''' |
||||
|
calc.set("") |
||||
|
|
||||
bouton7 = Button(fenetre, text="7") |
fenetre = Tk() |
||||
bouton7.grid(row=3, column=0) |
fenetre.geometry("350x300") |
||||
|
fenetre.title("Calculatrice") |
||||
bouton8 = Button(fenetre, text="8") |
|
||||
bouton8.grid(row=3, column=1) |
calculatrice = Calculette() |
||||
|
|
||||
bouton9 = Button(fenetre, text="9") |
calcLabel = Label(fenetre, text="Saisir le calcul") |
||||
bouton9.grid(row=3, column=2) |
calcLabel.pack() |
||||
|
|
||||
bouton0 = Button(fenetre, text="0") |
calc = StringVar() |
||||
bouton0.grid(row=4, column=1) |
calc.set("") |
||||
|
saisie = Entry(fenetre, textvariable=calc, width=40) |
||||
|
saisie.pack() |
||||
|
|
||||
|
buttons_frame = Frame(fenetre) |
||||
|
buttons_frame.pack() |
||||
|
#matrices des boutons |
||||
|
buttons = [ |
||||
|
('7', 1, 0), ('8', 1, 1), ('9', 1, 2), ('/', 1, 3), |
||||
|
('4', 2, 0), ('5', 2, 1), ('6', 2, 2), ('*', 2, 3), |
||||
|
('1', 3, 0), ('2', 3, 1), ('3', 3, 2), ('-', 3, 3), |
||||
|
('0', 4, 0), ('.', 4, 1), ('+', 4, 2), ('=', 4, 3), |
||||
|
] |
||||
|
|
||||
|
for (text, row, col) in buttons: |
||||
|
if text == '=': |
||||
|
Button(buttons_frame, text=text, width=10, height=3, command=calculer_expression).grid(row=row, column=col) |
||||
|
else: |
||||
|
Button(buttons_frame, text=text, width=10, height=3, command=lambda t=text: ajouter_au_calcul(t)).grid(row=row, column=col) |
||||
|
|
||||
|
Button(fenetre, text="EFFACER", width=40, command=effacer).pack() |
||||
|
|
||||
|
result_label = Label(fenetre, text="") |
||||
|
result_label.pack() |
||||
|
|
||||
fenetre.mainloop() |
fenetre.mainloop() |
Binary file not shown.
@ -0,0 +1,76 @@ |
|||||
|
from File import File_chaine as File |
||||
|
from Pile import Pile_chaine as Pile |
||||
|
|
||||
|
|
||||
|
class Expression: |
||||
|
"""Classe représentant une expression arithmétique""" |
||||
|
def __init__(self, valeur, gauche=None, droite=None): |
||||
|
self.valeur = valeur |
||||
|
self.gauche = gauche |
||||
|
self.droite = droite |
||||
|
|
||||
|
def evalue(self): |
||||
|
"""Méthode permettant d'évaluer l'expression""" |
||||
|
|
||||
|
if type(self.valeur) == int: |
||||
|
return self.valeur |
||||
|
|
||||
|
if self.valeur == '+': |
||||
|
return self.gauche.evalue() + self.droite.evalue() |
||||
|
elif self.valeur == '*': |
||||
|
return self.gauche.evalue() * self.droite.evalue() |
||||
|
elif self.valeur == '/': |
||||
|
if self.droite.valeur == 0: |
||||
|
raise ValueError ("division par 0 impossible !! ") |
||||
|
return self.gauche.evalue() / self.droite.evalue() |
||||
|
elif self.valeur == '-': |
||||
|
return self.gauche.evalue() - self.droite.evalue() |
||||
|
|
||||
|
|
||||
|
def __str__(self): |
||||
|
"""Méthode permettant d'afficher l'expression""" |
||||
|
if type(self.valeur) == int: |
||||
|
return str(self.valeur) |
||||
|
if self.valeur == '+': |
||||
|
return "("+ str (self.gauche) + "+" + str (self.droite) + ")" |
||||
|
if self.valeur == '*': |
||||
|
return "("+ str (self.gauche) + "*" + str (self.droite) + ")" |
||||
|
if self.valeur == '/': |
||||
|
return "("+ str (self.gauche) + "/" + str (self.droite) + ")" |
||||
|
if self.valeur == '-': |
||||
|
return "("+ str (self.gauche) + "-" + str (self.droite) + ")" |
||||
|
|
||||
|
def __repr__(self): |
||||
|
"""Méthode permettant d'afficher l'expression""" |
||||
|
return str(self) |
||||
|
|
||||
|
|
||||
|
if __name__ =="__main__": |
||||
|
e = Expression('*',Expression(6, None, None),Expression('+',Expression(4, None, None),Expression(3, None, None))) |
||||
|
|
||||
|
def npi2tree(lst): |
||||
|
"""fonction npi2tree prenant en paramètre une liste correspondant l’expression en notation |
||||
|
polonaise inversée et renvoyant l’arbre (de type Expression) correspondant""" |
||||
|
exp = [] |
||||
|
for ele in lst : |
||||
|
if ele in ["+","-","/","*"]: |
||||
|
ex = Expression(ele, exp.pop(), exp.pop()) |
||||
|
exp.append(ex) |
||||
|
else: |
||||
|
exp.append(int(ele)) |
||||
|
return exp.pop() |
||||
|
|
||||
|
class Calculette: |
||||
|
"""Classe représentant une calculette""" |
||||
|
def __init__(self): |
||||
|
pass |
||||
|
|
||||
|
def calculer(self, expression): |
||||
|
"""Méthode permettant de calculer une expression""" |
||||
|
try: |
||||
|
return eval(expression) |
||||
|
except Exception as e: |
||||
|
return str(e) |
||||
|
|
||||
|
if __name__ == "__main__": |
||||
|
npi2tree(["3","4","+","6","*"]) |
Loading…
Reference in new issue