程式說明:詢問Chat GPT,初期使用"re"模塊,而本人較不習慣,因此將其修改為較為簡易,導入"math"模塊。使用者輸入a,b,c三個數值,輸出結果將列出"多因式分解"結果。同時當輸入數值錯誤,則再次或的輸入機會,並提示。
程式執行截圖(我將其放置程式碼下方,感謝)
程式碼(主要程式):
```python
import math #重新輸入機會
from IPython.display import HTML
while True:
try:
a = int(input("請輸入 a: "))
b = int(input("請輸入 b: "))
c = int(input("請輸入 c: "))
break # 如果輸入成功,跳出迴圈
except ValueError:
print("輸入格式錯誤,請重新輸入!")
print(f'Your equation:\n{a} x^2 + {b} x + {c} = 0')
d = b * b - 4 * a * c
print('D =', '{0:g}'.format(d))
if d > 0:
x1 = (-b + math.sqrt(d)) / (2 * a)
x2 = (-b - math.sqrt(d)) / (2 * a)
html_text = f'<font color="green">{a} x^2 + {b} x + {c} = (x + {x1:.2f})(x + {x2:.2f})</font>'
display(HTML(html_text))
elif d == 0:
x = -b / (2 * a)
html_text = f'<font color="blue">x = {x:.2f}</font>'
display(HTML(html_text))
else:
html_text = f'<font color="red">無解</font>'
display(HTML(html_text))
real_part = -b / (2 * a)
imaginary_part = math.sqrt(abs(d)) / (2 * a)
x1 = complex(real_part, imaginary_part)
x2 = complex(real_part, -imaginary_part)
html_text = f'<font color="red">x1 = {x1}</font>'
display(HTML(html_text))
html_text = f'<font color="red">x2 = {x2}</font>'
display(HTML(html_text))
```


測試用程式碼(pytest):
```python
import math #測試用程式
from equation_solver import solve_equation
def test_solve_equation():
# Test case 1: D > 0 (Two real roots)
a, b, c = 1, -3, 2
roots = solve_equation(a, b, c)
assert len(roots) == 2
assert math.isclose(roots[0], 2.0, rel_tol=1e-6)
assert math.isclose(roots[1], 1.0, rel_tol=1e-6)
# Test case 2: D == 0 (One real root)
a, b, c = 1, -4, 4
roots = solve_equation(a, b, c)
assert len(roots) == 1
assert math.isclose(roots[0], 2.0, rel_tol=1e-6)
# Test case 3: D < 0 (Two complex roots)
a, b, c = 1, 2, 5
roots = solve_equation(a, b, c)
assert len(roots) == 2
assert roots[0] == complex(-1.0, 2.0)
assert roots[1] == complex(-1.0, -2.0)
# Test case 4: D > 0 (Different coefficients)
a, b, c = 2, 3, -5
roots = solve_equation(a, b, c)
assert len(roots) == 2
assert math.isclose(roots[0], 1.0, rel_tol=1e-6)
assert math.isclose(roots[1], -2.5, rel_tol=1e-6)
# Test case 5: D == 0 (Different coefficients)
a, b, c = 3, 0, 0
roots = solve_equation(a, b, c)
assert len(roots) == 1
assert math.isclose(roots[0], 0.0, rel_tol=1e-6)
# Test case 6: D < 0 (Different coefficients)
a, b, c = 5, 1, 3
roots = solve_equation(a, b, c)
assert len(roots) == 2
assert roots[0] == complex(-0.1, 1.4)
assert roots[1] == complex(-0.1, -1.4)
# Test case 7: a = 0 (Not a quadratic equation)
a, b, c = 0, 3, 5
roots = solve_equation(a, b, c)
assert roots is None
# Test case 8: b = 0 (Special case)
a, b, c = 2, 0, -8
roots = solve_equation(a, b, c)
assert len(roots) == 2
assert math.isclose(roots[0], 2.0, rel_tol=1e-6)
assert math.isclose(roots[1], -2.0, rel_tol=1e-6)
# Test case 9: c = 0 (Special case)
a, b, c = 2, -6, 0
roots = solve_equation(a, b, c)
assert len(roots) == 2
assert math.isclose(roots[0], 0.0, rel_tol=1e-6)
assert math.isclose(roots[1], 3.0, rel_tol=1e-6)
# Test case 10: a, b, c = 0 (Trivial case)
a, b, c = 0, 0, 0
roots = solve_equation(a, b, c)
assert roots is None
```
測試結果(由於本人'CS50'出了點問題,由Chat GPT代理執行pytest):

7/27期末考第一題,詢問Chat GPT連結:
https://chat.openai.com/share/fe609386-e46e-4ed8-89dd-a6af8d8cefe7