Browse Source

????????

master
Kalyax 2 years ago
parent
commit
d1248d271c
  1. 44
      infixe.py

44
infixe.py

@ -49,6 +49,8 @@ def tokenize(text):
tokens.append(buffer)
buffer = ""
tokens.append(char)
if buffer != "":
tokens.append(buffer)
return tokens
def arbre(tokens): #-> Expression
@ -59,7 +61,7 @@ def arbre(tokens): #-> Expression
##Brackets loop
i = 0
while i != len(tokens):
while i < len(tokens):
if tokens[i] == "(":
j = i + 1
innerBrackets = 1
@ -72,24 +74,54 @@ def arbre(tokens): #-> Expression
innerTokens.append(tokens[j])
j += 1
exp = arbre(innerTokens)
tokens = tokens[:i] + [" "] + tokens[j+1:]
#i -= j+1
if waiting_for_after_exp != None:
waiting_for_after_exp.droite == exp
waiting_for_after_exp.droite = exp
waiting_before_exp = waiting_for_after_exp
waiting_for_after_exp = None
else:
waiting_before_exp = exp
elif tokens[i] == "*":
exp = Expression("*")
if Token.isNumber(tokens[i-1]):
exp.gauche == tokens[i-1]
exp.gauche = tokens[i-1]
if Token.isNumber(tokens[i+1]):
exp.droite = tokens[i+1]
if Token.isNumber(tokens[i-1]) and Token.isNumber(tokens[i+1]):
return exp
if tokens[i-1] == ")":
exp.gauche = waiting_before_exp
waiting_before_exp = None
elif tokens[i+1] == "(":
waiting_for_after_exp = exp
print(exp)
elif tokens[i-1] == " ":
exp.gauche = waiting_before_exp
waiting_for_after_exp = exp
else:
raise Exception()
elif tokens[i] == "+":
exp = Expression("+")
if Token.isNumber(tokens[i-1]):
exp.gauche = tokens[i-1]
if Token.isNumber(tokens[i+1]):
exp.droite == tokens[i+1]
exp.droite = tokens[i+1]
if Token.isNumber(tokens[i-1]) and Token.isNumber(tokens[i+1]):
return exp
if tokens[i-1] == ")":
exp.gauche == waiting_before_exp
exp.gauche = waiting_before_exp
waiting_before_exp = None
elif tokens[i+1] == "(":
waiting_for_after_exp = exp
print(exp)
elif tokens[i-1] == " ":
exp.gauche = waiting_before_exp
waiting_for_after_exp = exp
else:
raise Exception()
@ -115,4 +147,4 @@ def arbre(tokens): #-> Expression
if __name__ == "__main__":
print(arbre(tokenize("4*(7*4)")))
print(arbre(tokenize("(2*8)*4+(7*4)*7")))

Loading…
Cancel
Save