I need help now!
输入一元二次方程的三个系数a,b,c,判断该方程是否有根,如果有则输出一元二次方程的两个根;否则输出该方程无根。 题目 输入一元二次方程的三个系数a,b,c,判断该方程是否有根,如果有则输出一元二次方程的两个根;否则输出该方程无根。
样例输入:1 1 1
样例输出:该方程无根
样例输入:1 2 1
样例输出:-1 -1
谢谢各位大佬帮忙看看
Private Sub Command1_Click() Text4.Text = "" Text5.Text = "" Dim A As Single, B As Single, C As Single Dim D As Single, X1 As Single, X2 As Single Dim R As Single, P As Single A = Val(Text1.Text) B = Val(Text2.Text) C = Val(Text3.Text) If A = 0 Then MsgBox "一元二次方程的系数A不能为0" Exit Sub Else D = B ^ 2 - 4 * A * C R = -B / (2 * A) If D = 0 Then Text4.Text = "相等实数根:" & vbCrLf Text4.Text = Text4.Text & R Text5.Text = "相等实数根:" & vbCrLf Text5.Text = Text5.Text & R ElseIf D > 0 Then X1 = (-B + Sqr(D)) / (2 * A) X2 = (-B - Sqr(D)) / (2 * A) Text4.Text = "不相等实数根:" & vbCrLf Text4.Text = Text4.Text & X1 Text5.Text = "不相等实数根:" & vbCrLf Text5.Text = Text5.Text & X2 ElseIf D < 0 Then P = Sqr(-D) / 2 * A Text4.Text = "不相等虚数根:" & vbCrLf Text4.Text = Text4.Text & R & "+" & P & "i" '其中 i 是虚数单位,就是-1的平方根,只有在D小于0时才有虚数根。 Text5.Text = "不相等虚数根:" & vbCrLf Text5.Text = Text5.Text & R & "-" & P & "i" End If End If End Sub