Vb编程,编写一个一元二次方程的求解程序,要求能够判断方程是否有根
问题描述:
Vb编程,编写一个一元二次方程的求解程序,要求能够判断方程是否有根
如果没有根则给出相应的提示信息
答
Private Sub Command1_Click()
Dim a#, b#, c#, d#, x1#, x2#
a = Val(InputBox("a=", "数据输入框", 1))
b = Val(InputBox("b=", "数据输入框", 2))
c = Val(InputBox("c=", "数据输入框", 1))
If a = 0 Then
If b = 0 Then
If c = 0 Then Print "解得:x有无穷解" Else Print "x无解"
Else
Print "解得:x=" & -c / b
End If
Else
d = b * b - 4 * a * c
If d > 0 Then
x1 = (-b + Sqr(d)) / (2 * a)
x2 = (-b - Sqr(d)) / (2 * a)
Print "解得:x1=" & x1,
Print "x2=" & x2
Else
x1 = -b / (2 * a)
If d = 0 Then Print "解得:x=" & x1
If d Print "无实根"
End If
End If
End If
End Sub