## ProblemYou want an easy way to calculate with complex numbers. ## SolutionSample code folder: Chapter 06\ComplexNumbers Create a ## DiscussionThis recipe provides a great way to see how overloading standard operators can enhance the usability of your classes and structures. In this case, we've created a The following code defines the 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 ComplexNumber) As ComplexNumber ' ----- Add two complex numbers together. Return New ComplexNumber(a.Real + b.Real, _ a.Imaginary + b.Imaginary) End Operator Public Shared Operator -(ByVal a As ComplexNumber, _ ByVal b As ComplexNumber) As ComplexNumber ' ----- Subtract one complex number from another. Return New ComplexNumber(a.Real - b.Real, _ a.Imaginary - b.Imaginary) End Operator Public Shared Operator *(ByVal a As ComplexNumber, _ ByVal b As ComplexNumber) As ComplexNumber ' ----- Multiply two complex numbers together. 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 ComplexNumber) As ComplexNumber ' ----- Divide one complex number by another. Return a * Reciprocal(b) End Operator Public Shared Function Reciprocal( _ ByVal a As ComplexNumber) As ComplexNumber ' ----- Calculate the reciprocal of a complex number; ' that is, the 1/x calculation. Dim divisor As Double ' ----- Check for divide-by-zero possibility. divisor = a.Real * a.Real + a.Imaginary * a.Imaginary If (divisor = 0.0#) Then Throw New DivideByZeroException ' ----- Perform the operation. Return New ComplexNumber(a.Real / divisor, _ -a.Imaginary / divisor) End Function End Structure The overloaded The following code demonstrates how complex numbers are created and how standard operators allow mathematical operations such as addition and subtraction in a natural way. The overloaded + operator also impacts the += assignment operator. The last example in the code demonstrates this by adding complex number Dim result As New System.Text.StringBuilder Dim a As ComplexNumber Dim b As ComplexNumber Dim c As ComplexNumber a = New ComplexNumber(3, 4) 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()) MsgBox(result.ToString()) The ## Figure 6-16. Working with complex numbers in VB 2005## See AlsoSearch for " complex numbers" on the Web for more information on this subject. |

Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))

ISBN: 0596101775

EAN: 2147483647

EAN: 2147483647

Year: 2006

Pages: 400

Pages: 400

Authors: Tim Patrick, John Craig

Similar book on Amazon

- Frame-Mode MPLS Configuration and Verification
- Case Study-Hub and Spoke MPLS VPN Network Using BGP PE-CE Routing for Sites Using Unique AS Numbers
- Case Study-Inter-AS Implementing Route-Reflector and BGP Confederation in Provider Networks
- Case Study-Multi-Homed Inter-AS Provider Network
- MPLS QoS Implementation

flylib.com © 2008-2017.

If you may any questions please contact us: flylib@qtcs.net

If you may any questions please contact us: flylib@qtcs.net