vb 一元二次方程的复根问题

来源:百度知道 编辑:UC知道 时间:2024/06/18 00:20:26
下面是我写的,只是考虑 b*b -4ac>0时的实根,如何把复根也放进来,谢谢了,希望能详细给我说一下...

Dim a As Integer, b As Integer, c As Integer

Private Sub Command1_Click()
If a <> 0 And (b * b) - 4 * a * c >= 0 Then
Text4.Text = Val((-b + Sqr((b * b) - 4 * a * c)) / 2 * a)
Text5.Text = Val((-b - Sqr((b * b) - 4 * a * c)) / 2 * a)
Else
i = MsgBox("b*b-4ac<0 无法计算", 0 + 64, "方程无实根")
End If
End Sub

Private Sub Text1_Click()
strs1 = "请输入a的值" + Chr(13) + Chr(10) + "然后单击确定"
a = InputBox$(strs1, "输入框", , 100, 100)
Text1 = a
If Text1 = 0 Then
i = MsgBox("a不能为零", 5 + 48, "重新输入a的值")
End If
End Sub

Private Sub Text2_Click()
strs2 = "请输入b的值" + Chr(13) + Chr(10) + "然后单击确定"
b = InputBox$(strs2, "输入框", , 100, 100)
Text2 = b
End Sub

Private Sub Text3_Click()
strs3 = "请输入c的值"

你的程序对a=0这种情况的说明不准确,应改为:
If a = 0 Then
MsgBox "这是一元一次方程"
x = -c / b
Text4.Visible = False
Text5 = x
这样比较完整。
考虑复根时:
Private Sub Command1_Click()
If a <> 0 And b^2 - 4 * a * c <0 Then
Text4 = (-b) / (2 * a) & "+j" & Sqr(-b ^ 2 + 4 * a * c) / (2 * a)
Text5 = (-b) / (2 * a) & "-j" & Sqr(-b ^ 2 + 4 * a * c) / (2 * a)
End If
End Sub

全部:
Dim a, b, c As Integer

Private Sub Command1_Click()
a = Val(Text1)
b = Val(Text2)
c = Val(Text3)
If a = 0 Then
MsgBox "这是一元一次方程"
x = -c / b
Text4.Visible = False
Text5 = x
Else
If a <> 0 And (b * b) - 4 * a * c >= 0 Then
Text4.Text = Val((-b + Sqr((b * b) - 4 * a * c)) / 2 * a)
Text5.Text = Val((-b - Sqr((b * b) - 4 * a * c)) / 2 * a)
ElseIf a <> 0 And (b * b) - 4 * a * c < 0 Then
Text4 = (-b) / (2 * a) & "+j" & Sqr(-b