handle implied "*"

This commit is contained in:
lafrite 2014-01-15 16:51:32 +01:00
parent 95bc15b917
commit 856cf8a323

View File

@ -131,8 +131,15 @@ class Expression(object):
tokens[-1] = - int(character) tokens[-1] = - int(character)
else: else:
tokens.append(int(character)) tokens.append(int(character))
elif character in "+-*/()":
elif character in "+-*/)":
tokens.append(character) tokens.append(character)
elif character in "(":
if self.isNumber(tokens[-1]) or tokens[-1] == ")":
tokens.append("*")
tokens.append(character)
elif character != " ": elif character != " ":
raise ValueError("{} is an unvalid character".format(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" exp="(-2+5)/(3*4)+1/12+5*5"
test(exp) test(exp)
exp="-2*4*(12 + 1)" exp="-2*4(12 + 1)(3-12)"
test(exp) test(exp)
import doctest import doctest