標題:
VBA 解一元二次方程式(有溢位問題求解?)
[打印本頁]
作者:
iverson105
時間:
2020-3-9 16:42
標題:
VBA 解一元二次方程式(有溢位問題求解?)
請問各位大神 , 我從網路上下載的, 有 溢位 的問題??
請問如何解
謝謝
Sub 解一元二次方程式()
Dim X1 As String, X2 As String
If B ^ 2 - 4 * A * C >= 0 Then
X1 = (-1 * B + (B ^ 2 - 4 * A * C) ^ 0.5) / (2 * A)
X2 = (-1 * B - (B ^ 2 - 4 * A * C) ^ 0.5) / (2 * A)
If InStr(X1, ".") <> 0 Then
Text4.Text = Format(X1, "0.00")
Else
Text4.Text = X1
End If
If InStr(X2, ".") <> 0 Then
Text5.Text = Format(X2, "0.00")
Else
Text5.Text = X2
End If
Else
If B ^ 2 - 4 * A * C = 0 Then
X1 = (-1 * B) / (2 * A) '重根
X2 = (-1 * B) / (2 * A) '重根
If InStr(X1, ".") <> 0 Then
Text4.Text = Format(X1, "0.00")
Else
Text4.Text = X1
End If
If InStr(X2, ".") <> 0 Then
Text5.Text = Format(X2, "0.00")
Else
Text5.Text = X2
End If
Else
MsgBox "本方程式無實數解", vbExclamation
End If
End If
End Sub
Private Sub Command1_Click()
Dim A As Integer, B As Integer, C As Integer
A = Val(Text1.Text)
B = Val(Text2.Text)
C = Val(Text3.Text)
解一元二次方程式 A, B, C
End Sub
Private Sub Command2_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If InStr("0123456789-" & Chr(8) & Chr(13), Chr(KeyAscii)) = 0 Then KeyAscii = 0
If Len(Text1.Text) = 2 Then
If Left(Text1.Text, 1) <> "-" Then
Text1.Text = Left(Text1.Text, 1)
End If
End If
End Sub
複製代碼
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)