解二次方程式AX^2+BX+C=0
一元二次方程式的一般形式是:
公式解法[編輯]
對於,它的根可以表示為:
有些時候也寫成:
Public Class Form1
Public A, B, C, D, X1, X2, U, V As Single
Public G1, G2, T As String
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
End
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
A = Val(TextBox2.Text)
B = Val(TextBox3.Text)
C = Val(TextBox4.Text)
'-------------------------------
If B >= 0 Then
G1 = "+"
Else
G1 = "-"
End If
If C >= 0 Then
G2 = "+"
Else
G2 = "-"
End If
'-------------------------------------------------------------------------
T = A.ToString & "X^2" & G1 & B.ToString + "X" & G2 & C.ToString & "= 0"
TextBox1.Text = "一元二次方程式" & T
D = B * B - 4 * A * C
TextBox5.Text = Str(D)
Select Case D
Case Is >= 0
X1 = -B / (2 * A) + Math.Sqrt(D) / (2 * A)
X2 = -B / (2 * A) - Math.Sqrt(D) / (2 * A)
If D > 0 Then TextBox6.Text = "有二不等實數根(實根)"
If D = 0 Then TextBox6.Text = "有二相等實數根(重根)"
TextBox7.Text = Str(X1)
TextBox8.Text = Str(X2)
Case Is < 0
U = -B / (2 * A)
V = Math.Sqrt(Math.Abs(D)) / (2 * A)
TextBox6.Text = "其解為共軛複根(虛根)"
TextBox7.Text = Str(U) + "+" + Str(V) + "i"
TextBox8.Text = Str(U) + "-" + Str(V) + "i"
End Select
End Sub
End Class
沒有留言:
張貼留言