Browse Source

fini

master
Laura 1 month ago
parent
commit
0cbb8d9cb6
  1. 60
      intergraph.py
  2. 83
      modules/calculette1.py

60
intergraph.py

@ -2,73 +2,65 @@ from tkinter import *
from modules.calculette import Calculette
from tkinter import *
from modules.calculette import Calculette
def ajouter_au_calcul(symbol):
'''Ajoute un symbole à la saisie'''
current_text = calc.get()
calc.set(current_text + symbol)
def ajouter_espace():
current_text = calc.get()
calc.set(current_text + " ")
def calculer_expression():
'''Calcule l'expression saisie'''
expression = calc.get()
expression = calc.get().strip()
resultat = calculatrice.calculer(expression)
result_label.config(text="Résultat: " + str(resultat))
def effacer():
'''Efface la saisie'''
calc.set("")
fenetre = Tk()
fenetre.geometry("350x300")
fenetre.title("Calculatrice")
fenetre.geometry("400x500")
fenetre.title("Calculatrice NPI")
calculatrice = Calculette()
calcLabel = Label(fenetre, text="Saisir le calcul")
from calculette import *
fenetre=Tk()
fenetre.title("Calculatrice")
calcLabel = Label(fenetre, text="saisir le calcul")
calcLabel.pack()
calcLabel = Label(fenetre, text="Expression en Notation Polonaise Inversée")
calcLabel.pack(pady=10)
calc = StringVar()
calc.set("")
saisie = Entry(fenetre, textvariable=calc, width=40)
saisie.pack()
saisie.pack(pady=10)
buttons_frame = Frame(fenetre)
buttons_frame.pack()
#matrices des boutons
buttons_frame.pack(pady=10)
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),
(' ', 5, 0) # Adding space button
]
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)
Button(buttons_frame, text=text, width=10, height=3, command=calculer_expression).grid(row=row, column=col, padx=2, pady=2)
elif text == ' ':
Button(buttons_frame, text="SPACE", width=10, height=3, command=ajouter_espace).grid(row=row, column=col, padx=2, pady=2)
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()
Button(buttons_frame, text=text, width=10, height=3, command=lambda t=text: ajouter_au_calcul(t)).grid(row=row, column=col, padx=2, pady=2)
result_label = Label(fenetre, text="")
result_label.pack()
Button(fenetre, text="EFFACER", width=40, command=effacer).pack(pady=10)
fenetre.mainloop()
saisie = Entry(fenetre, textvariable=calc, width=10)
saisie.pack()
result_label = Label(fenetre, text="", font=('Arial', 14))
result_label.pack(pady=10)
bouton1 = Button(fenetre, text="CALCULER", width=8)
bouton1.pack()
# Exemple d'utilisation
exemple = Label(fenetre, text="Exemple: '3 4 + 6 *' calcule (3+4)*6")
exemple.pack()
fenetre.mainloop()

83
modules/calculette1.py

@ -1,76 +1,49 @@
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:
if isinstance(self.valeur, (int, float)):
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 larbre (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()
operateurs = {
'+': lambda x, y: x + y,
'*': lambda x, y: x * y,
'/': lambda x, y: x / y if y != 0 else float('inf'),
'-': lambda x, y: x - y
}
return operateurs[self.valeur](self.gauche.evalue(), self.droite.evalue())
class Calculette:
"""Classe représentant une calculette"""
def __init__(self):
pass
def npi2tree(self, lst):
pile = []
for element in lst:
if element in ['+', '-', '*', '/']:
droite = pile.pop()
gauche = pile.pop()
pile.append(Expression(element, gauche, droite))
else:
try:
valeur = int(element) if element.isdigit() else float(element)
pile.append(Expression(valeur))
except ValueError:
return None
return pile[0]
def calculer(self, expression):
"""Méthode permettant de calculer une expression"""
try:
return eval(expression)
elements = expression.split()
arbre = self.npi2tree(elements)
if arbre:
return arbre.evalue()
return "Expression invalide"
except Exception as e:
return str(e)
if __name__ == "__main__":
npi2tree(["3","4","+","6","*"])

Loading…
Cancel
Save