Add list of operators in operator.py (may need to change the form
This commit is contained in:
parent
633e6b4e2c
commit
9c868b4e79
@ -18,16 +18,28 @@ class Expression(object):
|
||||
:param exp: the expression. It can be a string or a list of postfix tokens.
|
||||
"""
|
||||
if type(exp) == str:
|
||||
self._exp = exp
|
||||
#self._exp = exp
|
||||
print("\t type(exp) :" + str(type(exp)))
|
||||
|
||||
self.postfix_tokens = str2tokens(exp) # les tokens seront alors stockés dans self.tokens temporairement
|
||||
elif type(exp) == list:
|
||||
print("\t type(exp) :" + str(type(exp)))
|
||||
self.postfix_tokens = exp
|
||||
|
||||
print("\t self.postfix_tokens :" + str(self.postfix_tokens))
|
||||
|
||||
|
||||
def __str__(self):
|
||||
"""
|
||||
Overload str
|
||||
If you want to changer render set Expression.RENDER
|
||||
"""
|
||||
print("\t self.STR_RENDER :" + str(self.STR_RENDER))
|
||||
print("\t self.postfix_tokens :" + str(self.postfix_tokens))
|
||||
print("\t self.STR_RENDER(self.postfix_tokens) :" + str(self.STR_RENDER(self.postfix_tokens)))
|
||||
|
||||
|
||||
|
||||
return self.STR_RENDER(self.postfix_tokens)
|
||||
|
||||
def render(self, render = lambda x:str(x)):
|
||||
@ -122,8 +134,9 @@ class Expression(object):
|
||||
|
||||
def test(exp):
|
||||
a = Expression(exp)
|
||||
for i in a.simplify():
|
||||
print(i)
|
||||
print(a)
|
||||
#for i in a.simplify():
|
||||
# print(i)
|
||||
|
||||
print("\n")
|
||||
|
||||
@ -132,6 +145,10 @@ if __name__ == '__main__':
|
||||
exp = "2 ^ 3 * 5"
|
||||
test(exp)
|
||||
|
||||
from pymath.operator import add, pw, mul
|
||||
exp = [2, 3, pw, 5, mul]
|
||||
test(exp)
|
||||
|
||||
exp = "1 + 3 * 5"
|
||||
test(exp)
|
||||
|
||||
|
@ -183,6 +183,13 @@ class Operator(str):
|
||||
return flatten_list([op])
|
||||
|
||||
|
||||
add = Operator("+")
|
||||
sub = Operator("-")
|
||||
mul = Operator("*")
|
||||
div = Operator("/")
|
||||
pw = Operator("^")
|
||||
sub1 = Operator("-", 1)
|
||||
par = Operator("(")
|
||||
|
||||
def save_mainOp(obj, mainOp):
|
||||
"""Create a temporary class build over built-in type to stock the main operation of a calculus
|
||||
|
Loading…
Reference in New Issue
Block a user