设计一程序用二分法解方程x*x+4x-4=0
2个回答

vb吧

Private Sub Command1_Click()

'定义一些变量 很简单 学过数学都懂.

Dim a As Single,b As Single

Dim c As Single,d As Single

Dim ds As Single

Dim x1,x2 As String

a = Val(Text1.Text)

b = Val(Text2.Text)

c = Val(Text3.Text)

d = b * b - 4 * a * c

ds = Sqr(Abs(d))

'下面是程序段将用到选择语句的嵌套.

If d > 0 Then

x1 = Str((-b + ds) / (2 * a))

x2 = Str((-b - ds) / (2 * a))

Else

If d = 0 Then

x1 = Str(-b / (2 * a)):x2 = x1

Else

b = Round(-b / (2 * a),7):c = Round(ds / (2 * a),7)

x1 = Str(b) + "+" + Str(c) + "i"

x2 = Str(b) + "-" + Str(c) + "i"

End If

End If

Text4.Text = x1:Text5.Text = x2 '使用文本框输出两个根

End Sub

Private Sub Form_Activate()

Text1.SetFocus '程序开始焦点放在text内 方便 习惯

End Su