VB分支结构程序设计代码

来源:百度知道 编辑:UC知道 时间:2024/09/21 09:24:19
用求根公式求一个一元二次方程的解,△会有正负,用分支结构代码怎么写,好心人写个代码把

界面设计:
绘制5个Text,1个Command

作用:3个text用于输入abc,另2个分别显示2个根,command用于点击后计算

代码:

Private Sub Command1_Click()
Dim a As Single, b As Single, c As Single, d As Single
a = Val(Text1.Text)
b = Val(Text2.Text)
c = Val(Text3.Text)
d = b ^ 2 - 4 * a * c
If d >= 0 Then
Text4.Text = (-b + Sqr(d)) / (2 * a)
Text5.Text = (-b - Sqr(d)) / (2 * a)
Else
Text4.Text = Str(-b / (2 * a)) & "+" & Str(Sqr(-d) / (2 * a)) & "i"
Text5.Text = Str(-b / (2 * a)) & "-" & Str(Sqr(-d) / (2 * a)) & "i"
MsgBox "无实数解!"
End If
End Sub