示例#1
0
文件: app.py 项目: marcpare/edsger
def evaluate():
	expression = request.GET["expression"]
	
	response.content_type = 'text/json'
	try:
		result = edsger.evaluate(expression)
		return json.dumps(result)
	except Exception, err:
		return json.dumps(err.__class__.__name__ + ": " + str(err))
示例#2
0
	def test_unary_minus(self):
		self.assertAlmostEqual(evaluate('1--2'), 3)
		self.assertAlmostEqual(evaluate('-3+-2'), -5)
		self.assertAlmostEqual(evaluate('1+(-2*2)'), -3)
		self.assertAlmostEqual(evaluate('1--(-2^2)'), -3)
		self.assertAlmostEqual(evaluate('(1)-2'), -1)
示例#3
0
	def test_order_of_operations(self):
		self.assertAlmostEqual(evaluate("(1 + 2) * (4 - 1) ^ 2"), 27)
		self.assertAlmostEqual(evaluate("(1 + 2) * (4 - 2 ^ 3)"), -12)
		self.assertAlmostEqual(evaluate("10 / 2 / 3"), 10.0 / 2.0 / 3.0)
示例#4
0
	def test_nested_par(self):
		self.assertAlmostEqual(evaluate("((10 - 8) * (2)) + (5 / 3)"), ((10 - 8) * (2)) + (5.0 / 3))
示例#5
0
	def test_assoc(self):
		self.assertAlmostEqual(evaluate('1 + 2 * 3'), 7)
		self.assertAlmostEqual(evaluate('1 + 2 * 3 ^ 2'), 19)
示例#6
0
	def test_division(self):
		self.assertAlmostEqual(evaluate('10 / 2'), 5)
		self.assertAlmostEqual(evaluate('3 / 2'), 3.0/2.0)
示例#7
0
	def test_multiplication(self):
		self.assertAlmostEqual(evaluate('2 * 3'), 6)
示例#8
0
	def test_subtraction(self):
		self.assertAlmostEqual(evaluate('1 - 2'), -1)
示例#9
0
	def test_addition(self):
		self.assertAlmostEqual(evaluate('1 + 2'), 3)