本人自己寫的一段vba代碼,如下:
類:
Option Explicit
Private real_num As Double
'實部
Private imaginary_num As Double
'虛部
Public Property Get real() As Double
real = real_num
End Property
Public Property Let real(ByVal re As Double)
real_num = re
End Property
Public Property Get imaginary() As Double
imaginary = imaginary_num
End Property
Public Property Let imaginary(ByVal imag As Double)
imaginary_num = imag
End Property
Public Sub sum(a As Complex_numble, b As Complex_numble)
real = (a.real + b.real)
imaginary = (a.imaginary + b.imaginary)
End Sub
'復數加法
Public Sub subtract(a As Complex_numble, b As Complex_numble)
real = (a.real - b.real)
imaginary = (a.imaginary - b.imaginary)
End Sub
'復數減法
Public Sub multiply(a As Complex_numble, b As Complex_numble)
real = (a.real * b.real - a.imaginary * b.imaginary)
imaginary = (a.real * b.imaginary + a.imaginary * b.real)
End Sub
'復數乘法
Public Sub divide(a As Complex_numble, b As Complex_numble)
real = ((a.real * b.real + a.imaginary * b.imaginary) / (b.real * b.real + b.imaginary * b.imaginary))
imaginary = ((b.real * a.imaginary - a.real * b.imaginary) / (b.real * b.real + b.imaginary * b.imaginary))
End Sub
'復數除法
Public Sub sin(a As Complex_numble)
sin.real = ((Math.sin(a.real) * (Math.Exp(a.imaginary) - Math.Exp(-a.imaginary))) / 2)
sin.imaginary = ((Math.cos(a.real) * (Math.Exp(a.imaginary) - Math.Exp(-a.imaginary))) / 2)
End Sub
'正弦
Public Sub cos(a As Complex_numble)
real = ((Math.cos(a.real) * (Math.Exp(a.imaginary) + Math.Exp(-a.imaginary))) / 2)
imaginary = ((Math.sin(a.real) * (Math.Exp(-a.imaginary) - Math.Exp(a.imaginary))) / 2)
End Sub
'余弦
Private Sub Class_Initialize()
real = 1
imaginary = 1
End Sub
模塊:
Attribute VB_Name = "模塊2"
Public Sub text1()
Dim a As New Complex_numble
Dim b As New Complex_numble
a.real = CDbl(Range("a2"))
a.imaginary = CDbl(Range("b2"))
b.real = CDbl(Range("a3"))
b.imaginary = CDbl(Range("b3"))
b.sin (a)
Range("c2") = b.real
Range("d2") = b.imaginary
End Sub
在這個標紅的地方運行不下去了,請高手指點哪里錯了
uj5u.com熱心網友回復:
uj5u.com熱心網友回復:
要改為: Call b.sin(a)或者: b.sin a
(就是把那個括號去掉)
另外,你的 Public Sub sin(a As Complex_numble) 里面,那兩個 sin. 都要去掉。
uj5u.com熱心網友回復:
多謝明月指點,問題已解決!uj5u.com熱心網友回復:
追問一下,如果我想把類中的所有數學運算方法改寫為function函式,該如何實作外部呼叫?uj5u.com熱心網友回復:
這個很簡單呀。如果改成函式呼叫,這個就沒必要用類模塊了。
把你的那些所有的 Sub 移到一個標準模塊中就行。
然后你參考這個代做:
Option Explicit
' 定義復數資料型別
Public Type Complex_numble
' 如果要在別的模塊中使用這種型別,就不要用 Private !
' 在標準模塊代碼中, Public 是默認的,可以不寫。
real As Double
imaginary As Double
End Type
' 下面就是一些“運算”函式的實作…………
'復數加法
Public Function sum(a As Complex_numble, b As Complex_numble) As Complex_numble
sum.real = a.real + b.real
sum.imaginary = a.imaginary + b.imaginary
End Function
' ***** 其它的函式,你照著改 *****
'注意:如果“運算”需要兩個復數來做,那你的函式引數就必須要有兩個。
再給你一個呼叫的示例:
Private Sub Test()
Dim a As Complex_numble
Dim b As Complex_numble
Dim c As Complex_numble
a.real = 5
a.imaginary = 20
b.real = 16
b.imaginary = -30
c = sum(a, b)
Debug.Print c.real, c.imaginary
' 輸出:
' 21 -10
End Sub
uj5u.com熱心網友回復:
代碼寫到標準模塊中,那幾個 Public Property Get / Let 的,直接就不要了。uj5u.com熱心網友回復:
呵呵,我搞復雜了轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/139693.html
標籤:VBA
上一篇:表單顯示例外?
下一篇:求從網站讀取動態資料代碼
