Exemplo n.º 1
0
def eval():
    expression = request.args.get('expression', '')
    notation = request.args.get('notation', 'standard')

    try:
        retval = {
            'expression': expression,
            'notation': notation,
            'result': evaluate(expression, notation == 'rpn'),
            'error': False,
            'error_str': ''
        }

    except RuntimeError as e:
        retval = {
            'expression': expression,
            'notation': notation,
            'result': '',
            'error': True,
            'error_str': str(e)
        }

    except BaseException as e:
        retval = {
            'expression': expression,
            'notation': notation,
            'result': '',
            'error': True,
            'error_str': "Other error: " + str(e)
        }

    return jsonify(retval)
Exemplo n.º 2
0
def main():
    expression = None
    notation = None

    while expression is None or expression.strip() == '':
        expression = input("Please enter the expression: ")

    while notation not in ['y', 'n']:
        notation = input("Use postfix notation? [y/n]: ")

    print("Output:", evaluator.evaluate(expression))
Exemplo n.º 3
0
    def test_equation_in_postfix_not_allowed(self):
        with self.assertRaises(RuntimeError):
            evaluator.evaluate('(5 + 2)', True)

        with self.assertRaises(RuntimeError):
            evaluator.evaluate('x + 1', True)

        with self.assertRaises(RuntimeError):
            evaluator.evaluate('x = 5', True)
Exemplo n.º 4
0
 def test_case_8(self):
     self.assertEqual(2, evaluator.evaluate("4x-7(2-x)=3x+2"))
Exemplo n.º 5
0
 def test_case_9(self):
     self.assertEqual(9.8, evaluator.evaluate("2(w+3)-10=6(32-3w)"))
Exemplo n.º 6
0
    def test_division(self):
        self.assertEqual(6, evaluator.evaluate('60 / 10'))
        self.assertEqual(20, evaluator.evaluate('4x / 2 = 40'))

        with self.assertRaises(RuntimeError):
            evaluator.evaluate('20 / (2x) = 5')
Exemplo n.º 7
0
 def test_product(self):
     self.assertEqual(6, evaluator.evaluate('3 * 2'))
     self.assertEqual(20, evaluator.evaluate('x * 2 = 40'))
Exemplo n.º 8
0
 def test_simple_equation(self):
     self.assertEqual(5, evaluator.evaluate('x = 5'))
     self.assertEqual(5, evaluator.evaluate('2x = 10'))
Exemplo n.º 9
0
 def test_case_1(self):
     self.assertAlmostEqual(-4.7509872468,
                            evaluator.evaluate("3+sin(6*3)-7"))
Exemplo n.º 10
0
 def test_log(self):
     self.assertEqual(evaluator.evaluate("log 100"), 2)
     self.assertAlmostEqual(evaluator.evaluate("log 100 (1000)"), 1.5)
     self.assertAlmostEqual(evaluator.evaluate("ln e"), 1)
Exemplo n.º 11
0
 def test_case_14(self):
     self.assertEqual(0.25, evaluator.evaluate("2 * x + 0.5 = 1"))
Exemplo n.º 12
0
 def test_trigonometric(self):
     self.assertAlmostEqual(evaluator.evaluate("sin(pi)"),
                            math.sin(math.pi))
     self.assertAlmostEqual(evaluator.evaluate("cos(pi)"),
                            math.cos(math.pi))
Exemplo n.º 13
0
 def test_simple(self):
     self.assertEqual(5, evaluator.evaluate('5'))
Exemplo n.º 14
0
    def test_functions(self):
        with self.assertRaises(RuntimeError):
            evaluator.evaluate("sin (5x) = 5")

        with self.assertRaises(RuntimeError):
            evaluator.evaluate("sin")
Exemplo n.º 15
0
    def test_bad_equation(self):
        with self.assertRaises(RuntimeError):
            evaluator.evaluate('5x + 2')

        with self.assertRaises(RuntimeError):
            evaluator.evaluate('5x * x = 5')

        with self.assertRaises(RuntimeError):
            evaluator.evaluate('x + y = 0')

        with self.assertRaises(RuntimeError):
            evaluator.evaluate('5 + 2 = 0')

        with self.assertRaises(RuntimeError):
            evaluator.evaluate('5x + 2 = 0 = 0')

        with self.assertRaises(RuntimeError):
            evaluator.evaluate("x/(2x) = 5")
Exemplo n.º 16
0
 def test_bad_expression(self):
     with self.assertRaises(RuntimeError):
         evaluator.evaluate('5 + ')
Exemplo n.º 17
0
 def test_case_10(self):
     self.assertEqual(-3.5, evaluator.evaluate("(4-2z)/3 = 3/4 - (5z)/6"))
Exemplo n.º 18
0
 def test_case_17(self):
     self.assertEqual(1, evaluator.evaluate("Log10"))
Exemplo n.º 19
0
 def test_case_12(self):
     self.assertAlmostEqual(-0.1428571429,
                            evaluator.evaluate("(3(7x-1)+10x-4+3x)=90x+1"))
Exemplo n.º 20
0
 def test_case_19(self):
     self.assertEqual(0, evaluator.evaluate("sinpi"))
Exemplo n.º 21
0
 def test_case_16(self):
     self.assertEqual(1, evaluator.evaluate("Log(10)"))
Exemplo n.º 22
0
 def test_empty_expression(self):
     with self.assertRaises(RuntimeError):
         evaluator.evaluate("")
Exemplo n.º 23
0
 def test_case_18(self):
     self.assertEqual(0.5, evaluator.evaluate("Log100(10)"))
Exemplo n.º 24
0
 def test_case_3(self):
     self.assertEqual(0.25, evaluator.evaluate("2x + 1 = 2 - 2x"))
Exemplo n.º 25
0
 def test_case_20(self):
     self.assertEqual(0, evaluator.evaluate("sin(pi)"))
Exemplo n.º 26
0
 def test_case_4(self):
     self.assertEqual(30, evaluator.evaluate("(3+(4-1))*5"))
Exemplo n.º 27
0
 def test_case_22(self):
     self.assertEqual(-1, evaluator.evaluate("sin(1.5*pi)"))
Exemplo n.º 28
0
 def test_case_6(self):
     self.assertEqual(0.25, evaluator.evaluate("2x + 1 = 2(1-x)"))
Exemplo n.º 29
0
 def test_case_7(self):
     self.assertEqual(2, evaluator.evaluate("(x-2)*(2-3)=0"))
Exemplo n.º 30
0
 def test_subtraction(self):
     self.assertEqual(3, evaluator.evaluate('5 - 2'))
     self.assertEqual(5, evaluator.evaluate('3x - x = 10'))
     self.assertEqual(0, evaluator.evaluate('5 + 5 - 10'))