From 856cf8a323c40ed131cff1e2c50580e5b87be724 Mon Sep 17 00:00:00 2001 From: lafrite Date: Wed, 15 Jan 2014 16:51:32 +0100 Subject: [PATCH] handle implied "*" --- expression.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/expression.py b/expression.py index 9309c1d..9164709 100644 --- a/expression.py +++ b/expression.py @@ -131,8 +131,15 @@ class Expression(object): tokens[-1] = - int(character) else: tokens.append(int(character)) - elif character in "+-*/()": + + elif character in "+-*/)": tokens.append(character) + + elif character in "(": + if self.isNumber(tokens[-1]) or tokens[-1] == ")": + tokens.append("*") + tokens.append(character) + elif character != " ": raise ValueError("{} is an unvalid character".format(character)) @@ -350,7 +357,7 @@ if __name__ == '__main__': exp="(-2+5)/(3*4)+1/12+5*5" test(exp) - exp="-2*4*(12 + 1)" + exp="-2*4(12 + 1)(3-12)" test(exp) import doctest