Stockfish (4000 ELO) mais pour les maths Veuillez admirer ma belle calculatrice
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.
 

51 lines
1.2 KiB

from Pile import Pile_chaine
class Expression:
def __init__(self, valeur, gauche, droit):
self.valeur = valeur
self.gauche = gauche
self.droit = droit
def evalue(self):
dfs_post(self)
print(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
def dfs_in(a, expstr):
if a == None:
return expstr
expstr = dfs_in(a.gauche, expstr)
expstr = expstr + str(a.valeur)
expstr = dfs_in(a.droit, expstr)
return expstr
def npi2tree(exptext):
exptext = exptext.split()
expression = Pile_chaine()
for element in exptext:
if element in ("+", "*"):
droit = expression.depiler()
gauche = expression.depiler()
expression.empiler(Expression(element, gauche, droit))
else:
expression.empiler(Expression(int(element), None, None))
return expression.sommet()
exp = "6 4 3 * +"
exp = npi2tree(exp)
print(exp)
exp.evalue()