2013-08-09 09:35:14 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
|
2014-11-02 07:19:31 +00:00
|
|
|
from .generic import Stack, flatten_list, expand_list
|
|
|
|
from .operator import Operator
|
2014-01-17 19:17:49 +00:00
|
|
|
from .fraction import Fraction
|
2014-05-28 18:42:19 +00:00
|
|
|
from .renders import txt, post2in_fix, tex
|
2013-11-01 21:58:42 +00:00
|
|
|
|
2014-02-27 17:02:34 +00:00
|
|
|
__all__ = ['Expression']
|
|
|
|
|
2013-08-09 09:35:14 +00:00
|
|
|
class Expression(object):
|
|
|
|
"""A calculus expression. Today it can andle only expression with numbers later it will be able to manipulate unknown"""
|
|
|
|
|
2014-01-28 19:52:38 +00:00
|
|
|
PRIORITY = {"^": 5, "*" : 3, "/": 4, ":": 3, "+": 2, "-":2, "(": 1}
|
2014-08-29 14:35:38 +00:00
|
|
|
STR_RENDER = tex
|
2013-08-09 11:08:24 +00:00
|
|
|
|
2013-08-09 09:35:14 +00:00
|
|
|
def __init__(self, exp):
|
2013-11-01 11:42:42 +00:00
|
|
|
""" Initiate the expression
|
2013-08-09 09:35:14 +00:00
|
|
|
|
|
|
|
:param exp: the expression. It can be a string or a list of tokens. It can be infix or postfix expression
|
|
|
|
"""
|
2013-11-01 21:58:42 +00:00
|
|
|
if type(exp) == str:
|
|
|
|
self._exp = exp
|
|
|
|
self.tokens = self.str2tokens(exp) # les tokens seront alors stockés dans self.tokens temporairement
|
|
|
|
elif type(exp) == list:
|
|
|
|
self.tokens = exp
|
2013-08-09 09:35:14 +00:00
|
|
|
|
2013-11-08 09:05:28 +00:00
|
|
|
self._infix_tokens = None
|
|
|
|
self._postfix_tokens = None
|
|
|
|
|
|
|
|
self.feed_fix() # Determine le fix et range la liste dans self.[fix]_tokens
|
2013-11-01 21:58:42 +00:00
|
|
|
|
2014-01-17 11:48:48 +00:00
|
|
|
def __str__(self):
|
2014-08-29 14:35:38 +00:00
|
|
|
"""
|
|
|
|
Overload str
|
|
|
|
If you want to changer render set Expression.RENDER
|
|
|
|
"""
|
|
|
|
return self.STR_RENDER(self.postfix_tokens)
|
2014-01-17 11:48:48 +00:00
|
|
|
|
|
|
|
def render(self, render = lambda x:str(x)):
|
|
|
|
""" Same as __str__ but accept render as argument
|
2014-01-17 19:17:49 +00:00
|
|
|
:param render: function which render the list of token (postfix form) to string
|
2014-01-17 11:48:48 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
# TODO: I don't like the name of this method |ven. janv. 17 12:48:14 CET 2014
|
|
|
|
return render(self.postfix_tokens)
|
|
|
|
|
2013-11-01 21:58:42 +00:00
|
|
|
## ---------------------
|
|
|
|
## Mechanism functions
|
|
|
|
|
2014-08-29 15:15:29 +00:00
|
|
|
def simplify(self):
|
|
|
|
""" Generator which return steps for computing the expression """
|
2013-11-01 21:58:42 +00:00
|
|
|
if not self.can_go_further():
|
2014-08-29 15:15:29 +00:00
|
|
|
yield self.STR_RENDER(self.postfix_tokens)
|
2013-08-09 10:03:32 +00:00
|
|
|
else:
|
2013-11-02 15:36:08 +00:00
|
|
|
self.compute_exp()
|
2013-11-16 20:39:13 +00:00
|
|
|
old_s = ''
|
2013-11-01 21:58:42 +00:00
|
|
|
for s in self.steps:
|
2014-08-29 15:15:29 +00:00
|
|
|
new_s = self.STR_RENDER(s)
|
2013-11-17 08:38:33 +00:00
|
|
|
# Astuce pour éviter d'avoir deux fois la même étape (par exemple pour la transfo d'une division en fraction)
|
2013-11-16 20:39:13 +00:00
|
|
|
if new_s != old_s:
|
|
|
|
old_s = new_s
|
|
|
|
yield new_s
|
2014-08-29 15:15:29 +00:00
|
|
|
for s in self.child.simplify():
|
2013-11-17 08:38:33 +00:00
|
|
|
if old_s != s:
|
|
|
|
yield s
|
2013-11-01 21:58:42 +00:00
|
|
|
|
|
|
|
def can_go_further(self):
|
2013-11-02 15:36:08 +00:00
|
|
|
"""Check whether it's a last step or not. If not create self.child the next expression.
|
2013-11-01 21:58:42 +00:00
|
|
|
:returns: 1 if it's not the last step, 0 otherwise
|
|
|
|
"""
|
|
|
|
if len(self.tokens) == 1:
|
|
|
|
return 0
|
|
|
|
else:
|
|
|
|
return 1
|
|
|
|
|
|
|
|
def compute_exp(self):
|
2013-11-02 15:36:08 +00:00
|
|
|
""" Create self.child with self.steps to go up to it """
|
2013-11-01 21:58:42 +00:00
|
|
|
self.steps = [self.postfix_tokens]
|
|
|
|
|
|
|
|
tokenList = self.postfix_tokens.copy()
|
|
|
|
tmpTokenList = []
|
|
|
|
|
|
|
|
while len(tokenList) > 2:
|
2014-11-02 07:19:31 +00:00
|
|
|
# on va chercher les motifs du genre A B +, quad l'operateur est d'arité 2, pour les calculer
|
|
|
|
if self.isNumber(tokenList[0]) and self.isNumber(tokenList[1]) \
|
|
|
|
and type(tokenList[2]) == Operator and tokenList[2].arity == 2 :
|
2013-11-01 21:58:42 +00:00
|
|
|
|
|
|
|
# S'il y a une opération à faire
|
|
|
|
op1 = tokenList[0]
|
|
|
|
op2 = tokenList[1]
|
2014-11-02 07:19:31 +00:00
|
|
|
operator = tokenList[2]
|
2013-11-01 21:58:42 +00:00
|
|
|
|
2014-11-02 07:19:31 +00:00
|
|
|
res = operator(op1, op2)
|
2013-11-01 21:58:42 +00:00
|
|
|
|
|
|
|
tmpTokenList.append(res)
|
|
|
|
|
|
|
|
# Comme on vient de faire le calcul, on peut détruire aussi les deux prochains termes
|
|
|
|
del tokenList[0:3]
|
2014-11-02 07:19:31 +00:00
|
|
|
|
|
|
|
elif self.isNumber(tokenList[0]) \
|
|
|
|
and type(tokenList[1]) == Operator and tokenList[1].arity == 1 :
|
|
|
|
|
|
|
|
# S'il y a une opération à faire
|
|
|
|
op1 = tokenList[0]
|
|
|
|
operator = tokenList[1]
|
|
|
|
|
|
|
|
res = operator(op1)
|
|
|
|
|
|
|
|
tmpTokenList.append(res)
|
|
|
|
|
|
|
|
# Comme on vient de faire le calcul, on peut détruire aussi les deux prochains termes
|
|
|
|
del tokenList[0:2]
|
|
|
|
|
2013-11-01 21:58:42 +00:00
|
|
|
else:
|
|
|
|
tmpTokenList.append(tokenList[0])
|
|
|
|
|
|
|
|
del tokenList[0]
|
|
|
|
tmpTokenList += tokenList
|
|
|
|
|
2013-11-02 15:36:08 +00:00
|
|
|
steps = expand_list(tmpTokenList)
|
2013-11-01 21:58:42 +00:00
|
|
|
|
2013-11-02 15:47:47 +00:00
|
|
|
if len(steps[:-1]) > 0:
|
2013-11-16 19:50:42 +00:00
|
|
|
self.steps += [flatten_list(s) for s in steps[:-1]]
|
2013-11-02 15:47:47 +00:00
|
|
|
|
2013-11-02 15:36:08 +00:00
|
|
|
self.child = Expression(steps[-1])
|
2013-11-01 11:42:42 +00:00
|
|
|
|
|
|
|
## ---------------------
|
|
|
|
## String parsing
|
|
|
|
|
2014-02-28 09:31:56 +00:00
|
|
|
@classmethod
|
2013-11-01 21:58:42 +00:00
|
|
|
def str2tokens(self, exp):
|
|
|
|
""" Parse the expression, ie tranform a string into a list of tokens
|
2013-11-01 11:42:42 +00:00
|
|
|
|
2014-01-15 15:32:12 +00:00
|
|
|
/!\ float are not availiable yet!
|
|
|
|
|
2013-11-01 21:58:42 +00:00
|
|
|
:param exp: The expression (a string)
|
|
|
|
:returns: list of token
|
2013-11-01 11:42:42 +00:00
|
|
|
|
2013-11-01 21:58:42 +00:00
|
|
|
"""
|
2014-01-15 15:32:12 +00:00
|
|
|
tokens = ['']
|
|
|
|
|
|
|
|
for character in exp:
|
|
|
|
if character.isdigit():
|
2014-01-17 19:17:49 +00:00
|
|
|
# for "big" numbers (like 2345)
|
2014-01-15 15:32:12 +00:00
|
|
|
if type(tokens[-1]) == int:
|
2014-01-18 18:25:10 +00:00
|
|
|
if tokens[-1] > 0:
|
|
|
|
tokens[-1] = tokens[-1]*10 + int(character)
|
|
|
|
else:
|
|
|
|
tokens[-1] = tokens[-1]*10 - int(character)
|
|
|
|
|
2014-01-17 19:17:49 +00:00
|
|
|
|
2014-01-15 15:41:51 +00:00
|
|
|
# Special case for "-" at the begining of an expression or before "("
|
|
|
|
elif tokens[-1] == "-" and \
|
2014-01-19 17:38:15 +00:00
|
|
|
str(tokens[-2]) in " (+-*/:":
|
2014-01-15 15:41:51 +00:00
|
|
|
tokens[-1] = - int(character)
|
2014-01-15 15:32:12 +00:00
|
|
|
else:
|
|
|
|
tokens.append(int(character))
|
2014-01-15 15:51:32 +00:00
|
|
|
|
2014-01-28 19:52:38 +00:00
|
|
|
elif character in "+-*/):^":
|
2014-01-15 15:51:32 +00:00
|
|
|
tokens.append(character)
|
|
|
|
|
|
|
|
elif character in "(":
|
2014-02-28 08:43:54 +00:00
|
|
|
# If "3(", ")("
|
2014-01-17 19:17:49 +00:00
|
|
|
if self.isNumber(tokens[-1]) \
|
2014-02-28 08:43:54 +00:00
|
|
|
or tokens[-1] == ")" :
|
2014-01-15 15:51:32 +00:00
|
|
|
tokens.append("*")
|
2014-01-15 15:32:12 +00:00
|
|
|
tokens.append(character)
|
2014-01-15 15:51:32 +00:00
|
|
|
|
2014-02-28 10:34:31 +00:00
|
|
|
elif character == ".":
|
|
|
|
raise ValueError("No float number please")
|
|
|
|
|
2014-01-15 15:32:12 +00:00
|
|
|
elif character != " ":
|
|
|
|
raise ValueError("{} is an unvalid character".format(character))
|
|
|
|
|
|
|
|
return tokens[1:]
|
2013-11-01 11:42:42 +00:00
|
|
|
|
2013-11-01 21:58:42 +00:00
|
|
|
# ---------------------
|
|
|
|
# "fix" recognition
|
2013-11-01 11:42:42 +00:00
|
|
|
|
2013-11-01 21:58:42 +00:00
|
|
|
@classmethod
|
|
|
|
def get_fix(self, tokens):
|
|
|
|
""" Give the "fix" of an expression
|
2013-11-08 09:05:28 +00:00
|
|
|
[A, +, B] -> infix, or if there is parenthesis it is infix
|
2013-11-01 21:58:42 +00:00
|
|
|
[+, A, B] -> prefix
|
|
|
|
[A, B, +] -> postfix
|
|
|
|
/!\ does not verify if the expression is correct/computable!
|
|
|
|
|
|
|
|
:param exp: the expression (list of token)
|
|
|
|
:returns: the "fix" (infix, postfix, prefix)
|
|
|
|
|
|
|
|
"""
|
|
|
|
if self.isOperator(tokens[0]):
|
|
|
|
return "prefix"
|
2013-11-08 09:05:28 +00:00
|
|
|
elif "(" in tokens:
|
|
|
|
return "infix"
|
2013-11-01 21:58:42 +00:00
|
|
|
elif not self.isOperator(tokens[0]) and not self.isOperator(tokens[1]):
|
|
|
|
return "postfix"
|
|
|
|
else:
|
|
|
|
return "infix"
|
|
|
|
|
2013-11-08 09:05:28 +00:00
|
|
|
def feed_fix(self):
|
2013-11-01 21:58:42 +00:00
|
|
|
""" Recognize the fix of self.tokens and stock tokens in self.[fix]_tokens """
|
2013-11-02 15:36:08 +00:00
|
|
|
if len(self.tokens) > 1:
|
|
|
|
fix = self.get_fix(self.tokens)
|
|
|
|
else:
|
|
|
|
fix = "postfix" # Completement arbitraire mais on s'en fiche!
|
2013-11-01 21:58:42 +00:00
|
|
|
|
|
|
|
setattr(self, fix+"_tokens", self.tokens)
|
|
|
|
|
2013-11-08 09:05:28 +00:00
|
|
|
|
2013-11-01 21:58:42 +00:00
|
|
|
# ----------------------
|
|
|
|
# Expressions - tokens manipulation
|
|
|
|
|
|
|
|
@property
|
|
|
|
def infix_tokens(self):
|
|
|
|
""" Return infix list of tokens. Verify if it has already been computed and compute it if not
|
|
|
|
|
|
|
|
:returns: infix list of tokens
|
|
|
|
"""
|
2013-11-08 09:05:28 +00:00
|
|
|
if self._infix_tokens:
|
2013-11-01 21:58:42 +00:00
|
|
|
return self._infix_tokens
|
|
|
|
|
2013-11-08 09:05:28 +00:00
|
|
|
elif self._postfix_tokens:
|
2013-12-08 21:24:27 +00:00
|
|
|
self._infix_tokens = post2in_fix(self._postfix_tokens)
|
2013-11-01 21:58:42 +00:00
|
|
|
return self._infix_tokens
|
|
|
|
|
|
|
|
else:
|
|
|
|
raise ValueError("Unkown fix")
|
|
|
|
|
|
|
|
@infix_tokens.setter
|
|
|
|
def infix_tokens(self, val):
|
|
|
|
self._infix_tokens = val
|
|
|
|
|
|
|
|
@property
|
|
|
|
def postfix_tokens(self):
|
|
|
|
""" Return postfix list of tokens. Verify if it has already been computed and compute it if not
|
|
|
|
|
|
|
|
:returns: postfix list of tokens
|
|
|
|
"""
|
2013-11-08 09:05:28 +00:00
|
|
|
if self._postfix_tokens:
|
2013-11-01 21:58:42 +00:00
|
|
|
return self._postfix_tokens
|
|
|
|
|
2013-11-08 09:05:28 +00:00
|
|
|
elif self._infix_tokens:
|
2013-11-17 07:53:05 +00:00
|
|
|
self._postfix_tokens = self.in2post_fix(self._infix_tokens)
|
2013-11-01 21:58:42 +00:00
|
|
|
return self._postfix_tokens
|
|
|
|
|
|
|
|
else:
|
|
|
|
raise ValueError("Unkown fix")
|
|
|
|
|
|
|
|
@postfix_tokens.setter
|
|
|
|
def postfix_tokens(self, val):
|
|
|
|
self._postfix_tokens = val
|
|
|
|
|
|
|
|
# ----------------------
|
|
|
|
# "fix" tranformations
|
|
|
|
|
2013-11-17 07:53:05 +00:00
|
|
|
@classmethod
|
2013-12-08 20:02:19 +00:00
|
|
|
def in2post_fix(cls, infix_tokens):
|
2013-11-17 07:53:05 +00:00
|
|
|
""" From the infix_tokens list compute the corresponding postfix_tokens list
|
2013-11-08 09:05:28 +00:00
|
|
|
|
2013-11-17 07:53:05 +00:00
|
|
|
@param infix_tokens: the infix list of tokens to transform into postfix form.
|
|
|
|
@return: the corresponding postfix list of tokens.
|
2013-11-08 09:05:28 +00:00
|
|
|
|
|
|
|
>>> Expression.in2post_fix(['(', 2, '+', 5, '-', 1, ')', '/', '(', 3, '*', 4, ')'])
|
|
|
|
[2, 5, '+', 1, '-', 3, 4, '*', '/']
|
2014-10-23 10:59:21 +00:00
|
|
|
>>> Expression.in2post_fix(['-', '(', '-', 2, ')'])
|
2014-10-23 11:13:28 +00:00
|
|
|
[2, '-', '-']
|
2014-10-23 10:59:21 +00:00
|
|
|
>>> Expression.in2post_fix(['-', '(', '-', 2, '+', 3, "*", 4, ')'])
|
2014-10-23 11:13:28 +00:00
|
|
|
[2, '-', 3, 4, '*', '+', '-']
|
2013-11-08 09:05:28 +00:00
|
|
|
"""
|
2014-10-18 17:04:40 +00:00
|
|
|
# Stack where operator will be stocked
|
2013-11-01 21:58:42 +00:00
|
|
|
opStack = Stack()
|
2014-10-18 17:04:40 +00:00
|
|
|
# final postfix list of tokens
|
2014-10-23 10:59:21 +00:00
|
|
|
postfix_tokens = []
|
|
|
|
# stack with the nbr of tokens still to compute in postfix_tokens
|
|
|
|
arity_Stack = Stack()
|
|
|
|
arity_Stack.push(0)
|
2013-11-01 21:58:42 +00:00
|
|
|
|
2014-10-18 17:04:40 +00:00
|
|
|
for (pos_token,token) in enumerate(infix_tokens):
|
2014-10-23 10:59:21 +00:00
|
|
|
|
2014-10-23 11:13:28 +00:00
|
|
|
# # 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))
|
2013-11-01 21:58:42 +00:00
|
|
|
if token == "(":
|
|
|
|
opStack.push(token)
|
2014-10-23 10:59:21 +00:00
|
|
|
# Set next arity counter
|
|
|
|
arity_Stack.push(0)
|
2013-11-01 21:58:42 +00:00
|
|
|
elif token == ")":
|
2014-10-18 17:04:40 +00:00
|
|
|
op = opStack.pop()
|
|
|
|
while op != "(":
|
2014-10-23 10:59:21 +00:00
|
|
|
postfix_tokens.append(op)
|
2014-10-18 17:04:40 +00:00
|
|
|
op = opStack.pop()
|
|
|
|
|
2014-10-23 10:59:21 +00:00
|
|
|
# Go back to old arity
|
|
|
|
arity_Stack.pop()
|
|
|
|
# Raise the arity
|
|
|
|
arity = arity_Stack.pop()
|
|
|
|
arity_Stack.push(arity + 1)
|
2014-10-18 17:04:40 +00:00
|
|
|
|
2013-12-08 20:02:19 +00:00
|
|
|
elif cls.isOperator(token):
|
|
|
|
while (not opStack.isEmpty()) and (cls.PRIORITY[opStack.peek()] >= cls.PRIORITY[token]):
|
2014-10-18 17:04:40 +00:00
|
|
|
op = opStack.pop()
|
2014-10-23 10:59:21 +00:00
|
|
|
postfix_tokens.append(op)
|
2014-10-18 17:04:40 +00:00
|
|
|
|
2014-10-23 10:59:21 +00:00
|
|
|
arity = arity_Stack.pop()
|
|
|
|
opStack.push(Operator(token, arity + 1))
|
2014-10-23 11:13:28 +00:00
|
|
|
# print("--", token, " -> ", str(arity + 1))
|
2014-10-23 10:59:21 +00:00
|
|
|
# Reset arity to 0 in case there is other operators (the real operation would be "-op.arity + 1")
|
|
|
|
arity_Stack.push(0)
|
|
|
|
else:
|
|
|
|
postfix_tokens.append(token)
|
|
|
|
arity = arity_Stack.pop()
|
|
|
|
arity_Stack.push(arity + 1)
|
2013-11-01 21:58:42 +00:00
|
|
|
|
|
|
|
while not opStack.isEmpty():
|
2014-10-23 10:59:21 +00:00
|
|
|
op = opStack.pop()
|
|
|
|
postfix_tokens.append(op)
|
|
|
|
|
2014-10-23 11:13:28 +00:00
|
|
|
# # 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))
|
2014-10-23 10:59:21 +00:00
|
|
|
|
2014-10-23 11:13:28 +00:00
|
|
|
if arity_Stack.peek() != 1:
|
|
|
|
raise ValueError("Unvalid expression. The arity Stack is ", str(arity_Stack))
|
2013-11-01 21:58:42 +00:00
|
|
|
|
2014-10-23 10:59:21 +00:00
|
|
|
return postfix_tokens
|
2013-11-08 09:05:28 +00:00
|
|
|
|
2013-11-01 21:58:42 +00:00
|
|
|
## ---------------------
|
|
|
|
## Recognize numbers and operators
|
2013-11-01 11:42:42 +00:00
|
|
|
|
2013-11-17 07:53:05 +00:00
|
|
|
@staticmethod
|
|
|
|
def isNumber(exp):
|
2013-11-01 21:58:42 +00:00
|
|
|
"""Check if the expression can be a number
|
|
|
|
|
|
|
|
:param exp: an expression
|
|
|
|
:returns: True if the expression can be a number and false otherwise
|
|
|
|
|
|
|
|
"""
|
2014-01-17 13:57:45 +00:00
|
|
|
return type(exp) == int or \
|
2014-02-28 08:43:54 +00:00
|
|
|
type(exp) == Fraction
|
2013-11-01 21:58:42 +00:00
|
|
|
|
2013-11-17 07:53:05 +00:00
|
|
|
@staticmethod
|
|
|
|
def isOperator(exp):
|
2014-01-28 19:52:38 +00:00
|
|
|
"""Check if the expression is an opération in "+-*/:^"
|
2013-11-01 21:58:42 +00:00
|
|
|
|
|
|
|
:param exp: an expression
|
|
|
|
:returns: boolean
|
|
|
|
|
|
|
|
"""
|
2014-01-28 19:52:38 +00:00
|
|
|
return (type(exp) == str and exp in "+-*/:^")
|
2013-11-01 11:42:42 +00:00
|
|
|
|
2013-11-02 15:47:47 +00:00
|
|
|
|
|
|
|
def test(exp):
|
|
|
|
a = Expression(exp)
|
2014-08-29 15:15:29 +00:00
|
|
|
for i in a.simplify():
|
2013-11-01 11:42:42 +00:00
|
|
|
print(i)
|
2013-08-09 09:35:14 +00:00
|
|
|
|
2013-11-08 09:05:28 +00:00
|
|
|
print("\n")
|
|
|
|
|
2013-11-02 15:47:47 +00:00
|
|
|
if __name__ == '__main__':
|
2014-11-02 10:12:47 +00:00
|
|
|
Expression.STR_RENDER = txt
|
2014-11-02 07:19:31 +00:00
|
|
|
exp = "2 ^ 3 * 5"
|
|
|
|
test(exp)
|
2013-11-02 15:47:47 +00:00
|
|
|
|
2014-11-02 07:19:31 +00:00
|
|
|
exp = "1 + 3 * 5"
|
|
|
|
test(exp)
|
2014-01-17 19:17:49 +00:00
|
|
|
|
2013-11-08 09:05:28 +00:00
|
|
|
#exp = "2 * 3 * 3 * 5"
|
|
|
|
#test(exp)
|
2013-11-02 15:47:47 +00:00
|
|
|
|
2014-01-17 19:17:49 +00:00
|
|
|
#exp = "2 * 3 + 3 * 5"
|
|
|
|
#test(exp)
|
2013-11-02 15:47:47 +00:00
|
|
|
|
2014-01-17 19:17:49 +00:00
|
|
|
#exp = "2 * ( 3 + 4 ) + 3 * 5"
|
|
|
|
#test(exp)
|
2013-11-08 09:05:28 +00:00
|
|
|
|
2013-11-02 15:47:47 +00:00
|
|
|
#exp = "2 * ( 3 + 4 ) + ( 3 - 4 ) * 5"
|
|
|
|
#test(exp)
|
|
|
|
#
|
|
|
|
#exp = "2 * ( 2 - ( 3 + 4 ) ) + ( 3 - 4 ) * 5"
|
|
|
|
#test(exp)
|
|
|
|
#
|
|
|
|
#exp = "2 * ( 2 - ( 3 + 4 ) ) + 5 * ( 3 - 4 )"
|
|
|
|
#test(exp)
|
|
|
|
#
|
|
|
|
#exp = "2 + 5 * ( 3 - 4 )"
|
|
|
|
#test(exp)
|
2013-11-08 09:05:28 +00:00
|
|
|
|
2014-01-28 19:52:38 +00:00
|
|
|
#exp = "( 2 + 5 ) * ( 3 - 4 )^4"
|
2013-11-17 07:53:05 +00:00
|
|
|
#test(exp)
|
2013-11-08 09:05:28 +00:00
|
|
|
|
2013-11-17 07:53:05 +00:00
|
|
|
#exp = "( 2 + 5 ) * ( 3 * 4 )"
|
|
|
|
#test(exp)
|
2013-11-08 09:05:28 +00:00
|
|
|
|
2014-01-17 19:17:49 +00:00
|
|
|
#exp = "( 2 + 5 - 1 ) / ( 3 * 4 )"
|
|
|
|
#test(exp)
|
2013-11-02 15:47:47 +00:00
|
|
|
|
2014-01-17 19:17:49 +00:00
|
|
|
#exp = "( 2 + 5 ) / ( 3 * 4 ) + 1 / 12"
|
|
|
|
#test(exp)
|
2013-11-02 15:47:47 +00:00
|
|
|
|
2014-01-17 19:17:49 +00:00
|
|
|
#exp = "( 2+ 5 )/( 3 * 4 ) + 1 / 2"
|
|
|
|
#test(exp)
|
2013-11-02 15:47:47 +00:00
|
|
|
|
2014-01-17 19:17:49 +00:00
|
|
|
#exp="(-2+5)/(3*4)+1/12+5*5"
|
|
|
|
#test(exp)
|
2014-01-15 15:41:51 +00:00
|
|
|
|
2014-01-28 19:52:38 +00:00
|
|
|
#exp="-2*4(12 + 1)(3-12)"
|
2014-01-19 17:38:15 +00:00
|
|
|
#test(exp)
|
2014-01-28 19:52:38 +00:00
|
|
|
|
2014-01-17 19:17:49 +00:00
|
|
|
|
2014-01-17 19:17:49 +00:00
|
|
|
#exp="(-2+5)/(3*4)+1/12+5*5"
|
|
|
|
#test(exp)
|
|
|
|
|
2014-01-17 13:57:45 +00:00
|
|
|
# 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)
|
|
|
|
|
2014-01-15 15:54:33 +00:00
|
|
|
## Can't handle it yet!!
|
|
|
|
#exp="-(-2)"
|
|
|
|
#test(exp)
|
|
|
|
|
2013-11-17 07:53:05 +00:00
|
|
|
import doctest
|
|
|
|
doctest.testmod()
|
2013-11-02 15:47:47 +00:00
|
|
|
|
2013-08-09 09:35:14 +00:00
|
|
|
# -----------------------------
|
|
|
|
# Reglages pour 'vim'
|
|
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
|
|
|
# cursor: 16 del
|