急急急!!vb编写一个程序,根据输入的系数求一元二次方程ax2+bx+c=0的根,要求考虑实根、虚根等情况,

2024-11-08 12:16:48
推荐回答(1个)
回答(1):

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a, b, c, dt, s, s1, s2 As String
a = Val(TextBox1.Text)
b = Val(TextBox2.Text)
c = Val(TextBox3.Text)
dt = b ^ 2 - 4 * a * c
If dt < 0 Then
TextBox4.Text = "无实根"
Exit Sub
ElseIf (dt = 0) Then
s = (-b + Math.Sqrt(dt)) / (2 * a)
TextBox4.Text = "方程的唯一解是:X= " & s
Exit Sub
Else
s1 = (-b + Math.Sqrt(dt)) / (2 * a)
s2 = (-b - Math.Sqrt(dt)) / (2 * a)
TextBox4.Text = "s1=" & s1 & Chr(13) & Chr(10) & "s2" & s2

End If

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
End Class