예제 #1
0
def test_calculate():
    """
    test for final calculation method
    """
    equation_parser = EquationParser("10/3+10/3")
    equation_parser.build()
    res = equation_parser.calculate()
    assert compare(res, ['success', 7])

    equation_parser = EquationParser("4*9/3+10/3-1")
    equation_parser.build()
    res = equation_parser.calculate()
    assert compare(res, ['success', 14])

    equation_parser = EquationParser("10/0*6+10/4")
    equation_parser.build()
    res = equation_parser.calculate()
    assert compare(res, ['error', CalcError.DivisionError])

    equation_parser = EquationParser("10/6*0+10/4")
    equation_parser.build()
    res = equation_parser.calculate()
    assert compare(res, ['error', CalcError.DivisionError])
예제 #2
0
def calculate(equation):
    """
    Calculates The Final Result
    :param equation:
    :return:
    """
    if len(equation) == 0:
        return CalcError.FormatError.value
    if not is_symbol_permitted_equation(equation):
        return CalcError.SymbolError.value
    if has_edge_operator(equation):
        return CalcError.FormatError.value
    if not is_correct_sequence(equation):
        return CalcError.FormatError.value
    equation_parser = EquationParser(equation)
    equation_parser.build()
    res = equation_parser.calculate()
    if res[0] == "error":
        return res[1].value
    return str(res[1])