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