vb编写一个程序,根据输入的系数求一元二次方程ax2+bx+c=0的根,要求考虑实根、虚根等情况,如果系数使得方程没有实数根,则弹出一个只有确定按钮的消息框,提示“方程没有实根”,如果有实根则也通过消息框显示.单击“退出”则结束工程.
2019-04-13
vb编写一个程序,根据输入的系数求一元二次方程ax2+bx+c=0的根,要求考虑实根、虚根等情况,
如果系数使得方程没有实数根,则弹出一个只有确定按钮的消息框,提示“方程没有实根”,如果有实根则也通过消息框显示.单击“退出”则结束工程.
优质解答
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
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