Browse Source

shutting yard

master
alexandre.aboulin 2 years ago
parent
commit
c68c18779b
  1. 4
      main.py
  2. 38
      reader.py

4
main.py

@ -1,4 +1,4 @@
from reader import * from reader import *
cal = input("Entrez votre calcul en NPI : ") cal = str(input("Entrez votre calcul en NPI : "))
print(npi2tree(tokenize(cal)).evalue()) print(npi2tree(shutting_yard(tokenize(cal))).evalue())

38
reader.py

@ -2,7 +2,7 @@ from expression import Token, Expression
from lib.Pile import Pile_chaine as Pile from lib.Pile import Pile_chaine as Pile
from lib.File import File_chaine as File from lib.File import File_chaine as File
def tokenize(text): def tokenize(text: str) -> list:
buffer = "" buffer = ""
tokens = [] tokens = []
for char in text: for char in text:
@ -12,7 +12,6 @@ def tokenize(text):
else: else:
#PUSH #PUSH
if buffer != "": if buffer != "":
print("a")
tokens.append(Token(Token.NUMBER, buffer)) tokens.append(Token(Token.NUMBER, buffer))
buffer = "" buffer = ""
#SINGLE CHAR #SINGLE CHAR
@ -23,11 +22,9 @@ def tokenize(text):
tokens.append(Token(Token.OPERATOR, char)) tokens.append(Token(Token.OPERATOR, char))
if buffer != "": if buffer != "":
tokens.append(Token(Token.NUMBER, buffer)) tokens.append(Token(Token.NUMBER, buffer))
for token in tokens:
print(token.type, ":", token.val)
return tokens return tokens
def shutting_yard(tokens): def shutting_yard(tokens: list) -> File:
output = File() output = File()
opertator = Pile() opertator = Pile()
for token in tokens: for token in tokens:
@ -36,44 +33,39 @@ def shutting_yard(tokens):
elif token.type == Token.FUNCTION: elif token.type == Token.FUNCTION:
opertator.empiler(token) opertator.empiler(token)
elif token.type == Token.OPERATOR: elif token.type == Token.OPERATOR:
if not opertator.est_vide(): while (not opertator.est_vide() and opertator.sommet().val != "(") and (Token.getPrecedence(opertator.sommet().val) > Token.getPrecedence(token.val) or (Token.getPrecedence(token.val) == Token.getPrecedence(opertator.sommet().val) and token.val != "^")):
o2 = opertator.sommet() output.enfiler(opertator.depiler())
a = o2 != "("
print(o2.val)
b = Token.getPrecedence(o2.val) > Token.getPrecedence(token.val)
c = Token.getPrecedence(token.val) == Token.getPrecedence(o2.val)
while a and (b or (c)): #and d))
output.enfiler(opertator.depiler())
opertator.empiler(token) opertator.empiler(token)
elif token.val == "(": elif token.val == "(":
opertator.empiler(token) opertator.empiler(token)
elif token.val == ")": elif token.val == ")":
while opertator.sommet() != "(": while not opertator.est_vide() and opertator.sommet().val != "(":
assert not opertator.est_vide() assert not opertator.est_vide()
output.enfiler(opertator.depiler()) output.enfiler(opertator.depiler())
assert opertator.sommet() == "(" assert opertator.sommet().val == "("
opertator.depiler() opertator.depiler()
if opertator.sommet().type == Token.FUNCTION: if opertator.sommet().type == Token.FUNCTION:
output.enfiler(opertator.depiler()) output.enfiler(opertator.depiler())
while not opertator.est_vide(): while not opertator.est_vide():
assert opertator.sommet != "(" assert opertator.sommet().val != "("
output.enfiler(opertator.depiler()) output.enfiler(opertator.depiler())
return output return output
def npi2tree(tokens): def npi2tree(tokens: File) -> Expression:
pile = Pile() pile = Pile()
for token in tokens: while not tokens.est_vide():
if token in ["+", "-", "*", "/"]: token = tokens.defiler()
exp = Expression(token, pile.depiler(), pile.depiler()) if token.val in ["+", "-", "*", "/"]:
exp = Expression(token.val, pile.depiler(), pile.depiler())
pile.empiler(exp) pile.empiler(exp)
else: else:
pile.empiler(Expression(token)) pile.empiler(Expression(token.val))
return pile.depiler() return pile.depiler()
if __name__ == "__main__": if __name__ == "__main__":
print(shutting_yard(tokenize("2+44(4+21)"))) a = npi2tree(shutting_yard(tokenize("2+44*(4+21)"))).evalue()
print(a)
#print(npi2tree(tokenize("6 4 3 + *"))) #print(npi2tree(tokenize("6 4 3 + *")))
Loading…
Cancel
Save