from Pile import Pile_chaine from tkinter import * class Expression: def __init__(self, valeur, gauche, droit): self.valeur = valeur self.gauche = gauche self.droit = droit def evalue(self): dfs_post(self) return self.valeur def __str__(self): expstr = dfs_in(self, "") return expstr def dfs_post(a): if a == None: return dfs_post(a.gauche) dfs_post(a.droit) if a.valeur == "+": a.valeur = a.gauche.valeur + a.droit.valeur if a.valeur == "*": a.valeur = a.gauche.valeur * a.droit.valeur if a.valeur == "-": a.valeur = a.gauche.valeur - a.droit.valeur if a.valeur == "/": a.valeur = a.gauche.valeur / a.droit.valeur if a.valeur == "^": a.valeur = a.gauche.valeur ** a.droit.valeur def dfs_in(a, expstr): if a == None: return expstr if a.gauche is not None or a.droit is not None: expstr += "(" expstr = dfs_in(a.gauche, expstr) expstr += str(a.valeur) expstr = dfs_in(a.droit, expstr) if a.gauche is not None or a.droit is not None: expstr += ")" return expstr def npi2tree(exptext): exptext = exptext.split() expression = Pile_chaine() for element in exptext: if element in ("+ * - / ^".split()): gauche = expression.depiler() droit = expression.depiler() expression.empiler(Expression(element, gauche, droit)) else: expression.empiler(Expression(int(element), None, None)) return expression.sommet() def insert(text): global txt, currentnumber if text in ("+ * - / ^".split()): txt = currentnumber + " " + txt + " " + text currentnumber = "" else: currentnumber = currentnumber + text lcdscreen.config(text = currentnumber + " " + txt) def cls(): global txt, currentnumber txt = "" currentnumber = "" lcdscreen.config(text = txt) def calc(): global txt, currentnumber txt = currentnumber + " " + txt result = npi2tree(txt) lcdscreen.config(text = (str(result), "=", result.evalue())) window = Tk() window.title("Stockfish mais pour les maths") window.geometry(f"300x400+50+50") canvas = Canvas(window, width = 300, height = 400, bg = "#abcdef") canvas.pack() lcdscreen = Label(window, width = 24) lcdscreen.place(x=5, y=5) touches = "0 1 2 3 4 5 6 7 8 9 + * - / ^".split() txt = "" currentnumber = "" for touche in touches: button = Button(window, text = touche, command = lambda touche=touche: insert(touche), bg = "#fedcba") button.place(x=touches.index(touche) // 3 * 50 + 10, y = touches.index(touche) % 3 * 50 + 100) clsbutton = Button(window, text = "Reset Stockfish but for maths.py's current processus", command = cls, bg = "#abdcba", font=("Stockfish font, but it doesn't exist :(", 7)) clsbutton.place(x=10, y=250) calcbutton = Button(window, text = "Finalize Stockfish processus", command = calc, bg = "#f42205") calcbutton.place(x=10, y=290) logo = PhotoImage(file="stockfish mais pour les maths logo.png") logo = logo.subsample(10) canvas.create_image(200, 0, anchor=NW, image=logo) canvas.pack() window.mainloop()