solve final bug! :DDD
This commit is contained in:
parent
1b2d778ff6
commit
e0fb4da8ef
@ -5,16 +5,19 @@ from .generic import Stack, isOperator, isNumber, isPolynom
|
||||
from .operator import op
|
||||
from decimal import Decimal
|
||||
|
||||
import logging
|
||||
#logging.basicConfig(filename='str2tokens_debug.log',level=logging.DEBUG)
|
||||
|
||||
|
||||
def str2tokens(exp):
|
||||
""" Parse the string into tokens then turn it into postfix form
|
||||
|
||||
>>> str2tokens('2+3*4')
|
||||
[2, 3, 4, '*', '+']
|
||||
[2, 3, 4, *, +]
|
||||
>>> str2tokens('2*3+4')
|
||||
[2, 3, '*', 4, '+']
|
||||
[2, 3, *, 4, +]
|
||||
>>> str2tokens('2x+4')
|
||||
[2, < <class 'pymath.calculus.polynom.Polynom'> [0, 1]>, '*', 4, '+']
|
||||
[2, < <class 'pymath.calculus.polynom.Polynom'> [0, 1]>, *, 4, +]
|
||||
"""
|
||||
in_tokens = str2in_tokens(exp)
|
||||
post_tokens = in2post_fix(in_tokens)
|
||||
@ -104,12 +107,13 @@ def in2post_fix(infix_tokens):
|
||||
@return: the corresponding postfix list of tokens.
|
||||
|
||||
>>> in2post_fix(['(', 2, '+', 5, '-', 1, ')', '/', '(', 3, '*', 4, ')'])
|
||||
[2, 5, 1, '-', '+', 3, 4, '*', '/']
|
||||
[2, 5, 1, -, +, 3, 4, *, /]
|
||||
>>> in2post_fix(['-', '(', '-', 2, ')'])
|
||||
[2, '-', '-']
|
||||
[2, -, -]
|
||||
>>> in2post_fix(['-', '(', '-', 2, '+', 3, '*', 4, ')'])
|
||||
[2, '-', 3, 4, '*', '+', '-']
|
||||
[2, -, 3, 4, *, +, -]
|
||||
"""
|
||||
logging.debug("New start with {}".format(infix_tokens))
|
||||
# Stack where operator will be stocked
|
||||
opStack = Stack()
|
||||
# final postfix list of tokens
|
||||
@ -120,11 +124,10 @@ def in2post_fix(infix_tokens):
|
||||
|
||||
for (pos_token, token) in enumerate(infix_tokens):
|
||||
|
||||
# Pour voir ce qu'il se passe dans cette procédure
|
||||
#print(str(postfix_tokens), " | ", str(opStack), " | ", str(infix_tokens[(pos_token+1):]), " | ", str(arity_Stack))
|
||||
logging.debug(str(postfix_tokens)+ " | "+ str(opStack)+ " | "+ str(infix_tokens[(pos_token+1):])+ " | "+ str(arity_Stack))
|
||||
if token == ")":
|
||||
next_op = opStack.pop()
|
||||
while next_op != "(":
|
||||
while next_op != op.par:
|
||||
postfix_tokens.append(next_op)
|
||||
next_op = opStack.pop()
|
||||
|
||||
@ -136,7 +139,7 @@ def in2post_fix(infix_tokens):
|
||||
|
||||
elif op.can_be_operator(token):
|
||||
if token == "(":
|
||||
opStack.push(op.get_op(token))
|
||||
opStack.push(op.get_op(token,0))
|
||||
# Set next arity counter
|
||||
arity_Stack.push(0)
|
||||
else:
|
||||
@ -151,27 +154,26 @@ def in2post_fix(infix_tokens):
|
||||
postfix_tokens.append(next_op)
|
||||
|
||||
opStack.push(token_op)
|
||||
#print("--", token, " -> ", str(arity + 1))
|
||||
logging.debug("--"+ token+ " -> "+ str(arity + 1))
|
||||
else:
|
||||
postfix_tokens.append(token)
|
||||
arity = arity_Stack.pop()
|
||||
arity_Stack.push(arity + 1)
|
||||
|
||||
# Pour voir ce qu'il se passe dans cette procédure
|
||||
#print(str(postfix_tokens), " | ", str(opStack), " | ", str(infix_tokens[(pos_token+1):]), " | ", str(arity_Stack))
|
||||
logging.debug(str(postfix_tokens)+ " | "+ str(opStack)+ " | "+ str(infix_tokens[(pos_token+1):])+ " | "+ str(arity_Stack))
|
||||
|
||||
while not opStack.isEmpty():
|
||||
next_op = opStack.pop()
|
||||
postfix_tokens.append(next_op)
|
||||
|
||||
# Pour voir ce qu'il se passe dans cette procédure
|
||||
#print(str(postfix_tokens), " | ", str(opStack), " | ", str(infix_tokens[(pos_token+1):]), " | ", str(arity_Stack))
|
||||
logging.debug(str(postfix_tokens)+ " | "+ str(opStack)+ " | "+ str(infix_tokens[(pos_token+1):])+ " | "+ str(arity_Stack))
|
||||
|
||||
if arity_Stack.peek() != 1:
|
||||
raise ValueError(
|
||||
"Unvalid expression. The arity Stack is ",
|
||||
str(arity_Stack))
|
||||
|
||||
logging.debug("Fini!")
|
||||
return postfix_tokens
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user