Complex : Complex Number « Data Type « VB.Net Tutorial






public class Test

   public Shared Sub Main
        Dim X, Y As Complex
        X = New Complex(1, 4)
        Y = New Complex(2, 3)

        Console.WriteLine((X + Y).ToString)
        Console.WriteLine((X - Y).ToString)
        Console.WriteLine((X * Y).ToString)
        Console.WriteLine((X = Y).ToString)
        Console.WriteLine((X <> Y).ToString)
        Console.WriteLine((-X).ToString)
        Dim abs_x As Double = CDbl(X)
        Console.WriteLine(abs_x.ToString)

   End Sub

End class


Public Class Complex
    Public Re As Double
    Public Im As Double

    Public Sub New()
    End Sub
    Public Sub New(ByVal real_part As Double, ByVal imaginary_part As Double)
        Re = real_part
        Im = imaginary_part
    End Sub

    Public Overrides Function ToString() As String
        Return Re.ToString & " + " & Im.ToString & "i"
    End Function

    Public Shared Operator *(ByVal c1 As Complex, ByVal c2 As Complex) As Complex
        Return New Complex( _
            c1.Re * c2.Re - c1.Im * c2.Im, _
            c1.Re * c2.Im + c1.Im * c2.Re)
    End Operator
    Public Shared Operator +(ByVal c1 As Complex, ByVal c2 As Complex) As Complex
        Return New Complex( c1.Re + c2.Re,c1.Im + c2.Im)
    End Operator
    Public Shared Operator -(ByVal c1 As Complex, ByVal c2 As Complex) As Complex
        Return New Complex(c1.Re - c2.Re, c1.Im - c2.Im)
    End Operator
    Public Shared Operator =(ByVal c1 As Complex, ByVal c2 As Complex) As Boolean
        Return (c1.Re = c2.Re) AndAlso (c1.Im = c2.Im)
    End Operator
    Public Shared Operator <>(ByVal c1 As Complex, ByVal c2 As Complex) As Boolean
        Return (c1.Re <> c2.Re) OrElse (c1.Im <> c2.Im)
    End Operator
    Public Shared Operator -(ByVal c1 As Complex) As Complex
        Return New Complex(c1.Im, c1.Re)
    End Operator
    Public Shared Narrowing Operator CType(ByVal c1 As Complex) As Double
        Return System.Math.Sqrt(c1.Re * c1.Re + c1.Im * c1.Im)
    End Operator
End Class
3 + 7i
-1 + 1i
-10 + 11i
False
True
4 + 1i
4.12310562561766








2.19.Complex Number
2.19.1.Complex
2.19.2.Complex Number