From 30f94111811a76d937835b5a46a8b084906083b4 Mon Sep 17 00:00:00 2001 From: arthur Date: Fri, 27 Jan 2023 07:03:45 +0100 Subject: [PATCH] la partie console fonctionne --- .vscode/settings.json | 3 + __pycache__/data_structure.cpython-310.pyc | Bin 0 -> 10803 bytes data_structure.py | 12 ++- main.py | 83 ++++++++++++++++++++- 4 files changed, 93 insertions(+), 5 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 __pycache__/data_structure.cpython-310.pyc diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..a6735e5 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.analysis.typeCheckingMode": "off" +} \ No newline at end of file diff --git a/__pycache__/data_structure.cpython-310.pyc b/__pycache__/data_structure.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..1d334846fdf842fdc074718d8fc12cd9ac1c9586 GIT binary patch literal 10803 zcmb_iO^_VPRj$A8>FJs2`PWEZt<`I==S7E9B*cy!@F1i_U9qXQ8PM8JUq2M!$OM!*3g2#5+IIKTm2QS3!Qtogo|Rn=8J z)gBQ+cT{#}WoG4jFW>v}%dGCs%~dUYzWnr`T2J4!tp8vk`*BgYiYt8Eu`Fe6S-)f{ zTRBfHl%`Y#r3y+OO4F)}QWd2VN;7H}rCF59D9x!F zN;Q-q1m_7pQu-Q~zNzkYP&37)p`|jnh ze*>A#cdFH^&3^AeS6{C0H9P%kE9-sC!|%icU(}HatSuW}=qP*3RgQ9>T3ep-R0+TE zJd8ROO?|i7-renX`fs59oerKV#k#uK?*#STcGwH*tq0BHzvvtv1sg3oc=BPM!`;9Y zE+84&L(8{@4pIzz+d8rv>rPaD+}sWNI+_am?Z?edZ{3Z&Fxb7PJ#^6}BuAE>!q3Y) zH!k1369ziGyRG#n&AW}@lU{4L+gEp0v)83*x%*TKgA??e^fZ+F@~-=D=GAxACSReP`yYrc_Gw`^1;ALbL~ zUNGo;*3jC4m%(aw|1xg;Am}{qwu3tD1P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^?<{hJ{p35U8Qmot=W*AlF5hdx_Wt#M0ZsOcYXATM literal 0 HcmV?d00001 diff --git a/data_structure.py b/data_structure.py index a4c6ec2..13f4fa1 100644 --- a/data_structure.py +++ b/data_structure.py @@ -199,7 +199,7 @@ class File_lst: return s + "queue" -class Maillon: +class Maillon_db: """Un maillon d'une liste doublement chaînée.""" def __init__(self, precedent, valeur, suivant): self.valeur = valeur @@ -217,13 +217,21 @@ class File_chaine: self.__debut = self.__fin = None self.__taille = 0 + def __iter__(self): + return self + + def __next__(self): + if self.est_vide(): + raise StopIteration + return self.defiler() + def est_vide(self): """Indique si la file est vide.""" return self.__taille == 0 def enfiler(self, valeur): """Enfile l'élément valeur.""" - maillon = Maillon(self.__fin, valeur, None) + maillon = Maillon_db(self.__fin, valeur, None) if self.est_vide(): self.__debut = self.__fin = maillon else: diff --git a/main.py b/main.py index 8769fc3..6d98769 100644 --- a/main.py +++ b/main.py @@ -3,9 +3,86 @@ from data_structure import * class Expression: """manipule les expression sous forme d'arbre""" def __init__(self, val, fils_gauche, fils_droit) -> None: - self.Val = val + self.val = val self.fils_gauche = fils_gauche self.fils_droit = fils_droit - def evalue(self, val: float): - pass \ No newline at end of file + def __str__(self) -> str: + """renvoie l'expression sous forme infixé""" + if self.est_feuille(): + return str(self.val) + return '('+self.fils_gauche.__str__()+str(self.val)+self.fils_droit.__str__()+')' + + def est_feuille(self) -> bool: + """renvoie true si le noeud est une feuille""" + if self.fils_droit is None and self.fils_gauche is None: + return True + return False + + def evalue(self) -> float: + """renvoie le résultat de l'expression""" + if self.est_feuille(): + return float(self.val) + if self.val == '+': + return self.fils_gauche.evalue() + self.fils_droit.evalue() + if self.val == '*': + return self.fils_gauche.evalue() * self.fils_droit.evalue() + if self.val == '/': + return self.fils_gauche.evalue() / self.fils_droit.evalue() + if self.val == '^': + return self.fils_gauche.evalue() ** self.fils_droit.evalue() + if self.val == '-': + return self.fils_gauche.evalue() - self.fils_droit.evalue() + +def npi2tree(expr: list) -> Expression: + """renvoie l'arbre formé a partir de l'expression donnée""" + pile = Pile_chaine() + for val in expr: + if not val.isdigit(): + # on inverse pour avoir les nombres dans le bon ordre + nombre2, nombre1 = pile.depiler(), pile.depiler() + pile.empiler(Expression(val, nombre1, nombre2)) + else: + pile.empiler(Expression(int(val), None, None)) + return pile.sommet() + +def inf2npi(expr: list) -> list: + operator_stack = Pile_chaine() + operator_priority = { + '+': 1, + '-': 1, + '*': 2, + '/': 2, + '(': 0, + ')': 0 + } + output = [] + for val in expr: + if val.isdigit(): + output.append(val) + else: + if operator_stack.est_vide() or ( val == '(' or operator_priority[val] > operator_priority[operator_stack.sommet()]): + operator_stack.empiler(val) + else: + while not operator_stack.est_vide(): + if operator_stack.sommet() == '(': + operator_stack.depiler() + if operator_stack.est_vide(): # test si il y a un astérix avant la parenthèse + output.append('*') + elif operator_stack.sommet() != '*': + output.append('*') + else: + output.append(operator_stack.depiler()) + else: + output.append(operator_stack.depiler()) + if val != ')': + operator_stack.empiler(val) + while not operator_stack.est_vide(): + output.append(operator_stack.depiler()) + return output + +# [3, '-', 6, '*', 4, '+', 3] +exp = inf2npi(list('6*(4+3)')) +print(exp) + +print(npi2tree(exp)) \ No newline at end of file