Render ~work with letters still some bugs

This commit is contained in:
lafrite 2014-01-17 14:57:45 +01:00
parent 65c05a35b5
commit 8a41031fa8
2 changed files with 27 additions and 6 deletions

View File

@ -132,6 +132,12 @@ class Expression(object):
else:
tokens.append(int(character))
elif character.isalpha():
if str(tokens[-1]).isalpha():
tokens[-1] += character
else:
tokens.append(character)
elif character in "+-*/)":
tokens.append(character)
@ -143,7 +149,6 @@ class Expression(object):
elif character != " ":
raise ValueError("{} is an unvalid character".format(character))
print(tokens[1:])
return tokens[1:]
# ---------------------
@ -292,7 +297,9 @@ class Expression(object):
:returns: True if the expression can be a number and false otherwise
"""
return type(exp) == int or type(exp) == Fraction
return type(exp) == int or \
type(exp) == Fraction or \
exp.isalpha()
@staticmethod
def isOperator(exp):
@ -360,6 +367,15 @@ if __name__ == '__main__':
exp="-2*4(12 + 1)(3-12)"
test(exp)
exp="-2*a(12 + 1)(3-12)"
e = Expression(exp)
print(e)
# TODO: The next one doesn't work |ven. janv. 17 14:56:58 CET 2014
#exp="-2*(-a)(12 + 1)(3-12)"
#e = Expression(exp)
#print(e)
## Can't handle it yet!!
#exp="-(-2)"
#test(exp)

View File

@ -16,7 +16,7 @@ class Render(object):
PRIORITY = {"*" : 3, "/": 3, "+": 2, "-":2, "(": 1}
def __init__(self, op_infix = {}, op_postfix = {}, other = {}, join = " ", type_render = {int: str, Fraction: str}):
def __init__(self, op_infix = {}, op_postfix = {}, other = {}, join = " ", type_render = {int: str, Fraction: str, str: str}):
"""Initiate the render
@param op_infix: the dictionnary of infix operator with how they have to be render
@ -107,7 +107,9 @@ class Render(object):
:param posi: "after"(default) if the operande will be after the operator, "before" othewise
:returns: bollean
"""
if self.isNumber(operande) and operande < 0:
if self.isNumber(operande) \
and type(operande) != str \
and operande < 0:
return 1
elif not self.isNumber(operande):
# Si c'est une grande expression ou un chiffre négatif
@ -159,7 +161,10 @@ class Render(object):
:returns: True if the expression can be a number and false otherwise
"""
return type(exp) == int or type(exp) == Fraction
return type(exp) == int or \
type(exp) == Fraction or \
(type(exp) == str and exp.isalpha())
#return type(exp) == int or type(exp) == Fraction
def isOperator(self, exp):
"""Check if the expression is in self.operators
@ -209,7 +214,7 @@ def texFrac(frac):
tex_infix = {"+": " + ", "-": " - ", "*": " \\times "}
tex_postfix = {"/": texSlash}
tex_other = {"(": "(", ")": ")"}
tex_type_render = {int: str, Fraction: texFrac}
tex_type_render = {int: str, Fraction: texFrac, str: str}
tex_render = Render(tex_infix, tex_postfix, tex_other, type_render = tex_type_render)