From 40acd4ce82aabe3100b9f344acd4f6579c7a5df3 Mon Sep 17 00:00:00 2001 From: leifyung Date: Wed, 4 Mar 2026 18:29:53 +0800 Subject: [PATCH] Refactor functions into Calculator class - Add Calculator class with add and multiply methods - Maintain original functions for compatibility - Update test code to use class interface --- test.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/test.py b/test.py index 022132a..2159e62 100644 --- a/test.py +++ b/test.py @@ -1,3 +1,14 @@ +class Calculator: + """A calculator class that provides basic arithmetic operations.""" + + def add(self, a, b): + """Return the sum of two numbers.""" + return a + b + + def multiply(self, a, b): + """Return the product of two numbers.""" + return a * b + def add_two_numbers(a, b): """Return the sum of two numbers.""" return a + b @@ -7,9 +18,17 @@ def multiply_two_numbers(a, b): return a * b if __name__ == "__main__": - # Example usage - sum_result = add_two_numbers(5, 3) + # Example usage with the Calculator class + calc = Calculator() + + # Test addition using Calculator class + sum_result = calc.add(5, 3) print(f"The sum is: {sum_result}") - product_result = multiply_two_numbers(5, 3) - print(f"The product is: {product_result}") \ No newline at end of file + # Test multiplication using Calculator class + product_result = calc.multiply(5, 3) + print(f"The product is: {product_result}") + + # Also test the original functions for compatibility + print(f"Function add result: {add_two_numbers(5, 3)}") + print(f"Function multiply result: {multiply_two_numbers(5, 3)}") \ No newline at end of file