From a58ff9ad77f143c472a20f9c875b4415be6e0141 Mon Sep 17 00:00:00 2001 From: FORESTIER Alexi Date: Mon, 12 Feb 2024 15:33:18 +0100 Subject: [PATCH] =?UTF-8?q?ajout=20=20m=C3=A9thode=20=5F=5Fstr=5F=5F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Calc.py | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/Calc.py b/Calc.py index 1baec3e..24c2188 100644 --- a/Calc.py +++ b/Calc.py @@ -14,9 +14,23 @@ class Expression: return self.valeur def __str__(self): - - pass - + 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 + + def npi2tree(ch): ch = ch.split() Expr = Pile_chaine() @@ -29,21 +43,10 @@ def npi2tree(ch): 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 * +' exp = npi2tree(ch1) -exp.evalue()