VB的编程求一元二次方程的根
当判别式 b^2-4ac>=0时,x1 =[-b+sqr(b^2-4ac)]/2a, x2==[-b-sqr(b^2-4ac)]/2a
不是VB,是VFP编程 展开
这里分别采用两种输入输出方法。本题的关键点是判断输入数据的合法性,特别是a不能等于0。
第一种,系数a、b、c及方程的根都采用TextBox控件输入、输出;
第二种,系数a、b、c及方程的根都采用对话框方式输入、输出;
代码如下:
'对话框输入输出法
Private Sub Command1_Click()
Dim a As Single, b As Single, c As Single
Dim delta As Single, x1 As Single, x2 As Single
a = Val(InputBox("输入一元二次方程系数a"))
If a = 0 Then
a = InputBox("你输入的a=0,请重新输入系数a")
Else
b = Val(InputBox("输入一元二次方程系数b"))
c = Val(InputBox("输入一元二次方程系数c"))
delta = b * b - 4 * a * c
If delta >= 0 Then
x1 = (-b + Sqr(delta)) / (2 * a)
x2 = (-b - Sqr(delta)) / (2 * a)
MsgBox a & " x^2 +" & b & " x + " & c & "的根分别为: " & x1 & " " & x2
Else
MsgBox "此方程无实根"
End If
End If
End Sub
'文本框输入输出法
Private Sub Command2_Click()
a = Text1.Text
If a = 0 Then
a = InputBox("你输入的a=0,请重新输入系数a")
Else
b = Text2.Text
c = Text3.Text
delta = b * b - 4 * a * c
If delta >= 0 Then
x1 = (-b + Sqr(delta)) / (2 * a)
x2 = (-b - Sqr(delta)) / (2 * a)
Text4.Text = x1
Text5.Text = x2
Else
MsgBox "在实数范围内无解"
End If
End If
End Sub
Private Sub Command3_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
End Sub
Private Sub Command4_Click()
End
End Sub
是vb pro吗?
Visual Basic6.0或者Visual Basic.NET