Browse Source

shutting yard

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

4
main.py

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

36
reader.py

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