首页 > 数学 > 题目详情
编写函数求一元二次方程ax2+bx+c=0的解,分别通过文本框输入系数a,b,c,输出解x1,x2.
题目内容:
编写函数求一元二次方程ax2 +bx+c=0的解,分别通过文本框输入系数a,b,c,输出解x1,x2.优质解答
以下是核心算法:其中Text1,Text2,Text3是三个文本输入框,接受a,b,c三个系数.x1,x2为根
Dim a As Single,b As Single,c As Single,d As Single,ds As Single
Dim x1 As String,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)
ElseIf 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
——————————————————————转贴
优质解答
Dim a As Single,b As Single,c As Single,d As Single,ds As Single
Dim x1 As String,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)
ElseIf 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
——————————————————————转贴
本题链接: