|
@ -14,9 +14,23 @@ class Expression: |
|
|
return self.valeur |
|
|
return self.valeur |
|
|
|
|
|
|
|
|
def __str__(self): |
|
|
def __str__(self): |
|
|
|
|
|
if self.gauche is None and self.droit is None: |
|
|
pass |
|
|
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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def npi2tree(ch): |
|
|
def npi2tree(ch): |
|
|
ch = ch.split() |
|
|
ch = ch.split() |
|
|
Expr = Pile_chaine() |
|
|
Expr = Pile_chaine() |
|
@ -29,21 +43,10 @@ def npi2tree(ch): |
|
|
return Expr.sommet() |
|
|
return Expr.sommet() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def dfs_in(a): |
|
|
|
|
|
if a == None: |
|
|
|
|
|
return |
|
|
|
|
|
dfs_in(a.gauche) |
|
|
|
|
|
print(a.valeur) |
|
|
|
|
|
dfs_in(a.droit) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ch1 = '7 4 3 * +' |
|
|
ch1 = '7 4 3 * +' |
|
|
exp = npi2tree(ch1) |
|
|
exp = npi2tree(ch1) |
|
|
|
|
|
|
|
|
exp.evalue() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|