vb 程序 输入一元二次方程ax2+bx+c=0的系数a,b,c,计算并输出一元二次方程的两个根x1,x2。 求解

2024-11-23 12:22:17
推荐回答(2个)
回答1:

Private Sub Command1_Click()
Dim a As Integer, b As Integer, c As Integer
Dim s As Single, x1 As Single, x2 As Single
a = Text1.Text
b = Text2.Text
c = Text3.Text
s = Sqr(b * b - 4 * a * c)
If s = 0 Then
x1 = x2 = -b / (2 * a)
Else
If s > 0 Then
x1 = (-b + s) / (2 * a)
x2 = (-b - s) / (2 * a)
Else
x1 = -b / (2 * a) + s & "i"
x2 = -b / (2 * a) - s & "i"
End If
End If
End Sub
可以不要那个结束按钮。

回答2:

这个问题是很简单的啊。
private sub command1_click()
dim a,b,c as sigle

a=val(text1.text )
b=val(text2.text )
c=val(text3.text )
if b^2-4*a*c>=0 then
x1=(-b-sqr( b^2-4*a*c ))/2*a
x2=(-b-sqr( b^2+4*a*c ))/2*a
else
x1= (-b-sqr( abs(b^2-4*a*c) ))/2*a & i
x2=(-b+sqr( abs(b^2-4*a*c) ))/2*a & i
end if
end sub