用VB制作“计算一元二次方程ax^2+bx+c=0”的根(a,b,c的值由键盘输入)

2024-11-23 12:29:14
推荐回答(2个)
回答1:

Private Sub Command1_Click()

Dim a#, b#, c#, x1#, x2#

a = Text1.Text

b = Text2.Text

c = Text3.Text

x1 = (-b + Sqr(b ^ 2 - 4 * a * c)) / (2 * a)

x2 = (-b - Sqr(b ^ 2 - 4 * a * c)) / (2 * a)

Print x1, x2

End Sub

回答2:

Private Sub Command1_Click()
Dim a As Integer, b As Integer, c As Integer, d As Single, f As Single
a = Val(Text1.Text)
b = Val(Text2.Text)
c = Val(Text3.Text)
d = b * b - 4 * a * c
If a <> 0 Then
If d > 0 Then
Text4.Text = (-b + Sqr(d)) / (2 * a)
Text5.Text = (-b - Sqr(d)) / (2 * a)
ElseIf d = 0 Then
Text4.Text = -b / (2 * a)
Text5.Text = -b / (2 * a)
Else
Text4.Text = -b / (2 * a) & "+" & Sqr(Abs(d)) / (2 * a) & "i"
Text5.Text = -b / (2 * a) & "-" & Sqr(Abs(d)) / (2 * a) & "i"
End If
Else
If b = 0 Then
MsgBox "方程无意义,请重新输入", 48, "输入错误"
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text1.SetFocus
Else
Text4.Text = -c / b
Text5.Text = -c / b
End If
End If
End Sub

Private Sub Command2_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
Text1.SetFocus
End Sub

Private Sub Command3_Click()
End
End Sub

,求 加分啊,朋友