用VB编写一个解一元二次方程 AX2+BX+C=0的程序
2019-04-13
用VB编写一个解一元二次方程 AX2+BX+C=0的程序
优质解答
Private Sub Command1_Click() Dim a As Single, b As Single, c As Single Dim d As Single, x1 As Single, x2 As Single a = InputBox("请输入一元二次方程的系数a") b = InputBox("请输入一元二次方程的系数b") c = InputBox("请输入一元二次方程的系数c") If a = 0 Then a = InputBox("因为a≠0,你输入的a=0,请重新输入系数a") End If d = b * b - 4 * a * c If d >= 0 Then x1 = (-b + Sqr(d)) / (2 * a) x2 = (-b - Sqr(d)) / (2 * a) Print "系数为"; a; b; c; "的一元二次方程的根分别为"; x1 Print "系数为"; a; b; c; "的一元二次方程的根分别为"; x2 Else Print "此方程在实数范围内无解" End If End Sub
Private Sub Command1_Click() Dim a As Single, b As Single, c As Single Dim d As Single, x1 As Single, x2 As Single a = InputBox("请输入一元二次方程的系数a") b = InputBox("请输入一元二次方程的系数b") c = InputBox("请输入一元二次方程的系数c") If a = 0 Then a = InputBox("因为a≠0,你输入的a=0,请重新输入系数a") End If d = b * b - 4 * a * c If d >= 0 Then x1 = (-b + Sqr(d)) / (2 * a) x2 = (-b - Sqr(d)) / (2 * a) Print "系数为"; a; b; c; "的一元二次方程的根分别为"; x1 Print "系数为"; a; b; c; "的一元二次方程的根分别为"; x2 Else Print "此方程在实数范围内无解" End If End Sub