Browse Source

ajout méthode __str__

master
FORESTIER Alexi 10 months ago
parent
commit
a58ff9ad77
  1. 27
      Calc.py

27
Calc.py

@ -14,8 +14,22 @@ class Expression:
return self.valeur
def __str__(self):
if self.gauche is None and self.droit is None:
return str(self.valeur)
expr = ""
if self.gauche is not None:
if self.gauche.valeur in ['+', '*']:
expr += "(" + str(self.gauche) + ")"
else:
expr += str(self.gauche)
expr += " " + str(self.valeur) + " "
if self.droit is not None:
if self.droit.valeur in ['+', '*']:
expr += "(" + str(self.droit) + ")"
else:
expr += str(self.droit)
return expr
pass
def npi2tree(ch):
ch = ch.split()
@ -30,20 +44,9 @@ def npi2tree(ch):
def dfs_in(a):
if a == None:
return
dfs_in(a.gauche)
print(a.valeur)
dfs_in(a.droit)
ch1 = '7 4 3 * +'
exp = npi2tree(ch1)
exp.evalue()

Loading…
Cancel
Save