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.

34 lines
1.1 KiB

from Pile import Pile_lst
import tkinter as tk
class Expression:
def __init__(self, valeur, gauche, droite):
self.valeur = valeur
self.gauche = gauche
self.droite = droite
def evalue(self):
if self.valeur == '+':
return self.gauche.evalue() + self.droite.evalue()
if self.valeur == '*':
return self.gauche.evalue() * self.droite.evalue()
else:
return self.valeur
def __str__(self):
if self.valeur != '+' and self.valeur != '*':
return self.valeur
if self.valeur == '+' or self.valeur == '*':
return "(" + str(self.gauche.__str__()) + str(self.valeur) + str(self.droite.__str__()) + ")"
def npi2tree(exp):
pile_npi = Pile_lst()
lst_npi = exp.split()
for element in lst_npi:
if element == '+' or element == '*':
exp_npi = Expression(element, pile_npi.depiler(), pile_npi.depiler())
pile_npi.empiler(exp_npi)
else:
pile_npi.empiler(int(element))
return pile_npi.sommet()