Kalyax
2 years ago
5 changed files with 87 additions and 172 deletions
@ -1,113 +0,0 @@ |
|||
import re |
|||
|
|||
class Expression: |
|||
|
|||
def __init__(self, val=None, gauche=None, droite=None): |
|||
if Token.isNumber(val) and (gauche != None or droite != None): |
|||
raise AttributeError("gauche et droite ne peuvent pas exister si val est un nombre") |
|||
self.val = val |
|||
self.gauche = gauche |
|||
self.droite = droite |
|||
|
|||
def evalue(self): |
|||
if self.val == Token.OPERAND_ADDITION: |
|||
return self.gauche.evalue() + self.droite.evalue() |
|||
elif self.val == Token.OPERAND_MULTIPLICATION: |
|||
return self.gauche.evalue() * self.droite.evalue() |
|||
else: |
|||
return int(self.val) |
|||
|
|||
def __str__(self): |
|||
if Token.isNumber(self.val): |
|||
return str(self.val) |
|||
return "(" + str(self.gauche.__str__()) + str(self.val) + str(self.droite.__str__()) + ")" |
|||
|
|||
|
|||
class Token: |
|||
ADDITION = "+" |
|||
MULTIPLICATION = "*" |
|||
PARENTHESE_OUVERTE = "(" |
|||
PARENTHESE_FERME = ")" |
|||
|
|||
NUMBER = re.compile(r'(?:\d+)?.\d+') |
|||
|
|||
@staticmethod |
|||
def isNumber(n): |
|||
return Token.NUMBER.match(str(n)) is not None |
|||
|
|||
def tokenize(text): |
|||
buffer = "" |
|||
tokens = [] |
|||
for char in text: |
|||
if Token.isNumber(char): |
|||
buffer += char |
|||
continue |
|||
elif char == " ": |
|||
continue |
|||
|
|||
if buffer != "": |
|||
tokens.append(buffer) |
|||
buffer = "" |
|||
tokens.append(char) |
|||
if buffer != "": |
|||
tokens.append(buffer) |
|||
return tokens |
|||
|
|||
def arbre(tokens): #-> Expression |
|||
print(tokens) |
|||
current = None |
|||
waiting_before_exp = None |
|||
waiting_for_after_exp = None |
|||
|
|||
i = 0 |
|||
while i < len(tokens): |
|||
if tokens[i] == "(": |
|||
j = i + 1 |
|||
innerBrackets = 1 |
|||
innerTokens = [] |
|||
while tokens[j] != ")" and innerBrackets != 0: |
|||
if tokens[j] == "(": |
|||
innerBrackets += 1 |
|||
elif tokens[j] == ")": |
|||
innerBrackets -= 1 |
|||
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_before_exp = waiting_for_after_exp |
|||
waiting_for_after_exp = None |
|||
else: |
|||
waiting_before_exp = exp |
|||
|
|||
elif tokens[i] == "*" or 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] |
|||
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() |
|||
|
|||
i += 1 |
|||
|
|||
return waiting_for_after_exp |
|||
|
|||
|
|||
|
|||
if __name__ == "__main__": |
|||
print(arbre(tokenize("(2*8)+(4+7)"))) |
@ -1,4 +1,9 @@ |
|||
from reader import * |
|||
from misc.parser import * |
|||
from misc.tokenizer import * |
|||
|
|||
cal = str(input("Entrez votre calcul en NPI : ")) |
|||
print(npi2tree(shutting_yard(tokenize(cal))).evalue()) |
|||
while True: |
|||
calcul = str(input("Entrez votre calcul en NPI : ")) |
|||
tokens = tokenize(calcul) |
|||
npi = shutting_yard(tokens) |
|||
expression = npi2tree(npi) |
|||
print(expression.evalue()) |
@ -0,0 +1,27 @@ |
|||
from misc.expression import Token |
|||
|
|||
def tokenize(text: str) -> list: |
|||
buffer_num = "" |
|||
buffer_func = "" |
|||
tokens = [] |
|||
for char in text: |
|||
if Token.isChar(char): |
|||
buffer_func += char |
|||
elif Token.isNumber(char) or char == ".": |
|||
buffer_num += char |
|||
else: |
|||
if buffer_num != "": |
|||
tokens.append(Token(Token.NUMBER, buffer_num)) |
|||
buffer_num = "" |
|||
if buffer_func != "": |
|||
tokens.append(Token(Token.FUNCTION, buffer_func)) |
|||
buffer_func = "" |
|||
if char == "(" or char == ")": |
|||
tokens.append(Token(Token.PARENTHESE, char)) |
|||
elif char != " ": |
|||
tokens.append(Token(Token.OPERATOR, char)) |
|||
if buffer_num != "": |
|||
tokens.append(Token(Token.NUMBER, buffer_num)) |
|||
if buffer_func != "": |
|||
tokens.append(Token(Token.FUNCTION, buffer_func)) |
|||
return tokens |
Loading…
Reference in new issue