這個我直接用的4個command控件,如果你學過應該能看懂
Dim a As Integer, b As Integer, c As Integer
Private Sub Command1_Click()
a = InputBox("請輸入二次項系數", "輸入")
End Sub
Private Sub Command2_Click()
b = InputBox("請輸入一次項系數", "輸入")
End Sub
Private Sub Command3_Click()
c = InputBox("請輸入常數項", "輸入")
End Sub
Private Sub Command4_Click()
If b ^ 2 - 4 * a * c >= 0 Then
If b ^ 2 - 4 * a * c > 0 Then
X1 = (-b + Sqr(b ^ 2 - 4 * a * c)) / (2 * a)
X2 = (-b - Sqr(b ^ 2 - 4 * a * c)) / (2 * a)
MsgBox "x1=" & Format(X1, "#0.00") & vbCrLf & "x2=" & Format(X2, "#0.00")
End If
If b ^ 2 - 4 * a * c = 0 Then
X1 = (-b + Sqr(b ^ 2 - 4 * a * c)) / (2 * a)
MsgBox "x1=" & Format(X1, "#0.00")
End If
Else
d = (-b / (2 * a))
s = Sqr(-b ^ 2 + 4 * a * c)
MsgBox "x1=" & Format(d, "#0.00") & "+" & Format(s, "#0.0") & "i" & Chr(13) & "x2=" & Format(d, "#0.00") & "-" & Format(s, "#0.0") & "i"
End If
End Sub