parsing and rendering "^" done.Computing with integer too

This commit is contained in:
lafrite 2014-01-28 20:52:38 +01:00
parent 3b2176fb5c
commit 1e376787aa
4 changed files with 26 additions and 24 deletions

View File

@ -10,7 +10,7 @@ from formal import FormalExp
class Expression(object): class Expression(object):
"""A calculus expression. Today it can andle only expression with numbers later it will be able to manipulate unknown""" """A calculus expression. Today it can andle only expression with numbers later it will be able to manipulate unknown"""
PRIORITY = {"*" : 3, "/": 4, ":": 3, "+": 2, "-":2, "(": 1} PRIORITY = {"^": 5, "*" : 3, "/": 4, ":": 3, "+": 2, "-":2, "(": 1}
def __init__(self, exp): def __init__(self, exp):
""" Initiate the expression """ Initiate the expression
@ -156,7 +156,7 @@ class Expression(object):
else: else:
tokens.append(FormalExp(letter = character)) tokens.append(FormalExp(letter = character))
elif character in "+-*/):": elif character in "+-*/):^":
tokens.append(character) tokens.append(character)
elif character in "(": elif character in "(":
@ -308,7 +308,7 @@ class Expression(object):
operations = {"+": "__radd__", "-": "__rsub__", "*": "__rmul__"} operations = {"+": "__radd__", "-": "__rsub__", "*": "__rmul__"}
return getattr(op2,operations[op])(op1) return getattr(op2,operations[op])(op1)
else: else:
operations = {"+": "__add__", "-": "__sub__", "*": "__mul__"} operations = {"+": "__add__", "-": "__sub__", "*": "__mul__", "^": "__pow__"}
return getattr(op1,operations[op])(op2) return getattr(op1,operations[op])(op2)
@ -329,13 +329,13 @@ class Expression(object):
@staticmethod @staticmethod
def isOperator(exp): def isOperator(exp):
"""Check if the expression is an opération in "+-*/:" """Check if the expression is an opération in "+-*/:^"
:param exp: an expression :param exp: an expression
:returns: boolean :returns: boolean
""" """
return (type(exp) == str and exp in "+-*/:") return (type(exp) == str and exp in "+-*/:^")
def test(exp): def test(exp):
@ -348,7 +348,7 @@ def test(exp):
print("\n") print("\n")
if __name__ == '__main__': if __name__ == '__main__':
#exp = "1 + 3 * 5" #exp = "2 ^ 3 * 5"
#test(exp) #test(exp)
#exp = "2 * 3 * 3 * 5" #exp = "2 * 3 * 3 * 5"
@ -372,7 +372,7 @@ if __name__ == '__main__':
#exp = "2 + 5 * ( 3 - 4 )" #exp = "2 + 5 * ( 3 - 4 )"
#test(exp) #test(exp)
#exp = "( 2 + 5 ) * ( 3 - 4 )" #exp = "( 2 + 5 ) * ( 3 - 4 )^4"
#test(exp) #test(exp)
#exp = "( 2 + 5 ) * ( 3 * 4 )" #exp = "( 2 + 5 ) * ( 3 * 4 )"
@ -390,13 +390,13 @@ if __name__ == '__main__':
#exp="(-2+5)/(3*4)+1/12+5*5" #exp="(-2+5)/(3*4)+1/12+5*5"
#test(exp) #test(exp)
exp="-2*4(12 + 1)(3-12)" #exp="-2*4(12 + 1)(3-12)"
test(exp)
exp="-2+a+(12 + 1)(3-12) : 34a"
#test(exp) #test(exp)
e = Expression(exp)
print(e.render(render = tex_render)) #exp="-2+a+(12 + 1)(3-12) : 34a"
##test(exp)
#e = Expression(exp)
#print(e.render(render = tex_render))
#exp="-2*b+a(12 + 1)(3-12)" #exp="-2*b+a(12 + 1)(3-12)"
#test(exp) #test(exp)

View File

@ -123,7 +123,7 @@ if __name__ == '__main__':
#rdExp3 = RdExpression(form) #rdExp3 = RdExpression(form)
#desc_rdExp(rdExp3) #desc_rdExp(rdExp3)
form1 = "{a**2}y + {2*a*b}x + {b**2}" form1 = "{a**2}x^2 + {2*a*b}x + {b**2}"
cond1 = ["{a} != 0", "{b} != 0"] cond1 = ["{a} != 0", "{b} != 0"]
rdExp1 = RdExpression(form1, cond1) rdExp1 = RdExpression(form1, cond1)
desc_rdExp(rdExp1) desc_rdExp(rdExp1)

View File

@ -13,7 +13,7 @@ class Render(object):
Those three dictionnaries while define how a postfix expression will be transform into a string. Those three dictionnaries while define how a postfix expression will be transform into a string.
""" """
PRIORITY = {"*" : 3, "/": 3, ":": 3, "+": 2, "-":2, "(": 1} PRIORITY = {"^": 4,"*" : 3, "/": 3, ":": 3, "+": 2, "-":2, "(": 1}
def __init__(self, op_infix = {}, op_postfix = {}, other = {}, join = " ", type_render = {int: str, Fraction: str, FormalExp: str}): def __init__(self, op_infix = {}, op_postfix = {}, other = {}, join = " ", type_render = {int: str, Fraction: str, FormalExp: str}):
"""Initiate the render """Initiate the render
@ -107,12 +107,14 @@ class Render(object):
:param posi: "after"(default) if the operande will be after the operator, "before" othewise :param posi: "after"(default) if the operande will be after the operator, "before" othewise
:returns: bollean :returns: bollean
""" """
# Si l'operande est negatif
if self.isNumber(operande) \ if self.isNumber(operande) \
and operande < 0: and operande < 0:
return 1 return 1
# Si c'est un expression formelle
elif type(operande) == FormalExp: elif type(operande) == FormalExp:
if operator in ["*", "/"]: if operator in ["*", "/", "^"]:
if len(operande) > 1 \ if len(operande) > 1 \
or operande.master_coef() < 0: or operande.master_coef() < 0:
return 1 return 1
@ -124,8 +126,8 @@ class Render(object):
stand_alone = self.get_main_op(operande) stand_alone = self.get_main_op(operande)
# Si la priorité de l'operande est plus faible que celle de l'opérateur # Si la priorité de l'operande est plus faible que celle de l'opérateur
minor_priority = self.PRIORITY[self.get_main_op(operande)] < self.PRIORITY[operator] minor_priority = self.PRIORITY[self.get_main_op(operande)] < self.PRIORITY[operator]
# Si l'opérateur est -/ pour after ou juste / pour before # Si l'opérateur est - ou / pour after ou / ou ^ pour before
special = (operator in "-/" and posi == "after") or (operator in "/" and posi == "before") special = (operator in "-/" and posi == "after") or (operator in "/^" and posi == "before")
return stand_alone and (minor_priority or special) return stand_alone and (minor_priority or special)
else: else:

View File

@ -8,7 +8,7 @@ from formal import FormalExp
# ------------------------ # ------------------------
# A console render # A console render
txt_infix = {"+": "+", "-": "-", "*": "*", "/" : "/", ":": ":"} txt_infix = {"+": "+", "-": "-", "*": "*", "/" : "/", ":": ":", "^":"^"}
txt_postfix = {} txt_postfix = {}
txt_other = {"(": "(", ")": ")"} txt_other = {"(": "(", ")": ")"}
@ -17,7 +17,7 @@ txt_render = Render(txt_infix, txt_postfix, txt_other)
# ------------------------ # ------------------------
# A infix to postfix list convertor # A infix to postfix list convertor
p2i_infix = {"+": "+", "-": "-", "*": "*", "/" : "/", ":":":"} p2i_infix = {"+": "+", "-": "-", "*": "*", "/" : "/", ":":":", "^":"^"}
p2i_postfix = {} p2i_postfix = {}
p2i_other = {"(": "(", ")": ")"} p2i_other = {"(": "(", ")": ")"}
@ -36,7 +36,7 @@ def texSlash(op1, op2):
def texFrac(frac): def texFrac(frac):
return ["\\frac{" , str(frac._num) , "}{" , str(frac._denom) , "}"] return ["\\frac{" , str(frac._num) , "}{" , str(frac._denom) , "}"]
tex_infix = {"+": " + ", "-": " - ", "*": " \\times ", ":": ":"} tex_infix = {"+": " + ", "-": " - ", "*": " \\times ", ":": ":", "^":"^"}
tex_postfix = {"/": texSlash} tex_postfix = {"/": texSlash}
tex_other = {"(": "(", ")": ")"} tex_other = {"(": "(", ")": ")"}
tex_type_render = {int: str, Fraction: texFrac, FormalExp: str} tex_type_render = {int: str, Fraction: texFrac, FormalExp: str}
@ -46,11 +46,11 @@ tex_render = Render(tex_infix, tex_postfix, tex_other, type_render = tex_type_re
if __name__ == '__main__': if __name__ == '__main__':
exp = [2, 5, '+', 1, '-', 3, 4, '*', ':'] exp = [2, 5, '^', 1, '-', 3, 4, '*', ':']
print(txt_render(exp)) print(txt_render(exp))
exp = [2, 5, '+', 1, '-', 3, 4, '*', '/', 3, 5, '/', ':'] exp = [2, 5, '^', 1, '-', 3, 4, '*', '/', 3, 5, '/', ':']
print(tex_render(exp)) print(tex_render(exp))
exp = [2, 5, '+', 1, '-', 3, 4, '*', '/', 3, '+'] exp = [2, 5, '^', 1, '-', 3, 4, '*', '/', 3, '+']
print(post2in_fix(exp)) print(post2in_fix(exp))