Replace palindrome function with multiply and divide operations
- Add multiply() function for multiplication - Add divide() function with zero divisor check - Add comprehensive test cases and interactive mode - Remove palindrome detection functionality
This commit is contained in:
173
math.py
173
math.py
@@ -1,70 +1,155 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""
|
"""
|
||||||
math.py - 判断回文数的脚本
|
math.py - 数学运算工具
|
||||||
|
提供乘法和除法运算功能
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def is_palindrome(n: int) -> bool:
|
def multiply(a: float, b: float) -> float:
|
||||||
"""
|
"""
|
||||||
判断一个整数是否是回文数。
|
计算两个数的乘积
|
||||||
|
|
||||||
参数:
|
参数:
|
||||||
n: 要检查的整数
|
a: 第一个数
|
||||||
|
b: 第二个数
|
||||||
|
|
||||||
返回:
|
返回:
|
||||||
如果是回文数返回True,否则返回False
|
两个数的乘积
|
||||||
"""
|
"""
|
||||||
if n < 0:
|
return a * b
|
||||||
return False
|
|
||||||
if n == 0:
|
|
||||||
return True
|
|
||||||
|
|
||||||
# 转换为字符串并检查是否等于反转后的字符串
|
|
||||||
str_n = str(n)
|
|
||||||
return str_n == str_n[::-1]
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def divide(a: float, b: float) -> float:
|
||||||
"""测试函数"""
|
"""
|
||||||
test_cases = [
|
计算两个数的除法
|
||||||
121, # True
|
|
||||||
12321, # True
|
参数:
|
||||||
123, # False
|
a: 被除数
|
||||||
1221, # True
|
b: 除数
|
||||||
123321, # True
|
|
||||||
1234321, # True
|
返回:
|
||||||
12344321, # True
|
除法结果
|
||||||
10, # False
|
|
||||||
0, # True
|
异常:
|
||||||
-121, # False
|
ValueError: 当除数为0时抛出
|
||||||
123456, # False
|
"""
|
||||||
123454321,# True
|
if b == 0:
|
||||||
|
raise ValueError("除数不能为0")
|
||||||
|
return a / b
|
||||||
|
|
||||||
|
|
||||||
|
def test_operations():
|
||||||
|
"""测试乘法和除法运算"""
|
||||||
|
print("测试乘法运算:")
|
||||||
|
print("-" * 30)
|
||||||
|
|
||||||
|
# 测试乘法
|
||||||
|
test_cases_multiply = [
|
||||||
|
(2, 3, 6),
|
||||||
|
(0, 5, 0),
|
||||||
|
(-3, 4, -12),
|
||||||
|
(2.5, 4, 10.0),
|
||||||
|
(-2.5, -4, 10.0),
|
||||||
|
(0, 0, 0),
|
||||||
]
|
]
|
||||||
|
|
||||||
print("测试回文数判断函数:")
|
for a, b, expected in test_cases_multiply:
|
||||||
print("-" * 40)
|
result = multiply(a, b)
|
||||||
for num in test_cases:
|
status = "✓" if abs(result - expected) < 1e-10 else "✗"
|
||||||
result = is_palindrome(num)
|
print(f"{a} × {b} = {result} {status} (期望: {expected})")
|
||||||
print(f"{num:12} -> {'是回文数' if result else '不是回文数'}")
|
|
||||||
|
|
||||||
# 额外测试:用户输入
|
print("\n测试除法运算:")
|
||||||
|
print("-" * 30)
|
||||||
|
|
||||||
|
# 测试除法
|
||||||
|
test_cases_divide = [
|
||||||
|
(6, 2, 3),
|
||||||
|
(10, 4, 2.5),
|
||||||
|
(-12, 3, -4),
|
||||||
|
(0, 5, 0),
|
||||||
|
(7.5, 2.5, 3),
|
||||||
|
(-10, -2, 5),
|
||||||
|
]
|
||||||
|
|
||||||
|
for a, b, expected in test_cases_divide:
|
||||||
|
try:
|
||||||
|
result = divide(a, b)
|
||||||
|
status = "✓" if abs(result - expected) < 1e-10 else "✗"
|
||||||
|
print(f"{a} ÷ {b} = {result} {status} (期望: {expected})")
|
||||||
|
except ValueError as e:
|
||||||
|
print(f"{a} ÷ {b} = 错误: {e}")
|
||||||
|
|
||||||
|
# 测试除数为0的情况
|
||||||
|
print("\n测试除数为0的情况:")
|
||||||
|
print("-" * 30)
|
||||||
|
zero_test_cases = [
|
||||||
|
(5, 0),
|
||||||
|
(0, 0),
|
||||||
|
(-3, 0),
|
||||||
|
(10.5, 0),
|
||||||
|
]
|
||||||
|
|
||||||
|
for a, b in zero_test_cases:
|
||||||
|
try:
|
||||||
|
result = divide(a, b)
|
||||||
|
print(f"{a} ÷ {b} = {result} ✗ (应该抛出异常)")
|
||||||
|
except ValueError as e:
|
||||||
|
print(f"{a} ÷ {b} = 正确抛出异常: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
def interactive_mode():
|
||||||
|
"""交互式模式"""
|
||||||
print("\n" + "=" * 40)
|
print("\n" + "=" * 40)
|
||||||
print("手动测试模式 (输入 'exit' 退出)")
|
print("交互式计算模式 (输入 'exit' 退出)")
|
||||||
print("=" * 40)
|
print("=" * 40)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
user_input = input("请输入一个整数: ").strip()
|
print("\n选择操作:")
|
||||||
if user_input.lower() == 'exit':
|
print("1. 乘法")
|
||||||
print("程序结束。")
|
print("2. 除法")
|
||||||
|
print("exit. 退出")
|
||||||
|
|
||||||
|
choice = input("请选择 (1/2/exit): ").strip().lower()
|
||||||
|
|
||||||
|
if choice == 'exit':
|
||||||
|
print("退出交互模式。")
|
||||||
break
|
break
|
||||||
|
|
||||||
|
if choice not in ['1', '2']:
|
||||||
|
print("无效选择,请重新输入。")
|
||||||
|
continue
|
||||||
|
|
||||||
try:
|
try:
|
||||||
num = int(user_input)
|
a = float(input("请输入第一个数: "))
|
||||||
if is_palindrome(num):
|
b = float(input("请输入第二个数: "))
|
||||||
print(f"{num} 是回文数。")
|
|
||||||
else:
|
if choice == '1':
|
||||||
print(f"{num} 不是回文数。")
|
result = multiply(a, b)
|
||||||
|
print(f"{a} × {b} = {result}")
|
||||||
|
elif choice == '2':
|
||||||
|
try:
|
||||||
|
result = divide(a, b)
|
||||||
|
print(f"{a} ÷ {b} = {result}")
|
||||||
|
except ValueError as e:
|
||||||
|
print(f"错误: {e}")
|
||||||
|
|
||||||
except ValueError:
|
except ValueError:
|
||||||
print("错误:请输入有效的整数。")
|
print("错误:请输入有效的数字。")
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
"""主函数"""
|
||||||
|
print("数学运算工具测试")
|
||||||
|
print("=" * 40)
|
||||||
|
|
||||||
|
# 运行测试
|
||||||
|
test_operations()
|
||||||
|
|
||||||
|
# 询问是否进入交互模式
|
||||||
|
choice = input("\n是否进入交互式计算模式? (y/n): ").strip().lower()
|
||||||
|
if choice == 'y':
|
||||||
|
interactive_mode()
|
||||||
|
else:
|
||||||
|
print("程序结束。")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user