|
|
Title | Make a ComplexNumber class with operators in Visual Basic .NET |
Description | This example shows how to make a ComplexNumber class with operators (+, -, *, and unary -) in Visual Basic .NET. It uses private m_RealPart and m_ImaginaryPart variables to represent the number. It provides methods to add, subtract, negate, and multiply ComplexNumbers. |
Keywords | complex, complex number, real, imaginary, VB .NET, Visual Basic .NET |
Categories | Algorithms |
|
|
The ComplexNumber class uses the following code, which is fairly straightforward.
|
|
Public Class ComplexNumber
Private m_RealPart As Single
Private m_ImaginaryPart As Single
' Property procedures.
Public Property RealPart() As Single
Get
Return m_RealPart
End Get
Set(ByVal Value As Single)
m_RealPart = Value
End Set
End Property
Public Property ImaginaryPart() As Single
Get
Return m_ImaginaryPart
End Get
Set(ByVal Value As Single)
m_ImaginaryPart = Value
End Set
End Property
' Constructor using real and imaginary parts.
Public Sub New(Optional ByVal real_part As Single = _
0.0, Optional ByVal imaginary_part As Single = 0.0)
m_RealPart = real_part
m_ImaginaryPart = imaginary_part
End Sub
' Constructor using a String of the form R + Ii.
Public Sub New(ByVal string_value As String)
Dim pos As Integer
' Remove spaces and the "i".
string_value = string_value.Replace(" "c, "")
string_value = string_value.ToLower.Replace("i"c, _
"")
' Find the + or - between the parts.
Dim plus_or_minus() As Char = {"+"c, "-"c}
pos = string_value.IndexOfAny(plus_or_minus)
If pos = 0 Then
' Skip the leading +/-.
pos = string_value.IndexOfAny(plus_or_minus, 1)
End If
' Get the real and imaginary parts.
m_RealPart = Single.Parse(string_value.Substring(0, _
pos))
m_ImaginaryPart = _
Single.Parse(string_value.Substring(pos))
End Sub
' Return the negative of a complex number.
Public Shared Operator -(ByVal c1 As ComplexNumber) As _
ComplexNumber
Return New ComplexNumber( _
-c1.RealPart, _
-c1.ImaginaryPart)
End Operator
' Return the sum of two complex numbers.
Public Shared Operator +(ByVal c1 As ComplexNumber, _
ByVal c2 As ComplexNumber) As ComplexNumber
Return New ComplexNumber( _
c1.RealPart + c2.m_RealPart, _
c1.ImaginaryPart + c2.m_ImaginaryPart)
End Operator
' Return the difference between two complex numbers.
Public Shared Operator -(ByVal c1 As ComplexNumber, _
ByVal c2 As ComplexNumber) As ComplexNumber
Return c1 + -c2
End Operator
' Return Me * complex_number.
Public Shared Operator *(ByVal c1 As ComplexNumber, _
ByVal c2 As ComplexNumber) As ComplexNumber
Dim re_part As Single = _
c1.RealPart * c2.RealPart - _
c1.ImaginaryPart * c2.ImaginaryPart
Dim im_part As Single = _
c1.RealPart * c2.ImaginaryPart + _
c1.ImaginaryPart * c2.RealPart
Return New ComplexNumber(re_part, im_part)
End Operator
' Return a string representation of this number.
Public Overrides Function ToString() As String
' Start with the real part.
Dim result As String = m_RealPart.ToString()
' Add the imaginary part.
If m_ImaginaryPart >= 0 Then
result &= " + " & _
m_ImaginaryPart.ToString() & _
"i"
Else
result &= " - " & _
(-m_ImaginaryPart).ToString() & _
"i"
End If
Return result
End Function
End Class
|
|
For more information on algorithms in Visual Basic, see my book Ready-to-Run Visual Basic Algorithms.
|
|
|
|
|
|