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

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » Data Type » Complex Number 
2.19.2.Complex Number
Public Class Tester


    Public Shared Sub Main
        Dim result As New System.Text.StringBuilder
        Dim As ComplexNumber
        Dim As ComplexNumber
        Dim As ComplexNumber

        a = New ComplexNumber(34)
        b = New ComplexNumber(5, -2)
        c = a + b

        result.AppendLine("Complex Numbers")
        result.AppendLine("a = " & a.ToString())
        result.AppendLine("b = " & b.ToString())

        ' ----- Addition.
        c = a + b
        result.AppendLine("a + b = " & c.ToString())

        ' ----- Subtraction.
        c = a - b
        result.AppendLine("a - b = " & c.ToString())

        ' ----- Multiplication.
        c = a * b
        result.AppendLine("a * b = " & c.ToString())

        ' ----- Division.
        c = a / b
        result.AppendLine("a / b = " & c.ToString())

        ' ----- Addition as assignment.
        a += b
        result.AppendLine("a += b ... a = " & a.ToString())

        Console.WriteLine(result.ToString())

    End Sub

    
End Class

Structure ComplexNumber
    Public Real As Double
    Public Imaginary As Double

    Public Sub New(ByVal realPart As Double, ByVal imaginaryPart As Double)
        Me.Real = realPart
        Me.Imaginary = imaginaryPart
    End Sub

    Public Sub New(ByVal sourceNumber As ComplexNumber)
        Me.Real = sourceNumber.Real
        Me.Imaginary = sourceNumber.Imaginary
    End Sub

    Public Overrides Function ToString() As String
        Return Real & "+" & Imaginary & "i"
    End Function

    Public Shared Operator +(ByVal a As ComplexNumber, _
            ByVal b As ComplexNumberAs ComplexNumber
        Return New ComplexNumber(a.Real + b.Real, a.Imaginary + b.Imaginary)
    End Operator

    Public Shared Operator -(ByVal a As ComplexNumber, _
            ByVal b As ComplexNumberAs ComplexNumber
        Return New ComplexNumber(a.Real - b.Real, a.Imaginary - b.Imaginary)
    End Operator

    Public Shared Operator *(ByVal a As ComplexNumber, _
            ByVal b As ComplexNumberAs ComplexNumber
        Return New ComplexNumber(a.Real * b.Real - a.Imaginary * b.Imaginary, _
            a.Real * b.Imaginary + a.Imaginary * b.Real)
    End Operator

    Public Shared Operator /(ByVal a As ComplexNumber, _
            ByVal b As ComplexNumberAs ComplexNumber
        Return a * Reciprocal(b)
    End Operator

    Public Shared Function Reciprocal(ByVal a As ComplexNumberAs ComplexNumber
        Dim divisor As Double

        divisor = a.Real * a.Real + a.Imaginary * a.Imaginary
        If (divisor = 0.0#Then Throw New DivideByZeroException

        Return New ComplexNumber(a.Real / divisor, -a.Imaginary / divisor)
    End Function
End Structure
Complex Numbers
a = 3+4i
b = 5+-2i
a + b = 8+2i
a - b = -2+6i
a * b = 23+14i
a / b = 0.241379310344828+0.896551724137931i
a += b ... a = 8+2i
2.19.Complex Number
2.19.1.Complex
2.19.2.Complex Number
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.