正文

复数运算之二2008-11-14 20:54:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/iamben250/39393.html

分享到:

抽空把复数的基本结构给写了出来,现在简单说明一下。
该结构只是初步的编写,还没有正式使用,但基本要素:复数的表达、复数的运算(除了
开方)都包括了。
希望大家提出意见。
你在复制到vs中,需要improts system.math
Structure Complex
        Private Real As Single '设置复数的实数部分
        Property RealPart() As Single
            Get
                Return Real
            End Get
            Set(ByVal value As Single)
                Real = value
            End Set
        End Property
        Private Imaginary As Single '设置复数的虚数部分
        Property ImaginaryParty() As Single
            Get
                Return Imaginary
            End Get
            Set(ByVal value As Single)
                Imaginary = value
            End Set
        End Property
        Shadows ReadOnly Property ToString() As String '返回复数的标准形式,如
果没有指定值,返回0
            Get
                If Imaginary = 0 Then
                    Return Real.ToString
                End If
                If Imaginary < 0 Then
                    Return Real.ToString + Imaginary.ToString + "i"
                End If
                Return Real.ToString + "+" + Imaginary.ToString + "i"
            End Get
        End Property
        ReadOnly Property Add(ByVal Complex1 As Complex, ByVal Complex2 As
Complex) As Complex '加法运算
            Get
                Return ComplexAdd(Complex1, Complex2)
            End Get
        End Property
        Private Function ComplexAdd(ByVal Complex1 As Complex, ByVal
Complex2 As Complex) As Complex
            Real = Complex1.RealPart + Complex2.RealPart
            Imaginary = Complex1.ImaginaryParty + Complex2.ImaginaryParty
            Return Me
        End Function
        ReadOnly Property Substract(ByVal Complex1 As Complex, ByVal
Complex2 As Complex) As Complex '减法运算
            Get
                Return ComplexSub(Complex1, Complex2)
            End Get
        End Property
        Private Function ComplexSub(ByVal Complex1 As Complex, ByVal
Complex2 As Complex) As Complex
            Real = Complex1.RealPart - Complex2.RealPart
            Imaginary = Complex1.ImaginaryParty - Complex2.ImaginaryParty
            Return Me
        End Function
        ReadOnly Property Multiple(ByVal Complex1 As Complex, ByVal Complex2
As Complex) As Complex '乘法运算
            Get
                Return ComplexMul(Complex1, Complex2)
            End Get
        End Property
        Private Function ComplexMul(ByVal Complex1 As Complex, ByVal
Complex2 As Complex) As Complex
            Real = Complex1.RealPart * Complex2.RealPart -
Complex1.ImaginaryParty * Complex2.ImaginaryParty
            Imaginary = Complex1.RealPart * Complex2.ImaginaryParty +
Complex1.ImaginaryParty * Complex2.RealPart
            Return Me
        End Function
        ReadOnly Property Devide(ByVal Complex1 As Complex, ByVal Complex2
As Complex) As Complex '除法运算,没有考虑为0的情况,注意。
            Get
                Return ComplexDev(Complex1, Complex2)
            End Get
        End Property
        Private Function ComplexDev(ByVal Complex1 As Complex, ByVal
Complex2 As Complex) As Complex

            If Complex2.RealPart = 0 Then
                Real = Complex1.ImaginaryParty / Complex2.ImaginaryParty
                Imaginary = -Complex1.RealPart / Complex2.ImaginaryParty
            ElseIf Complex2.ImaginaryParty = 0 Then
                Real = Complex1.RealPart / Complex2.RealPart
                Imaginary = Complex1.ImaginaryParty / Complex2.RealPart
                Return Me
            ElseIf Abs(Complex2.RealPart) >= Abs(Complex2.ImaginaryParty)
Then
                Dim temp As Single = Complex2.ImaginaryParty /
Complex2.RealPart
                Real = (Complex1.RealPart + Complex1.ImaginaryParty * temp)
/ (Complex2.RealPart + Complex2.ImaginaryParty * temp)
                Imaginary = (Complex1.ImaginaryParty - Complex1.RealPart *
temp) / (Complex2.RealPart + Complex2.ImaginaryParty * temp)
                Return Me
            Else
                Dim temp As Single = Complex2.RealPart / Complex2.ImaginaryPar
ty
                Real = (Complex1.RealPart * temp + Complex1.ImaginaryParty)
/ (Complex2.RealPart * temp + Complex2.ImaginaryParty)
                Imaginary = (Complex1.ImaginaryParty * temp -
Complex1.RealPart) / (Complex2.RealPart * temp + Complex2.ImaginaryParty)
                Return Me

            End If
            Real = Complex1.RealPart * Complex2.RealPart -
Complex1.ImaginaryParty * Complex2.ImaginaryParty
            Imaginary = Complex1.RealPart * Complex2.ImaginaryParty +
Complex1.ImaginaryParty * Complex2.RealPart
            Return Me
        End Function
    End Structure

阅读(2670) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册