diff --git a/src/main/py/read_math_function.py b/src/main/py/read_math_function.py index c6ddc4e..f2a2a7b 100644 --- a/src/main/py/read_math_function.py +++ b/src/main/py/read_math_function.py @@ -11,3 +11,10 @@ def is_string(value): def is_allowed_string(type, value): allowed = "+-*/()^" return all(character.isdigit() or character in allowed for character in value) + +def calculate_string(string): + try: + result = eval(string) + return result + except Exception as e: + return f"Fehler bei der Berechnung: {str(e)}" \ No newline at end of file diff --git a/src/test/py/test_read_math_function.py b/src/test/py/test_read_math_function.py index cb2754d..6f2bfd8 100644 --- a/src/test/py/test_read_math_function.py +++ b/src/test/py/test_read_math_function.py @@ -15,6 +15,8 @@ class read_function(unittest.TestCase): self.assertEqual(is_allowed_string("c", "42^(2)+1-3*4/3"), True) def test_constant_isallowed_string_false(self): self.assertEqual(is_allowed_string("c", "42^(2)+1-3*4/3x"), False) + def test_constant_calculate_without_pow(self): + self.assertEqual(calculate_string("42+1-3*4/3"), 39) if __name__ == '__main__': unittest.main()