handle - at the beginig of expression and after "("
This commit is contained in:
parent
b52ec78d21
commit
95bc15b917
@ -125,6 +125,10 @@ class Expression(object):
|
|||||||
if character.isdigit():
|
if character.isdigit():
|
||||||
if type(tokens[-1]) == int:
|
if type(tokens[-1]) == int:
|
||||||
tokens[-1] = tokens[-1]*10 + int(character)
|
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:
|
else:
|
||||||
tokens.append(int(character))
|
tokens.append(int(character))
|
||||||
elif character in "+-*/()":
|
elif character in "+-*/()":
|
||||||
@ -343,7 +347,10 @@ if __name__ == '__main__':
|
|||||||
exp = "( 2+ 5 )/( 3 * 4 ) + 1 / 2"
|
exp = "( 2+ 5 )/( 3 * 4 ) + 1 / 2"
|
||||||
test(exp)
|
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)
|
test(exp)
|
||||||
|
|
||||||
import doctest
|
import doctest
|
||||||
|
Loading…
Reference in New Issue
Block a user