求一元二次方程ax^2+bx+c=0的根.用VB语言怎么编?知道的都来帮帮忙,急.
问题描述:
求一元二次方程ax^2+bx+c=0的根.用VB语言怎么编?知道的都来帮帮忙,急.
答
Private Sub Command1_Click()
Dim A As Single,B As Single,C As Single
Dim R1 As Single,R2 As Single
A = Val(Text1.Text)
B = Val(Text2.Text)
C = Val(Text3.Text)
If B ^ 2 - 4 * A * C MsgBox "该方程无解."
ElseIf B ^ 2 - 4 * A * C = 0 Then
R1 = -B / 2 / A
MsgBox "该方程仅有一个根为" & R1 & "."
Else
R1 = (-B + Sqr(B ^ 2 - 4 * A * C)) / (2 * A)
R2 = (-B - Sqr(B ^ 2 - 4 * A * C)) / (2 * A)
MsgBox "该方程有两个根分别为" & R1 & "和" & R2 & "."
End If
End Sub