new class for operator and include it into expression.in2post_fix
This commit is contained in:
parent
eb1088d31f
commit
62549d2e10
@ -1,7 +1,7 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
|
|
||||||
from .generic import Stack, flatten_list, expand_list
|
from .generic import Stack, flatten_list, expand_list, Operator
|
||||||
from .fraction import Fraction
|
from .fraction import Fraction
|
||||||
from .renders import txt, post2in_fix, tex
|
from .renders import txt, post2in_fix, tex
|
||||||
|
|
||||||
@ -248,24 +248,41 @@ class Expression(object):
|
|||||||
>>> Expression.in2post_fix(['(', 2, '+', 5, '-', 1, ')', '/', '(', 3, '*', 4, ')'])
|
>>> Expression.in2post_fix(['(', 2, '+', 5, '-', 1, ')', '/', '(', 3, '*', 4, ')'])
|
||||||
[2, 5, '+', 1, '-', 3, 4, '*', '/']
|
[2, 5, '+', 1, '-', 3, 4, '*', '/']
|
||||||
"""
|
"""
|
||||||
|
# Stack where operator will be stocked
|
||||||
opStack = Stack()
|
opStack = Stack()
|
||||||
|
# final postfix list of tokens
|
||||||
postfixList = []
|
postfixList = []
|
||||||
|
# Nbr of tokens to compute in postfixList
|
||||||
|
nbr_token_in_postfix = 0
|
||||||
|
|
||||||
for token in infix_tokens:
|
for (pos_token,token) in enumerate(infix_tokens):
|
||||||
if token == "(":
|
if token == "(":
|
||||||
opStack.push(token)
|
opStack.push(token)
|
||||||
|
nbr_token_in_postfix -= 1
|
||||||
elif token == ")":
|
elif token == ")":
|
||||||
topToken = opStack.pop()
|
op = opStack.pop()
|
||||||
while topToken != "(":
|
while op != "(":
|
||||||
postfixList.append(topToken)
|
postfixList.append(op)
|
||||||
topToken = opStack.pop()
|
nbr_token_in_postfix -= (op.arrity - 1)
|
||||||
|
op = opStack.pop()
|
||||||
|
|
||||||
|
nbr_token_in_postfix += 1
|
||||||
|
|
||||||
elif cls.isOperator(token):
|
elif cls.isOperator(token):
|
||||||
# On doit ajouter la condition == str sinon python ne veut pas tester l'appartenance à la chaine de caractère.
|
# On doit ajouter la condition == str sinon python ne veut pas tester l'appartenance à la chaine de caractère.
|
||||||
while (not opStack.isEmpty()) and (cls.PRIORITY[opStack.peek()] >= cls.PRIORITY[token]):
|
while (not opStack.isEmpty()) and (cls.PRIORITY[opStack.peek()] >= cls.PRIORITY[token]):
|
||||||
postfixList.append(opStack.pop())
|
op = opStack.pop()
|
||||||
opStack.push(token)
|
postfixList.append(op)
|
||||||
|
nbr_token_in_postfix -= (op.arrity - 1)
|
||||||
|
|
||||||
|
# la max est là dans le cas où l'expression commence par "("
|
||||||
|
opStack.push(Operator(token, max(1,nbr_token_in_postfix + 1)))
|
||||||
else:
|
else:
|
||||||
postfixList.append(token)
|
postfixList.append(token)
|
||||||
|
nbr_token_in_postfix += 1
|
||||||
|
|
||||||
|
## Pour voir ce qu'il se passe dans cette procédure
|
||||||
|
#print(str(postfixList), " | ", str(opStack), " | ", str(infix_tokens[(pos_token+1):]), " | ", str(nbr_token_in_postfix))
|
||||||
|
|
||||||
while not opStack.isEmpty():
|
while not opStack.isEmpty():
|
||||||
postfixList.append(opStack.pop())
|
postfixList.append(opStack.pop())
|
||||||
|
@ -57,6 +57,16 @@ class Stack(object):
|
|||||||
def __add__(self, addList):
|
def __add__(self, addList):
|
||||||
return self.items + addList
|
return self.items + addList
|
||||||
|
|
||||||
|
class Operator(str):
|
||||||
|
|
||||||
|
"""The operator class, is a string (representation of the operator) with its arrity (?!? - arrite)"""
|
||||||
|
|
||||||
|
def __new__(cls, operator, arrity = 2):
|
||||||
|
op = str.__new__(cls, operator)
|
||||||
|
op.arrity = arrity
|
||||||
|
return op
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def flatten_list(a, result=None):
|
def flatten_list(a, result=None):
|
||||||
"""Flattens a nested list.
|
"""Flattens a nested list.
|
||||||
|
Loading…
Reference in New Issue
Block a user