用vb求一元二次方程的根,根的结果用Msgbox函数表示
发布网友
发布时间:2022-12-10 20:17
我来回答
共2个回答
热心网友
时间:2023-11-06 18:24
这种是相对简单的解法,直接用求根公式。而且没加化简解的公式,直接输出小数结果了。
Private Sub Command1_Click()
Dim a, b, c, x1, x2, delta
If Text1.Text = "" Then a = 1 Else a = Val(Text1.Text)
If Text2.Text = "" Then b = 1 Else b = Val(Text2.Text)
If Text1.Text = "-" Then a = -1
If Text2.Text = "-" Then b = -1
If Text3.Text = "" Then Text3.Text = 0
If Text4.Text = "" Then Text4.Text = 0
c = Val(Text3.Text) - Val(Text4.Text)
delta = b ^ 2 - 4 * a * c
If delta < 0 Then MsgBox ("无解")
If delta >= 0 Then x1 = ((b ^ 2 - 4 * a * c) ^ 0.5 - b) / 2 * a: x2 = (-(b ^ 2 - 4 * a * c) ^ 0.5 - b) / 2 * a
If delta >= 0 And x1 = x2 Then MsgBox ("x=" & x1)
If delta >= 0 And x1 <> x2 Then MsgBox ("x1=" & Str(x1) & " x2=" & Str(x2))
End Sub
Private Sub Text1_Change()
If Not (IsNumeric(Text1)) And Text1.Text <> "" And Text1.Text <> "-" Then MsgBox ("输入了非数字"): Text1.Text = ""
End Sub
Private Sub Text2_Change()
If Not (IsNumeric(Text2)) And Text2.Text <> "" And Text2.Text <> "-" Then MsgBox ("输入了非数字"): Text2.Text = ""
End Sub
Private Sub Text3_Change()
If Not (IsNumeric(Text3)) And Text3.Text <> "" And Text3.Text <> "-" Then MsgBox ("输入了非数字"): Text3.Text = ""
End Sub
Private Sub Text4_Change()
If Not (IsNumeric(Text4)) And Text4.Text <> "" And Text4.Text <> "-" Then MsgBox ("输入了非数字"): Text4.Text = ""
End Sub
热心网友
时间:2023-11-06 18:24
你直接把方程写成是y=**X的形式,然后,带入X求出Y,式子写在代码里,然后显示就可以了