From 95bc15b91721b2256e1011b27a535a8c36707dab Mon Sep 17 00:00:00 2001 From: lafrite Date: Wed, 15 Jan 2014 16:41:51 +0100 Subject: [PATCH] handle - at the beginig of expression and after "(" --- expression.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/expression.py b/expression.py index 9433ce4..9309c1d 100644 --- a/expression.py +++ b/expression.py @@ -125,6 +125,10 @@ class Expression(object): if character.isdigit(): if type(tokens[-1]) == int: tokens[-1] = tokens[-1]*10 + int(character) + # Special case for "-" at the begining of an expression or before "(" + elif tokens[-1] == "-" and \ + str(tokens[-2]) in " (": + tokens[-1] = - int(character) else: tokens.append(int(character)) elif character in "+-*/()": @@ -343,7 +347,10 @@ if __name__ == '__main__': exp = "( 2+ 5 )/( 3 * 4 ) + 1 / 2" test(exp) - exp="(2+5)/(3*4)+1/12+5*5" + exp="(-2+5)/(3*4)+1/12+5*5" + test(exp) + + exp="-2*4*(12 + 1)" test(exp) import doctest